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 .
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
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 .
# 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 .
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()
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 .
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 .
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
Catalog python Date calculator
Sometimes our code is always c