Ready for same framerate
This commit is contained in:
1 parent
cb56886a4b
commit
bd91d34d8a
2 files changed
+75
-26
No files matched your search
+66
-18
@@ -5,6 +5,7 @@ from pathlib import Path
|
|||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import signal
|
from scipy import signal
|
||||||
|
import sys
|
||||||
|
|
||||||
def run(cmd):
|
def run(cmd):
|
||||||
"""Run a subprocess command and raise on error."""
|
"""Run a subprocess command and raise on error."""
|
||||||
@@ -82,12 +83,12 @@ def probe_video(path):
|
|||||||
|
|
||||||
raise RuntimeError("No video stream found")
|
raise RuntimeError("No video stream found")
|
||||||
|
|
||||||
def trim_audio(input_mkv, start_time, end_time, out_audio):
|
def trim_audio(German_Videout_mkv, start_time, end_time, out_audio):
|
||||||
"""Trim only the first (and only) audio track from input_mkv."""
|
"""Trim only the first (and only) audio track from German_Videout_mkv."""
|
||||||
run([
|
run([
|
||||||
"ffmpeg",
|
"ffmpeg",
|
||||||
"-y",
|
"-y",
|
||||||
"-i", input_mkv,
|
"-i", German_Videout_mkv,
|
||||||
"-ss", str(start_time),
|
"-ss", str(start_time),
|
||||||
"-to", str(end_time),
|
"-to", str(end_time),
|
||||||
"-map", "0:a:0", # first audio track
|
"-map", "0:a:0", # first audio track
|
||||||
@@ -95,17 +96,17 @@ def trim_audio(input_mkv, start_time, end_time, out_audio):
|
|||||||
out_audio
|
out_audio
|
||||||
])
|
])
|
||||||
|
|
||||||
def mux_audio_into_output(inp2, audio_file, output_dir):
|
def mux_audio_into_output(Quality_Video, audio_file, output_dir):
|
||||||
"""Mux trimmed audio into inp2 and place final file in output_dir."""
|
"""Mux trimmed audio into Quality_Video and place final file in output_dir."""
|
||||||
output_dir = Path(output_dir)
|
output_dir = Path(output_dir)
|
||||||
output_dir.mkdir(exist_ok=True)
|
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([
|
run([
|
||||||
"mkvmerge",
|
"mkvmerge",
|
||||||
"-o", str(final_output),
|
"-o", str(final_output),
|
||||||
str(inp2),
|
str(Quality_Video),
|
||||||
"--language", "0:deu", # assign German language
|
"--language", "0:deu", # assign German language
|
||||||
"--track-name", "0:German (2.0 aac)",
|
"--track-name", "0:German (2.0 aac)",
|
||||||
str(audio_file)
|
str(audio_file)
|
||||||
@@ -113,17 +114,15 @@ def mux_audio_into_output(inp2, audio_file, output_dir):
|
|||||||
|
|
||||||
return final_output
|
return final_output
|
||||||
|
|
||||||
def main():
|
def only_one(German_Video, Quality_Video):
|
||||||
for i in range(1, 12):
|
n1 = find_offset(German_Video, Quality_Video) # first frame to keep
|
||||||
# --- Inputs ---
|
print(n1)
|
||||||
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"
|
sys.exit()
|
||||||
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
|
|
||||||
output_dir = "./output_dir"
|
output_dir = "./output_dir"
|
||||||
|
|
||||||
# --- Probe video ---
|
# --- Probe video ---
|
||||||
fps, _, _, vstart = probe_video(inp)
|
fps, duration, _, vstart = probe_video(German_Video)
|
||||||
_, duration2, _, _ = probe_video(inp2)
|
_, duration2, _, _ = probe_video(Quality_Video)
|
||||||
|
|
||||||
# --- Compute timestamps ---
|
# --- Compute timestamps ---
|
||||||
start = vstart + (n1 / fps)
|
start = vstart + (n1 / fps)
|
||||||
@@ -132,17 +131,66 @@ def main():
|
|||||||
if end <= start:
|
if end <= start:
|
||||||
raise ValueError("n2 must be greater than n1")
|
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"fps: {fps}")
|
||||||
print(f"Cutting audio from frame {n1} (timestamp {start:.6f}s) to timestamp {end:.6f}s")
|
print(f"Cutting audio from frame {n1} (timestamp {start:.6f}s) to timestamp {end:.6f}s")
|
||||||
|
|
||||||
# --- Trim only audio ---
|
# --- Trim only audio ---
|
||||||
audio_file = Path(output_dir) / f"{Path(inp2).stem}_german_audio.aac"
|
audio_file = Path(output_dir) / f"{Path(Quality_Video).stem}_german_audio.aac"
|
||||||
trim_audio(inp, start, end, audio_file)
|
trim_audio(German_Video, start, end, audio_file)
|
||||||
|
|
||||||
# --- Mux trimmed audio ---
|
# --- 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}")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for i in range(1, 12):
|
||||||
|
# --- 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, 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}")
|
print(f"Final file with trimmed audio created at: {final_file}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__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()
|
main()
|
||||||
@@ -52,19 +52,20 @@ def find_offset(video_a, video_b):
|
|||||||
|
|
||||||
return best_lag
|
return best_lag
|
||||||
|
|
||||||
# Usage
|
def multiple():
|
||||||
for i in range(1, 12):
|
# Usage
|
||||||
|
for i in range(1, 12):
|
||||||
# print(f"{i:02}")
|
# 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_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"
|
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)
|
offset_frames = find_offset(video_a, video_b)
|
||||||
print(f"{i}: Video B is offset by {offset_frames} frames relative to Video A.")
|
print(f"{i}: Video B is offset by {offset_frames} frames relative to Video A.")
|
||||||
# sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
# video_a = f"/home/honney/projects/mkv/output_dir/AnoHana The Flower We Saw That Day S01E001_ger.mkv"
|
def single():
|
||||||
# video_b = f"/home/honney/projects/mkv/Anohana - The Flower We Saw That Day/S01/AnoHana The Flower We Saw That Day S01E001.mkv"
|
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)
|
offset_frames = find_offset(video_a, video_b)
|
||||||
# print(f"Video B is offset by {offset_frames} frames relative to Video A.")
|
print(f"Video B is offset by {offset_frames} frames relative to Video A.")
|
||||||
# sys.exit()
|
|
||||||
Reference in new issue
Block a user