File size: 3,763 Bytes
5de05bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import subprocess
import os
from shutil import copy2
import logging

log_file_path = 'log_VIDEO_ANALYSIS.log'

# Clear the log file at the start of each run
with open(log_file_path, 'w'):
    pass

logging.basicConfig(filename=log_file_path, level=logging.INFO, format='%(message)s')

current_dir = os.getcwd()
path_to_ffprobe = os.path.join(current_dir, 'ffprobe.exe')

specific_errors = [
    "moov atom not found",
    "contradictory STSC and STCO",
    "error reading header",
    "Invalid data found when processing input",
    "corrupt",
    "error",
    "invalid",
    "malformed",
    "missing",
    "partial file"
]

# Define a constant for the folder name
FOLDER_NAME = "ARCHIVE"

def log_and_print(message, log_only=False):
    if not log_only:
        print(message)
    logging.info(message)

def extract_error_summary(output):
    detailed_lines = []

    for line in output.splitlines():
        for error in specific_errors:
            if error.lower() in line.lower():
                detailed_lines.append(line)
    return detailed_lines

def analyze_video(file_path, ffprobe_path):
    try:
        cmd = [ffprobe_path, '-v', 'error', '-show_entries', 'format', '-show_entries', 'stream', '-show_entries', 'frame', '-print_format', 'json', file_path]
        result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        # Print only the original stderr
        print(result.stderr)
        
        detailed_lines = extract_error_summary(result.stderr)
        
        # Check if the only error is "missing picture in access unit with size 5"
        if len(detailed_lines) == 1 and "missing picture in access unit with size 5" in detailed_lines[0]:
            return False

        if detailed_lines:
            relative_path = os.path.relpath(file_path, current_dir)
            relative_path = relative_path.replace(f"{FOLDER_NAME}\\", "")
            log_and_print(f"{relative_path}:")
            for line in detailed_lines:
                log_and_print(f"{line}")
            log_and_print("-----------------------------------------------", log_only=True)
            return True
        
        return False
    except Exception as e:
        log_and_print(f"Error analyzing video {file_path}: {e}")
        return False

def copy_corrupted_file(file_path, target_folder, folder_path):
    if not os.path.exists(target_folder):
        os.makedirs(target_folder)
    
    # Retain the original path structure within the corrupted folder
    relative_path = os.path.relpath(file_path, folder_path)
    destination = os.path.join(target_folder, relative_path)
    
    # Create directories if they do not exist
    os.makedirs(os.path.dirname(destination), exist_ok=True)
    
    copy2(file_path, destination)

def main():
    current_dir = os.getcwd()
    folders_to_check = [FOLDER_NAME]
    for folder in folders_to_check:
        folder_path = os.path.join(current_dir, folder)
        corrupted_folder = os.path.join(folder_path, "corrupted")

        for root, dirs, files in os.walk(folder_path):
            if "corrupted" in dirs:
                dirs.remove("corrupted")

            for file in files:
                if file.lower().endswith(('.mp4', '.m4v')):
                    file_path = os.path.join(root, file)
                    relative_path = os.path.relpath(file_path, folder_path)
                    print(f"Checking: {relative_path}")
                    if analyze_video(file_path, path_to_ffprobe):
                        copy_corrupted_file(file_path, corrupted_folder, folder_path)

if __name__ == "__main__":
    main()

input("Press Enter to exit...")