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...")