0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ffmpegとpythonで動画のサイズを一括変換

Last updated at Posted at 2024-12-24

概要

pythonのos.system関数から、ffmpegを呼び出して動画のサイズ(幅)を一括で変更するスクリプトです。雑にいろんな動画の容量を軽くしたいときなどに使います。

注:2021年に書いた記事です。当時は自分用として限定公開にしていましたが、積極的に隠すほどでもないと考え直して公開します。

環境

% sw_vers
ProductName:    macOS
ProductVersion: 11.4
BuildVersion:   20F71
% python -V
Python 3.8.6

コード

import os
INPUT_DIR = os.path.join("input") #path to input movie directory
OUTPUT_DIR = os.path.join("output") #path to output movie directory
OUTPUT_WIDTH = 640#16:9の場合1280, 854, 640あたりから選ぶ

def resize(INPUT=None, OUTPUT=None, WIDTH=None, skip=True):
  if INPUT is None: INPUT = INPUT_DIR
  if OUTPUT is None: OUTPUT = OUTPUT_DIR
  if WIDTH is None: WIDTH = OUTPUT_WIDTH

  os.makedirs(OUTPUT, exist_ok=True)

  files = os.listdir(INPUT)
  files = [f for f in files if f.split(".")[-1] in ["mp4","MP4","m4v","MOV","mov"]]
  input_files = [os.path.join(INPUT, f) for f in files]
  output_files = [os.path.join(OUTPUT, f) for f in files]

  for ipt, opt in zip(input_files, output_files):
    if skip and os.path.exists(opt):
      print("{} is skipped".format(ipt))
      continue
    cmd = f"ffmpeg -i {ipt} -vf scale={WIDTH}:-1 {opt}"
    os.system(cmd)

if __name__=="__main__":
  resize()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?