程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Gadget: it takes five minutes to handle the workload of a day. It smells good

編輯:Python

writing |  doug

source :Python technology 「ID: pythonall」

I wrote an article the other day   Automatic processing of short video materials   The article , A little friend said to use it directly python Of ffmpeg Kugengxiang , Just today, the leader arranged a task of adding watermark to the video in batch , Let's take it to test the water .

brief introduction

FFmpeg It is a powerful audio and video processing program , It is also the basis of many audio and video software , in fact ,FFmpeg It has become the standard of audio and video processing in the industry . But the command line uses FFmpeg There is a certain learning cost , and ffmpeg-python Library solves this problem well .

adopt pip After simple installation, it can be installed in python The code uses ffmpeg.

pip3 install ffmpeg-python

Get video information

path = 'main.mp4'
probe = ffmpeg.probe(path)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
width = int(video_stream['width'])
height = int(video_stream['height'])
print(width, height)

We can go through stream To get some basic information about the video , Like size 、 Duration 、 Frame rate, etc .

Image processing

#  Left and right mirror image
ffmpeg.input(path).hflip().output('output.mp4').run()
#  Up and down mirror image
ffmpeg.input(path).vflip().output('output.mp4').run()

It can be simply understood as English words (horizontal) And vertically (vertical) An acronym for .

Add watermark

main = ffmpeg.input(path)
logo = ffmpeg.input('logo.png')
ffmpeg.filter([main, logo], 'overlay', 0, 500).output('out.mp4').run()

What does this command mean , take logo The watermark image is placed in main At the top of the video , Coordinate for (0,500). The upper left corner of the video can be understood as the origin (0,0) The location of , From the origin to the right and down, respectively x Axis and y Axis .

Of course , If you put logo Big enough , Bigger than video , Then change the positions of both sides , That will become putting the video on logo Yes , In fact, it is equivalent to adding a background picture to the video .

ffmpeg.filter([logo, main], 'overlay', 0, 500).output('out.mp4').run()

Video capture

ffmpeg.input(path).trim(start_frame=10,end_frame=20).output('out3.mp4').run()

This command looks understandable ,start_frame and end_frame Respectively represent the start and end frames .

Video splicing

base = ffmpeg.input(path)
ffmpeg.concat(
    base.trim(start_frame=10, end_frame=20),
    base.trim(start_frame=30, end_frame=40),
    base.trim(start_frame=50, end_frame=60)
).output('out3.mp4').run()

Video splicing uses concat Function .

summary

Today I share with you a python A good library for processing video , I hope I can give you a job / Sidelines bring some efficiency improvements .

PS: Reply within company number 「Python」 You can enter Python Novice learning communication group , Together 100 Day plan !

Old rules , Brothers, remember , Right lower corner “ Looking at ” click , If you think the content of the article is good , Remember to share the circle of friends and let more people know !

Code Access method

Identify the QR code at the end of the text , reply : doug


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved