syncing latest revisions - dk123
This commit is contained in:
@@ -2,6 +2,7 @@ import os
|
||||
import subprocess
|
||||
import sys
|
||||
import json
|
||||
import tracker
|
||||
from utils import verify_file_not_empty
|
||||
|
||||
def run_ffmpeg(args):
|
||||
@@ -15,10 +16,10 @@ def run_ffmpeg(args):
|
||||
subprocess.run(cmd, check=True)
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"FFmpeg Error: {e}")
|
||||
tracker.logger.error(f"FFmpeg Error: {e}")
|
||||
return False
|
||||
except FileNotFoundError:
|
||||
print("Error: 'ffmpeg' command not found. Please ensure it is installed on your host system.")
|
||||
tracker.logger.error("Error: 'ffmpeg' command not found. Please ensure it is installed on your host system.")
|
||||
return False
|
||||
|
||||
def get_video_duration(file_path):
|
||||
@@ -54,10 +55,10 @@ def extract_audio(video_path, output_path=None):
|
||||
output_path = f"{base_name}.wav"
|
||||
|
||||
if verify_file_not_empty(output_path):
|
||||
print(f"Audio file already exists: {output_path}")
|
||||
tracker.logger.info(f"Audio file already exists: {output_path}")
|
||||
return output_path
|
||||
|
||||
print(f"Extracting audio from {video_path}...")
|
||||
tracker.logger.info(f"Extracting audio from {video_path}...")
|
||||
|
||||
args = [
|
||||
"-i", video_path,
|
||||
@@ -72,7 +73,7 @@ def extract_audio(video_path, output_path=None):
|
||||
if run_ffmpeg(args):
|
||||
if not verify_file_not_empty(output_path):
|
||||
raise Exception("FFmpeg succeeded but output is empty.")
|
||||
print(f"Audio extracted to: {output_path}")
|
||||
tracker.logger.info(f"Audio extracted to: {output_path}")
|
||||
return output_path
|
||||
else:
|
||||
sys.exit(1)
|
||||
@@ -83,7 +84,7 @@ def embed_subtitles(video_path, srt_path, output_path=None):
|
||||
Includes strict safety checks (Size & Duration) to prevent replacing videos with corrupted files.
|
||||
"""
|
||||
if not os.path.exists(video_path) or not os.path.exists(srt_path):
|
||||
print("Error: Video or SRT file not found for embedding.")
|
||||
tracker.logger.error("Error: Video or SRT file not found for embedding.")
|
||||
return False
|
||||
|
||||
# Capture original stats
|
||||
@@ -91,14 +92,14 @@ def embed_subtitles(video_path, srt_path, output_path=None):
|
||||
original_duration = get_video_duration(video_path)
|
||||
|
||||
if original_size == 0:
|
||||
print("Error: Source video is 0 bytes.")
|
||||
tracker.logger.error("Error: Source video is 0 bytes.")
|
||||
return False
|
||||
|
||||
if output_path is None:
|
||||
base, ext = os.path.splitext(video_path)
|
||||
output_path = f"{base}.subbed{ext}"
|
||||
|
||||
print(f"Embedding subtitles into: {output_path}...")
|
||||
tracker.logger.info(f"Embedding subtitles into: {output_path}...")
|
||||
|
||||
sub_codec = "mov_text" if video_path.lower().endswith(".mp4") else "srt"
|
||||
|
||||
@@ -124,7 +125,7 @@ def embed_subtitles(video_path, srt_path, output_path=None):
|
||||
if run_ffmpeg(args):
|
||||
# --- Safety Checks ---
|
||||
if not os.path.exists(output_path):
|
||||
print("Error: Output file was not created.")
|
||||
tracker.logger.error("Error: Output file was not created.")
|
||||
return False
|
||||
|
||||
new_size = os.path.getsize(output_path)
|
||||
@@ -132,29 +133,24 @@ def embed_subtitles(video_path, srt_path, output_path=None):
|
||||
|
||||
# 1. Zero Byte Check
|
||||
if new_size == 0:
|
||||
print("❌ CRITICAL: Output file is 0 bytes. Deleting corrupted output.")
|
||||
tracker.logger.error("❌ CRITICAL: Output file is 0 bytes. Deleting corrupted output.")
|
||||
os.remove(output_path)
|
||||
raise Exception("Embedding failed: Output is empty.")
|
||||
|
||||
# 2. Significant Size Drop Check
|
||||
if new_size < (original_size * 0.8):
|
||||
print(f"❌ CRITICAL: Output file is significantly smaller than source!")
|
||||
print(f" Original: {original_size/1024/1024:.2f} MB")
|
||||
print(f" New: {new_size/1024/1024:.2f} MB")
|
||||
tracker.logger.error(f"❌ CRITICAL: Output file is significantly smaller than source!")
|
||||
os.remove(output_path)
|
||||
raise Exception("Embedding failed: Suspicious file size reduction.")
|
||||
|
||||
# 3. Duration Mismatch Check (New)
|
||||
if abs(original_duration - new_duration) > 1.0:
|
||||
print(f"❌ CRITICAL: Duration mismatch detected!")
|
||||
print(f" Original: {original_duration:.2f}s")
|
||||
print(f" New: {new_duration:.2f}s")
|
||||
print(" This indicates truncated video stream. Aborting.")
|
||||
tracker.logger.error(f"❌ CRITICAL: Duration mismatch detected!")
|
||||
os.remove(output_path)
|
||||
raise Exception("Embedding failed: Duration mismatch > 1s.")
|
||||
|
||||
print(f"Subtitles embedded successfully: {output_path}")
|
||||
tracker.logger.info(f"Subtitles embedded successfully: {output_path}")
|
||||
return True
|
||||
else:
|
||||
print("Error: Embedding failed.")
|
||||
tracker.logger.error("Error: Embedding failed.")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user