返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Python

Using Python+FFmpeg for Video Splitting and Merging

Preface Occasionally in daily life you'll run into situations that need some simple video editing. In the past I might have pulled out Premiere Pro to clip something (don't even mention those domestic Chinese apps—after you finish editing they force ads onto the beginning and end of the video, which is disgusting), but Premiere Pro is too heavyweight after all; even clipping a simple video takes quite a bit of time. Until I discovered FFmpeg... This FFmpeg is truly a godsend—free, open source, and powerful. It seems like a lot of software is built on top of it. Its command-line interface alone is already enough for our everyday simple video processing needs. I use it for simple video splitting and merging

曦远 (DealiAxy)约 12 分钟
目录7 个章节

Preface

Occasionally in daily life you'll run into situations that need some simple video editing. In the past I might have pulled out Premiere Pro to clip something (don't even mention those domestic Chinese apps—after you finish editing they force ads onto the beginning and end of the video, which is disgusting), but Premiere Pro is too heavyweight after all; even clipping a simple video takes quite a bit of time. Until I discovered FFmpeg...

This FFmpeg is truly a godsend—free, open source, and powerful. It seems like a lot of software is built on top of it. Its command-line interface alone is already enough for our everyday simple video processing needs. I use it for simple video splitting and merging, and it's really great.

Using the command line is fine for one or two files, but if you need to process multiple videos, or if the server has video processing needs (such as transcoding, adding watermarks, and the like), you need a convenient way to use FFmpeg in code. So this article will introduce seamless calling between Python and FFmpeg~

ffmpy

First, FFmpeg is an executable. To call FFmpeg from Python and monitor its status, you need to use something like os.system() or the subprocess module to create a subprocess, and you have to handle a lot of work yourself, which is rather troublesome...

The ffmpy library is what helps us solve this problem. It perfectly integrates FFmpeg and encapsulates subprocess management, so we just fill in the parameters and call it, and we're done.

Code

This article only briefly introduces how to call FFmpeg from Python. FFmpeg is very powerful; due to space limitations this article cannot cover all of FFmpeg's features one by one. Those of you who want to dive deeper into FFmpeg can check the references at the end~

Video Splitting

The example is the simplest usage of splitting a clip out of a video, and it uses the -vcodec copy -acodec copy parameters, meaning neither audio nor video is transcoded, which speeds up video processing.

Looking at the code below you'll understand the operation. Input and output are laid out crystal clear. Both inputs and outputs are dictionaries; the filename is the key, and the parameters are the value.

import ffmpy

ff = ffmpy.FFmpeg(
    inputs={'input.mp4': None},
    outputs={'output.mp4': [
        '-ss', '00:01:20',
        '-t', '02:00:00',
        '-vcodec', 'copy',
        '-acodec', 'copy'
    ]}
)

ff.run()

Before executing ff.run(), you can first use print(ff.cmd) to print the command and take a look.

The video splitting operation above corresponds to this FFmpeg command:

ffmpeg -i input.mp4 -ss 00:01:20 -t 02:00:00 -vcodec copy -acodec copy output.mp4

Video Merging

Video merging is a bit more troublesome. If you directly use an FFmpeg command with multiple video files as input and one as output, a transcoding process is required, which makes it very slow and defeats the purpose of merging.

I looked up some materials; only merging videos via a file list can concatenate without transcoding. However, this requires first creating a file to store the video list. In Python, we can achieve this by creating a temporary file.

The code is as follows:

import os
import tempfile
import ffmpy

# 创建临时文件
temp_dir = tempfile.mktemp()
os.mkdir(temp_dir)
concat_file = os.path.join(temp_dir, 'concat_list.txt')

with open(concat_file, 'w', encoding='utf-8') as f:
    f.write('\n'.join([
        'file C:/1.mp4',
        'file C:/2.mp4',
        'file C:/3.mp4',
    ]))

ff = ffmpy.FFmpeg(
    global_options=['-f', 'concat'],
    inputs={concat_file: None},
    outputs={'output.mp4': ['-c', 'copy']}
)

ff.run()

Done!

References

Feel Free to Discuss

Programming Lab focuses on exploring popular new internet technologies and practicing team agile development. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, and so on in the backend of the WeChat official account 「程序设计实验室」 to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~

_EOF_

Using Python+FFmpeg for Video Splitting and Merging

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

手写核心价值观编码工具-Python实现

前言 今天是五一劳动节,祝各位无产阶级劳动者节日快乐! 然后来整活分享一些有趣的东西~ 这个小工具是我大学时做着玩的,对于各位接班人来说,12个词的核心价值观这东西,大家都非常熟悉了,这工具可以实现将一段话编码为核心价值观实现加密,同时也能将密文解密出来。 为啥要做这个呢,隔太久不太记得了,~~好像是因为游戏里喷队友老是被屏蔽,灵机一动,要是换成和谐的文字不就不会被屏蔽了吗(误~~ 实现原理 简单

2022-05-01 · Python · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…