Ready for same framerate
This commit is contained in:
1 parent
cb56886a4b
commit
bd91d34d8a
2 files changed
+80
-31
No files matched your search
+65
-17
@@ -5,6 +5,7 @@ from pathlib import Path
|
||||
import cv2
|
||||
import numpy as np
|
||||
from scipy import signal
|
||||
import sys
|
||||
|
||||
def run(cmd):
|
||||
"""Run a subprocess command and raise on error."""
|
||||
@@ -82,12 +83,12 @@ def probe_video(path):
|
||||
|
||||
raise RuntimeError("No video stream found")
|
||||
|
||||
def trim_audio(input_mkv, start_time, end_time, out_audio):
|
||||
"""Trim only the first (and only) audio track from input_mkv."""
|
||||
def trim_audio(German_Videout_mkv, start_time, end_time, out_audio):
|
||||
"""Trim only the first (and only) audio track from German_Videout_mkv."""
|
||||
run([
|
||||
"ffmpeg",
|
||||
"-y",
|
||||
"-i", input_mkv,
|
||||
"-i", German_Videout_mkv,
|
||||
"-ss", str(start_time),
|
||||
"-to", str(end_time),
|
||||
"-map", "0:a:0", # first audio track
|
||||
@@ -95,17 +96,17 @@ def trim_audio(input_mkv, start_time, end_time, out_audio):
|
||||
out_audio
|
||||
])
|
||||
|
||||
def mux_audio_into_output(inp2, audio_file, output_dir):
|
||||
"""Mux trimmed audio into inp2 and place final file in output_dir."""
|
||||
def mux_audio_into_output(Quality_Video, audio_file, output_dir):
|
||||
"""Mux trimmed audio into Quality_Video and place final file in output_dir."""
|
||||
output_dir = Path(output_dir)
|
||||
output_dir.mkdir(exist_ok=True)
|
||||
|
||||
final_output = output_dir / f"{Path(inp2).stem}_ger.mkv"
|
||||
final_output = output_dir / f"{Path(Quality_Video).stem}_ger.mkv"
|
||||
|
||||
run([
|
||||
"mkvmerge",
|
||||
"-o", str(final_output),
|
||||
str(inp2),
|
||||
str(Quality_Video),
|
||||
"--language", "0:deu", # assign German language
|
||||
"--track-name", "0:German (2.0 aac)",
|
||||
str(audio_file)
|
||||
@@ -113,17 +114,53 @@ def mux_audio_into_output(inp2, audio_file, output_dir):
|
||||
|
||||
return final_output
|
||||
|
||||
def only_one(German_Video, Quality_Video):
|
||||
n1 = find_offset(German_Video, Quality_Video) # first frame to keep
|
||||
print(n1)
|
||||
sys.exit()
|
||||
output_dir = "./output_dir"
|
||||
|
||||
# --- Probe video ---
|
||||
fps, duration, _, vstart = probe_video(German_Video)
|
||||
_, duration2, _, _ = probe_video(Quality_Video)
|
||||
|
||||
# --- Compute timestamps ---
|
||||
start = vstart + (n1 / fps)
|
||||
end = start + duration2
|
||||
|
||||
if end <= start:
|
||||
raise ValueError("n2 must be greater than n1")
|
||||
|
||||
|
||||
|
||||
# if duration < end:
|
||||
# raise ValueError("German_Video must be longer than what it needs to be cut to")
|
||||
|
||||
print(f"fps: {fps}")
|
||||
print(f"Cutting audio from frame {n1} (timestamp {start:.6f}s) to timestamp {end:.6f}s")
|
||||
|
||||
# --- Trim only audio ---
|
||||
audio_file = Path(output_dir) / f"{Path(Quality_Video).stem}_german_audio.aac"
|
||||
trim_audio(German_Video, start, end, audio_file)
|
||||
|
||||
# --- Mux trimmed audio ---
|
||||
final_file = mux_audio_into_output(Quality_Video, audio_file, output_dir)
|
||||
|
||||
print(f"Final file with trimmed audio created at: {final_file}")
|
||||
|
||||
def main():
|
||||
for i in range(1, 12):
|
||||
# --- Inputs ---
|
||||
inp = 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"
|
||||
inp2 = f"/home/honney/projects/mkv/Anohana - The Flower We Saw That Day/S01/AnoHana The Flower We Saw That Day S01E0{i:02}.mkv"
|
||||
n1 = find_offset(inp, inp2) # first frame to keep
|
||||
# --- German_Videouts ---
|
||||
German_Video = 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"
|
||||
Quality_Video = f"/home/honney/projects/mkv/Anohana - The Flower We Saw That Day/S01/AnoHana The Flower We Saw That Day S01E0{i:02}.mkv"
|
||||
n1 = find_offset(German_Video, Quality_Video) # first frame to keep
|
||||
print(n1)
|
||||
sys.exit()
|
||||
output_dir = "./output_dir"
|
||||
|
||||
# --- Probe video ---
|
||||
fps, _, _, vstart = probe_video(inp)
|
||||
_, duration2, _, _ = probe_video(inp2)
|
||||
fps, duration, _, vstart = probe_video(German_Video)
|
||||
_, duration2, _, _ = probe_video(Quality_Video)
|
||||
|
||||
# --- Compute timestamps ---
|
||||
start = vstart + (n1 / fps)
|
||||
@@ -131,18 +168,29 @@ def main():
|
||||
|
||||
if end <= start:
|
||||
raise ValueError("n2 must be greater than n1")
|
||||
|
||||
if duration < end:
|
||||
raise ValueError("German_Video must be longer than what it needs to be cut to")
|
||||
|
||||
print(f"fps: {fps}")
|
||||
print(f"Cutting audio from frame {n1} (timestamp {start:.6f}s) to timestamp {end:.6f}s")
|
||||
|
||||
# --- Trim only audio ---
|
||||
audio_file = Path(output_dir) / f"{Path(inp2).stem}_german_audio.aac"
|
||||
trim_audio(inp, start, end, audio_file)
|
||||
audio_file = Path(output_dir) / f"{Path(Quality_Video).stem}_german_audio.aac"
|
||||
trim_audio(German_Video, start, end, audio_file)
|
||||
|
||||
# --- Mux trimmed audio ---
|
||||
final_file = mux_audio_into_output(inp2, audio_file, output_dir)
|
||||
final_file = mux_audio_into_output(Quality_Video, audio_file, output_dir)
|
||||
|
||||
print(f"Final file with trimmed audio created at: {final_file}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
if len(sys.argv) == 3:
|
||||
print(sys.argv)
|
||||
print(f"German_Video={sys.argv[1]}")
|
||||
print(f"Quality_Video={sys.argv[2]}")
|
||||
only_one(German_Video=sys.argv[1], Quality_Video=sys.argv[2])
|
||||
else:
|
||||
# print(len(sys.argv), sys.argv)
|
||||
# sys.exit()
|
||||
main()
|
||||
+15
-14
@@ -52,19 +52,20 @@ def find_offset(video_a, video_b):
|
||||
|
||||
return best_lag
|
||||
|
||||
# Usage
|
||||
for i in range(1, 12):
|
||||
# print(f"{i:02}")
|
||||
video_a = 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"
|
||||
video_b = f"/home/honney/projects/mkv/Anohana - The Flower We Saw That Day/S01/AnoHana The Flower We Saw That Day S01E0{i:02}.mkv"
|
||||
def multiple():
|
||||
# Usage
|
||||
for i in range(1, 12):
|
||||
# print(f"{i:02}")
|
||||
video_a = 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"
|
||||
video_b = f"/home/honney/projects/mkv/Anohana - The Flower We Saw That Day/S01/AnoHana The Flower We Saw That Day S01E0{i:02}.mkv"
|
||||
|
||||
offset_frames = find_offset(video_a, video_b)
|
||||
print(f"{i}: Video B is offset by {offset_frames} frames relative to Video A.")
|
||||
sys.exit()
|
||||
|
||||
def single():
|
||||
video_a = f"/home/honney/mount/Media/torent/[Anime Time] One Piece (0001-1071+Movies+Specials) [BD+CR] [Dual Audio] [1080p][HEVC 10bit x265][AAC][Multi Sub]/Season 01 - East Blue/[Anime Time] One Piece - 0001 - I'm Luffy! The Man Who's Gonna Be King of the Pirates!.mkv"
|
||||
video_b = f"/home/honney/mount/Media/tmp/serien/complicated/One Piece (1999-2025) [imdbid-tt0388629]/Season 01/One Piece S01E001.mkv"
|
||||
|
||||
offset_frames = find_offset(video_a, video_b)
|
||||
print(f"{i}: Video B is offset by {offset_frames} frames relative to Video A.")
|
||||
# sys.exit()
|
||||
|
||||
# video_a = f"/home/honney/projects/mkv/output_dir/AnoHana The Flower We Saw That Day S01E001_ger.mkv"
|
||||
# video_b = f"/home/honney/projects/mkv/Anohana - The Flower We Saw That Day/S01/AnoHana The Flower We Saw That Day S01E001.mkv"
|
||||
|
||||
# offset_frames = find_offset(video_a, video_b)
|
||||
# print(f"Video B is offset by {offset_frames} frames relative to Video A.")
|
||||
# sys.exit()
|
||||
print(f"Video B is offset by {offset_frames} frames relative to Video A.")
|
||||
Reference in new issue
Block a user