File size: 2,565 Bytes
5767e72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import subprocess
import os
import glob
import argparse
import sys
from tqdm import tqdm  # pip install tqdm

def check_integrity_ffmpeg(directory, extension="*.mp4"):
    # Normalize the extension path for glob
    search_pattern = os.path.join(directory, "**", extension)
    files = glob.glob(search_pattern, recursive=True)
    corrupt_files = []
    
    if not files:
        print(f"! No files found matching '{extension}' in '{directory}'")
        return []

    print(f"Scanning {len(files)} files using FFmpeg...")

    for video_path in tqdm(files):
        try:
            # Run ffmpeg, discard video output (-f null -), capture error logs
            # -v error: Only output fatal errors
            # -xerror: Stop on error
            cmd = [
                "ffmpeg", 
                "-v", "error", 
                "-i", video_path, 
                "-f", "null", 
                "-"
            ]
            
            # Run the command and capture output
            result = subprocess.run(
                cmd, 
                stdout=subprocess.DEVNULL, 
                stderr=subprocess.PIPE, 
                text=True
            )
            
            # If there is ANY output in stderr, the file has corruption
            if result.stderr:
                print(f"\n ! CORRUPT: {video_path}")
                # Print first line of error for context
                print(f"   Reason: {result.stderr.strip().splitlines()[0]}...") 
                corrupt_files.append(video_path)
                
        except Exception as e:
            print(f"! Script Error on {video_path}: {e}")

    return corrupt_files

if __name__ == "__main__":
    # Initialize Argument Parser
    parser = argparse.ArgumentParser(
        description="Recursively check integrity of video files using FFmpeg."
    )
    
    # Positional argument: The folder path
    parser.add_argument(
        "path", 
        type=str, 
        help="Path to the directory containing video files"
    )

    args = parser.parse_args()

    # Validate directory exists
    if not os.path.isdir(args.path):
        print(f"Error: The directory '{args.path}' does not exist.")
        sys.exit(1)

    # Run the check
    bad_ones = check_integrity_ffmpeg(args.path)
    
    print(f"\n--- Final Report ---")
    print(f"Found {len(bad_ones)} corrupt files.")
    
    if bad_ones:
        output_file = "corrupt_files_ffmpeg.txt"
        with open(output_file, "w") as f:
            f.write("\n".join(bad_ones))
        print(f"List saved to '{output_file}'")