Home > Article > Backend Development > Python gadget: Complete a day’s workload in five minutes, it’s so delicious
FFmpeg is a set of powerful audio and video processing programs and the basis of many audio and video software. In fact, FFmpeg has become the industry standard for audio and video processing. . However, there is a certain learning cost for using FFmpeg from the command line, and the ffmpeg-python library solves this problem very well.
After simple installation through pip, you can use ffmpeg in python code.
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 use stream to get some basic information of the video, such as size, duration, frame rate, etc.
# 左右镜像 ffmpeg.input(path).hflip().output('output.mp4').run() # 上下镜像 ffmpeg.input(path).vflip().output('output.mp4').run()
can be simply understood as the abbreviation of the English words horizontal (horizontal) and vertical (vertical).
main = ffmpeg.input(path) logo = ffmpeg.input('logo.png') ffmpeg.filter([main, logo], 'overlay', 0, 500).output('out.mp4').run()
This command means to place the logo watermark image above the main video at the coordinate of (0,500). The upper left corner of the video can be understood as the position of the origin (0,0), and the x-axis and y-axis are represented to the right and downward from the origin respectively.
Of course, if you make the logo big enough, larger than the video, and then change the positions of the two sides, it will become the video on the logo, which is actually equivalent to adding a Background image.
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 seems easy to understand. start_frame and end_frame represent the start and end frames respectively.
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 can be done using the concat function.
Today I will share with you a good library for processing videos in python. I hope it can bring some efficiency improvements to your work/side job.
The above is the detailed content of Python gadget: Complete a day’s workload in five minutes, it’s so delicious. For more information, please follow other related articles on the PHP Chinese website!