Python gadget: Complete a day's workload in five minutes, it's so delicious

王林
Release: 2023-05-23 16:43:06
forward
1472 people have browsed it

Python gadget: Complete a days workload in five minutes, its so delicious

Introduction

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
Copy after login

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)
Copy after login

We can use stream to get some basic information of the video, such as size, duration, frame rate, etc.

Mirror processing

# 左右镜像
ffmpeg.input(path).hflip().output('output.mp4').run()
# 上下镜像
ffmpeg.input(path).vflip().output('output.mp4').run()
Copy after login

can be simply understood as the abbreviation of the English words horizontal (horizontal) and vertical (vertical).

Add watermark

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

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()
Copy after login

Video interception

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

This command seems easy to understand. start_frame and end_frame represent the start and end frames respectively.

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()
Copy after login

Video splicing can be done using the concat function.

Summary

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template