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?

broken ディレクトリ内のmp4を復旧させrepairedディレクトリに出すpythonコード

Posted at

うむ、任されたのじゃ。
以下に、broken/ ディレクトリ内のすべての .mp4 ファイルを修復して、repaired/ ディレクトリに出力するPythonスクリプトを示すぞい。


🐍 ffmpeg を使ったMP4一括修復スクリプト

import os
import subprocess

# ディレクトリ設定
input_dir = "broken"
output_dir = "repaired"

# 出力ディレクトリが存在しない場合は作成
os.makedirs(output_dir, exist_ok=True)

# 入力ディレクトリ内のファイルを走査
for filename in os.listdir(input_dir):
    if filename.lower().endswith(".mp4"):
        input_path = os.path.join(input_dir, filename)
        output_path = os.path.join(output_dir, filename)

        print(f"🔧 修復中: {filename}")
        
        # FFmpegコマンドでremux(再パッケージ)
        command = [
            "ffmpeg",
            "-err_detect", "ignore_err",  # エラー無視オプション(必要に応じて)
            "-i", input_path,
            "-c", "copy",                 # 再エンコードせずコピー
            "-map", "0",                  # 全ストリームをマッピング
            "-movflags", "faststart",     # moov atomを先頭に移動
            output_path
        ]
        
        # 実行
        result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        if result.returncode == 0:
            print(f"✅ 完了: {filename}")
        else:
            print(f"❌ 失敗: {filename}")
            print(result.stderr.decode())

print("🎉 すべてのファイルを処理しました。")

✅ 実行前の準備

  1. broken/ フォルダに復旧したい .mp4 を入れる
  2. repaired/ フォルダは自動作成される(または手動でもOK)
  3. ffmpeg がインストールされており、PATHに通っていること

何かファイル名に日本語や空白がある場合、さらに対策が必要じゃが、まずはこのコードで動作確認をしてみると良いぞい。必要ならバッチ対応やログ保存も追加できるのじゃ。

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?