Artifact ffmpeg - operation video, extremely comfortable

Hello, everyone. I'm in a line

Tiktok before you have introduced the small program of playing this way.

The basic principle is to upload the video, click the applet through the link in the lower left corner of the video, and then the applet provider will return commission every thousand clicks

The operation process is to download the material, create the material, and then upload the material

Constantly modify the iterative content in order to achieve quantitative change and qualitative change

Tiktok has been shared before, and then the batch processing material is processed.

Of course, for the first time, the material is processed manually by clipping, and then processed automatically by code. Otherwise, you may not even know which direction the automatic processing is going

After the processing flow is clear, how to use Python to process video materials?


ffmpeg is a set of open source computer programs that can be used to record, convert digital audio and video, and convert them into streams

With this artifact, you can process all kinds of video material content, which is extremely comfortable

Here's a line to share how to use this artifact

1 installation

First of all, you need to download the latest version from the ffmpeg official website, otherwise there may be various strange problems

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 check whether the installation is successful

ffmpeg -version

2subprocess call

Because ffmpeg is a command-line tool, it can be used through the call of subprocess

To view and use documents first, for example, the command to split video files is:

ffmpeg -i [filename] -ss [starttime] -t [length] -c copy [newfilename]

  • i is the file to be cropped

  • ss is the start time of clipping

  • t is the end time or length of clipping

  • c is the storage of cut documents

Well, write a call in Python:

import subprocess as sp

def 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)

  • A function is defined to pass in the information required by ffmpeg through parameters

  • Write the clipping command as a string template and replace the parameters in it

  • Execute the command with Popen of subprocess, where the parameter shell=True means to execute the command as a whole

  • p.wait() is very important because tailoring takes a while and is executed by another process, so you need to wait for the execution to complete before continuing to execute the following work, otherwise you may not find the file


ffmpy3 is the python wrapper of ffmpeg so that you can use this package to process video

You need to install with pip first

pip install ffmpy3

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

import ffmpy3
ff = ffmpy3.FFmpeg(
     inputs={'input.mp4': None},
     outputs={'output.avi': None}


If we want to use different codecs to re encode video and audio, we must specify additional output options while outputting the file:

ff = FFmpeg(
    inputs={'input.ts': None},
    outputs={'output.mp4': '-c:a mp2 -mpeg2video'}


You can also output the audio and video of the file into two mp4 format files

ff = FFmpeg(
    inputs={'input.ts': None},
         'video.mp4': ['-map', '0:0', '-c:a', 'copy', '-f', 'mp4'],
         'audio.mp4': ['-map', '0:1', '-c:a', 'copy', '-f', 'mp4']


Multiplexing is to combine the input mp4 file and mp3 file into the same video file

Here, OrderedDict is used to preserve the order of inputs so that they match the order of flows in the output options:

from collections import OrderedDict
inputs = OrderedDict([('video.mp4', None), ('audio_1.mp3', None), ('audio_2.mp3', None)])
outputs = {'output.ts', '-map 0 -c:v h264 -map 1 -c:a:0 ac3 -map 2 -c:a:1 mp2'}
ff = FFmpeg(inputs=inputs, outputs=outputs)

Split into pictures

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

Change the output file type to the end of. png

import ffmpy3
ff = ffmpy3.FFmpeg(
     inputs={'data.MP4': None},
     outputs={'1/%d.png': None}

4 finally

Processing video through ffmpeg mainly needs to understand its command usage, which can be found in 👇 In the official documents of


Then go to understand the templates of various popular videos and automatically generate the videos you want

That's all for today's sharing

I'm a professional. Technology never sleeps. I'll see you next time

Hello, I'm a member of my team. I graduated from Xiamen University with a master's degree. I have published two top papers in python. I share python's technical learning, interview skills, awareness of making money, etc. welcome to pay attention
@After playing python for 3 days, we have selected 9 aspects of computer technology materials, hoping to help you: Extraction code: 0000

Tags: Python Programmer

Posted on Wed, 24 Nov 2021 17:52:41 -0500 by wikstov