Table of Contents
Introduction
Get video information
Mirror processing
Add watermark
Video interception
Video splicing
Summary
Home Backend Development Python Tutorial Python gadget: Complete a day's workload in five minutes, it's so delicious

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

May 23, 2023 pm 04:43 PM
python tool

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Does mysql need the internet Does mysql need the internet Apr 08, 2025 pm 02:18 PM

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Does mysql need a server Does mysql need a server Apr 08, 2025 pm 02:12 PM

For production environments, a server is usually required to run MySQL, for reasons including performance, reliability, security, and scalability. Servers usually have more powerful hardware, redundant configurations and stricter security measures. For small, low-load applications, MySQL can be run on local machines, but resource consumption, security risks and maintenance costs need to be carefully considered. For greater reliability and security, MySQL should be deployed on cloud or other servers. Choosing the appropriate server configuration requires evaluation based on application load and data volume.

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

Can mysql store pdf Can mysql store pdf Apr 08, 2025 pm 01:48 PM

MySQL cannot directly store PDF files, and can be achieved by storing file paths or hash values ​​of binary data. The core idea is to use a table to store the following fields: ID, file name, file path (or hash value). The file path scheme stores file paths, which are simple and efficient, but depend on the file system for security; the file hash scheme stores the SHA-256 hash value of PDF files, which is more secure and can perform data integrity verification.

Can mysql connect to the sql server Can mysql connect to the sql server Apr 08, 2025 pm 05:54 PM

No, MySQL cannot connect directly to SQL Server. But you can use the following methods to implement data interaction: Use middleware: Export data from MySQL to intermediate format, and then import it to SQL Server through middleware. Using Database Linker: Business tools provide a more friendly interface and advanced features, essentially still implemented through middleware.

See all articles