在系统中安装ffmpeg

#win 
可以直接下载

#linux 
apt install ffmpeg -y

安装ffmpeg-python

pip install ffmpeg-python

使用方法

ffmpeg.run(stream_spec, cmd="/xxx/xx/ffmpeg", capture_stdout=False, capture_stderr=False, input=None, quiet=False, overwirte_output=False)

参数说明

https://github.com/kkroening/ffmpeg-python
https://python-ffmpeg.readthedocs.io/en/stable/api/

https://www.jianshu.com/p/38b28bb3df79

#查看支持的 filter
./ffmpeg.exe -filters

#查看详细的参数
./ffmpeg.exe  -h filter=fade
  • 实例

  • 获取视频信息(宽高)

probe = ffmpeg.probe(args.in_filename)
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'])
  • 从视频中截取一帧图片 (从视频中的时间 4 秒生成宽度 500px 的缩略图)
video = ffmpeg.input('Pencil.mp4', ss=4)
video = video.filter('scale', 500, -1)
video= ffmpeg.output(video,'output.png', vframes=1)
ffmpeg.run(video)
  • 翻转视频
#水平翻转视频
video = ffmpeg.input('Pencil.mp4')
video = ffmpeg.hflip(video)
video = ffmpeg.output(video, 'horizontal.mp4')
ffmpeg.run(video)

#垂直翻转视频
video = ffmpeg.input('Pencil.mp4')
video = ffmpeg.vflip(video)
video = ffmpeg.output(video, 'vertical.mp4')
ffmpeg.run(video)