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
目录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
- ffmpy official documentation: https://ffmpy-zh.readthedocs.io/zh/latest/ffmpy.html
- Generating video thumbnails with Python: https://blog.csdn.net/fei347795790/article/details/102068777
- FFMPEG video splitting and merging: https://www.jianshu.com/p/cf1e61eb6fc8
- An introductory tutorial to FFmpeg video processing: https://www.ruanyifeng.com/blog/2020/01/ffmpeg.html
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~
- Cnblogs: https://www.cnblogs.com/deali/
- Live coding stream: https://live.bilibili.com/11883038
- Zhihu: https://www.zhihu.com/people/dealiaxy
_EOF_
Using Python+FFmpeg for Video Splitting and Merging
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
手写核心价值观编码工具-Python实现
前言 今天是五一劳动节,祝各位无产阶级劳动者节日快乐! 然后来整活分享一些有趣的东西~ 这个小工具是我大学时做着玩的,对于各位接班人来说,12个词的核心价值观这东西,大家都非常熟悉了,这工具可以实现将一段话编码为核心价值观实现加密,同时也能将密文解密出来。 为啥要做这个呢,隔太久不太记得了,~~好像是因为游戏里喷队友老是被屏蔽,灵机一动,要是换成和谐的文字不就不会被屏蔽了吗(误~~ 实现原理 简单
2022-05-01 · Python · 约 1 分钟
Jupyter-Notebook-简单配置
安装主题 默认主题太丑了,必须换个好看的主题,pip安装: pip install jupyterthemes 装完之后: # 列出所有主题 jt -l # 设置主题 # -T 启用工具栏 # -N 启用名称栏 jt -t 主题名称 -T -N 自动补全 安装nbextensions pip install jupyter_contrib_nbextensions jupyter contrib
2021-12-18 · Python · 约 1 分钟
配置pip国内镜像加快python第三方库安装速度~
现在这网络对于开发者来说有点不太友好,下载什么东西都慢慢,pip install慢,git clone也慢,难受…… 不过还好有各种代替的国内镜像源可以加速 临时配置 在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple 例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple d
2022-11-12 · Python · 约 1 分钟
Python网络请求库,从 requests 到 httpx
Requests早该退休了,httpx这老古董才配活在异步时代?HTTP/2和超时?这些基本配置requests早该有。异步支持原生?别逗了,这年头谁还用同步。
2026-03-27 · Python · 约 1 分钟
讨论
共 0 条加载评论中…