Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ffmpeg to process Video material in Python

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how Python uses ffmpeg to deal with video material". In daily operation, I believe many people have doubts about how Python uses ffmpeg to deal with video material. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how Python uses ffmpeg to deal with video material". Next, please follow the editor to study!

I. installation

First of all, you need to download the latest version from the official website of ffmpeg, otherwise all kinds of strange problems may arise.

Because ffmpeg is a command line tool, you need to add ffmpeg to the environment variable during installation. After installation, you can enter the following command in the terminal to see if the installation is successful.

Ffmpeg-version II, subprocess call

Because ffmpeg is a command line tool, you can use it through calls to subprocess

Take a look at the usage document first, for example, the command to split the video file is:

Ffmpeg-I [filename]-ss [starttime]-t [length]-c copy [newfilename]

I is the file that needs to be cut

Ss is the start time of the crop

T is the end time or length of the cut

C to store the cut files

All right, write a call in Python:

Import subprocess as spdef cut_video (filename, outfile, start, length=90): cmd = "ffmpeg-I% s-ss% d-t% d-c copy% s"% (filename, start, length, outfile) p = sp.Popen (cmd, shell=True) p.wait () return

Defines a function that passes in the information needed by ffmpeg through parameters

Write the crop command as a string template and replace the parameters into it

Execute the command with the Popen of subprocess, where the parameter shell=True indicates that the command is executed as a whole

P.wait () is important because tailoring takes a while and is executed by another process, so you need to wait for execution to complete before continuing with the rest of the work, otherwise the file may not be found

III. Ffmpy3

Ffmpy3 is the python wrapper for ffmpeg so that you can use this package to process video

Need to install with pip first

Pip install ffmpy3

For example, if you modify the file format, you can use ffmpy3 to output directly.

Import ffmpy3ff = ffmpy3.FFmpeg (inputs= {'input.mp4': None}, outputs= {' output.avi': None}) ff.run () transcoding

If we want to re-encode video and audio using different codecs at the same time, we must specify additional output options at the same time as the output file:

Ff = FFmpeg (inputs= {'input.ts': None}, outputs= {' output.mp4':'- CMV a mp2-mpeg2video'}) ff.cmdff.run () demultiplexing

You can also output the audio and video of a file to two files in mp4 format

Ff = FFmpeg (inputs= {'input.ts': None}, outputs= {' video.mp4': ['- map', '0VOV),'-copy','- mp4'], 'audio.mp4': ['-map', '0VOV 1V','- cRV'- FF', 'copy','-f' 'mp4']}) ff.cmdff.run () reuse

Reuse is to synthesize the input mp4 file and mp3 file into the same video file.

OrderedDict is used here to preserve the order of inputs so that they match the order of the streams in the output options:

From collections import OrderedDictinputs = OrderedDict ([('video.mp4', None), (' audio_1.mp3', None), ('audio_2.mp3', None)]) outputs= {' output.ts','- map 0-c:a:1 mp2' v h364-map 1-c:a:0 ac3-map 2-c:a:1 mp2'} ff = FFmpeg (inputs=inputs, outputs=outputs) ff.cmdff.run () split into pictures

To deal with the content of the video material, you also need to split the video into pictures, process the pictures, and then merge them into videos.

Change the output file type to .png at the end.

Import ffmpy3ff = ffmpy3.FFmpeg (inputs= {'data.MP4': None}, outputs= {' 1% d.pngmovie: None}) ff.run () here, the study on "how Python uses ffmpeg to process video material" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report