A lot of bug fixes and edge cases

This commit is contained in:
Hannes committed 2026-03-22 03:21:02 +01:00
1 parent 5e7a73d78f
commit 295177be6f
3 files changed
+46 -338

No files matched your search

+44 -67
View File
@@ -10,7 +10,7 @@ import os
import glob
import tempfile
import shutil
from typing import Tuple
from typing import Tuple, Optional
DEBUG = True
TARGET_FPS = 30
@@ -221,7 +221,7 @@ def stick_together_audio(left_audio: Path, right_audio: Path, output_audio: Path
shutil.move(output_audio_tmp, output_audio)
def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offset: None | int = None):
def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offset: Optional[int] = None):
print(f"Working on {Quality_Video}")
@@ -229,82 +229,52 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
fps, duration, _, vstart = probe_video(German_Video)
fps2, duration2, _, vstart2 = probe_video(Quality_Video)
# if fps != fps2:
# raise ValueError("FPS of both Videos must be the same (May not work yet, more testing needs to be done)")
if duration <= 0:
raise ValueError("Duration of German_Video must be greater than 0")
if duration2 <= 0:
raise ValueError("Duration of Quality_Video must be greater than 0")
if offset == None:
n1, offset = find_offset(Quality_Video, German_Video)
print(f"Offset: {n1} frames| {offset}s")
if offset > duration or offset > duration2:
n1, offset0 = find_offset(Quality_Video, German_Video)
print(f"Offset: {n1} frames| {offset0}s")
else:
offset0 = offset
if offset0 > duration or offset0 > duration2:
raise ValueError("Offset needs to be smaller than the video durations")
# print(f"fps: {fps}")
# print(f"Cutting audio from frame {n1} (timestamp {start:.6f}s) to timestamp {end:.6f}s")
# --- Trim only audio ---
german_audio_file = output_dir / f"{Path(Quality_Video).stem}_german_audio.mka"
quality_audio_file1 = output_dir / f"{Path(Quality_Video).stem}_original_audio1.mka"
quality_audio_file2 = output_dir / f"{Path(Quality_Video).stem}_original_audio2.mka"
# if offset >= 0:
# offset = -offset
# # Quality starts later than German
# start = vstart + offset
# if duration < offset + duration2:
# # When the german audio is shorter than the original video
# end = start + duration
# if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s")
# trim_audio(German_Video, start, end, german_audio_file)
# # Get the end from the first original audio
# start2 = vstart2 + (end - vstart)
# end2 = vstart2 + duration2
# if end2 > start2:
# if DEBUG: print(f"Cutting audio ({quality_audio_file2}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s")
# trim_audio(Quality_Video, start2, end2, quality_audio_file2)
# else:
# end = start + duration2
# if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s")
# trim_audio(German_Video, start, end, german_audio_file)
# else:
# German starts Later than Quality
# Get the start from the original Audio
if os.path.isfile(quality_audio_file1):
os.remove(quality_audio_file1)
if os.path.isfile(quality_audio_file2):
os.remove(quality_audio_file2)
### Get the first part till german starts (if german does not start before file)
start2 = vstart2
end2 = start2 + offset
end2 = start2 + offset0
if end2 - start2 > 0.05:
if DEBUG: print(f"Cutting audio ({quality_audio_file1}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s")
trim_audio(Quality_Video, start2, end2, quality_audio_file1)
###
# if duration - offset < duration2:
# When the german audio is shorter than the original video
start = vstart
end = start + duration
### Get the German Audio
### TODO: get the offset off if german starts before the original
if offset0 < 0:
start = vstart - offset0
else:
start = vstart
end = min(vstart + duration, start + duration2)
if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s")
trim_audio(German_Video, start, end, german_audio_file)
# Get the end from the first original audio
### Get the end from the first original audio if it is longer than the german part
start2 = vstart2 + end
# end2 = start2 + (duration2 - duration - (n1 / fps))
end2 = vstart2 + get_audio_duration(Quality_Video)
# print(f"duration {duration}")
# print(f"duration2 {duration2}")
if end2 - start2 > 0.5:
if DEBUG: print(f"Cutting audio ({quality_audio_file2}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s")
trim_audio(Quality_Video, start2, end2, quality_audio_file2)
# else:
# start = vstart
# end = start + duration2
# if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s")
# trim_audio(German_Video, start, end, german_audio_file)
# german_audio_file2 = output_dir / f"{Path(Quality_Video).stem}_german_audio2.mka"
# german_audio_file3 = output_dir / f"{Path(Quality_Video).stem}_german_audio3.mka"
# if os.path.isfile(quality_audio_file1):
# stick_together_audio(quality_audio_file1, german_audio_file, german_audio_file2)
# if os.path.isfile(quality_audio_file2):
# stick_together_audio(german_audio_file2, quality_audio_file2, german_audio_file3)
if os.path.isfile(quality_audio_file1):
stick_together_audio(quality_audio_file1, german_audio_file, german_audio_file)
@@ -325,23 +295,30 @@ def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offse
os.remove(quality_audio_file2)
def main():
output_dir = "/home/honney/mount/Media/tmp/serien/ready/Maid-sama (2010) [tmdb-65648]/Season 01"
start_num = 17
episode_num = 26
output_dir = "./output_dir"
start_num = 1
episode_num = 11
for i in range(start_num, episode_num+1):
# for i in [7, 8, 10, 11, 13, 16, 17, 19, 20, 22, 25]:
# for i in [7]:
# --- German_Videouts ---
pattern = f"/home/honney/mount/Media/tmp/serien/Maid Sama All/Maid Sama/S01/{i:02}.mkv"
print(f"German_Video={pattern}")
German_Video = glob.glob(pattern)
pattern = f"/home/honney/mount/Media/tmp/serien/Maid Sama All/Kaichou wa Maid-sama!/S01/{i:02}.mkv"
Quality_Video = glob.glob(pattern)
german_pattern = f"/home/honney/projects/mkv/AnoHana The Flower We Saw That Day (2011-2011) [imdbid-tt1913273]/Season 01/AnoHana The Flower We Saw That Day S01E0{i:02}.mkv"
if os.path.isfile(german_pattern):
German_Video = [german_pattern]
else:
German_Video = glob.glob(german_pattern)
quality_pattern = f"/home/honney/projects/mkv/Anohana - The Flower We Saw That Day/S01/AnoHana The Flower We Saw That Day S01E0{i:02}.mkv"
if os.path.isfile(quality_pattern):
Quality_Video = [quality_pattern]
else:
Quality_Video = glob.glob(quality_pattern)
print(f"German_Video={German_Video}")
print(f"Quality_Video={Quality_Video}")
if len(German_Video) > 1:
raise ValueError("There are to many videos matching the German wildcard")
if len(Quality_Video) > 1:
raise ValueError("There are to many videos matching the Quality wildcard")
if len(German_Video) != 1:
raise ValueError(f"Error with German wildcard:\n{german_pattern}\n{German_Video}")
if len(Quality_Video) != 1:
raise ValueError(f"Error with Quality wildcard:\n{quality_pattern}\n{Quality_Video}")
process_one(Path(German_Video[0]), Path(Quality_Video[0]), Path(output_dir))
# sys.exit()