pythonで動画編集する

利用ツール - FFmpeg

FFmpeg とは FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation. It is also highly portable: FFmpeg compiles, runs, and passes our testing infrastructure FATE across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc. under a wide variety of build environments, machine architectures, and configurations.

1、環境構築

1-1. ffmpegをインストールする

  1. 公式サイトからインストーラーをダウンロード.
  2. PATHの設定 PATHが設定しないと、以下のようなエラーが発生する。
Traceback (most recent call last):
...
<module>
    video_info = ffmpeg.probe(path)
  File "../ffmpeg/_probe.py", line 20, in probe
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "../python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "../python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe'
  1. 開発環境 PATHの代わりに
    /usr/local/bin
    lrwxr-xr-x  1 shoutoneulle  admin  43 Jun  3 23:38 ffprobe -> /Volumes/Tom/work/projects/jdai/bin/ffprobe
    lrwxr-xr-x  1 shoutoneulle  admin  42 Jun  3 23:38 ffmpeg -> /Volumes/Tom/work/projects/jdai/bin/ffmpeg
    

1-2. ffmpeg-pythonをインストールする

ffmpeg-pythonはsubprocessでCLIからffmpegを実行するwrapperのみのパッケージ

pip install ffmpeg-python

DONE!

2、動作確認

import ffmpeg
video_file = '/Volumes/Tom/work/company/video/jdai.mp4'
video_info = ffmpeg.probe(video_file)
print(video_info)
{'streams': [{'index': 0, 'codec_name': 'h264', 'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part ...'}}}

3、色んな処理