Added tests and more
This commit is contained in:
1 parent
295177be6f
commit
a7a7abf720
9 files changed
+1327
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import subprocess
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
def shift_mka(file: Path, offset: float) -> None:
|
||||
"""
|
||||
Hard-edits an MKA file by shifting audio data.
|
||||
Positive offset: Adds silence at start (Delay).
|
||||
Negative offset: Trims the beginning (Advance).
|
||||
"""
|
||||
if not file.is_file():
|
||||
print(f"File not found: {file}")
|
||||
return
|
||||
|
||||
# Create a normal file path right next to the original file
|
||||
# If the file is "/folder/audio.mka", this becomes "/folder/shifted_audio.mka"
|
||||
new_file = file.parent / f"shifted_{file.name}"
|
||||
|
||||
# 1. Determine the filter
|
||||
if offset > 0:
|
||||
# Delay all channels by X milliseconds
|
||||
ms = int(offset * 1000)
|
||||
audio_filter = f"adelay={ms}:all=1"
|
||||
elif offset < 0:
|
||||
# Trim the start and reset timestamps to 0
|
||||
audio_filter = f"atrim=start={abs(offset)},asetpts=PTS-STARTPTS"
|
||||
else:
|
||||
print("Offset is 0, no shift needed.")
|
||||
return
|
||||
|
||||
# 2. Execute FFmpeg
|
||||
cmd = [
|
||||
"ffmpeg", "-y",
|
||||
"-i", str(file),
|
||||
"-af", audio_filter,
|
||||
"-c:a", "libopus",
|
||||
str(new_file)
|
||||
]
|
||||
|
||||
print(f"Processing shift: {offset}s...")
|
||||
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
|
||||
if result.returncode == 0:
|
||||
# Move the new file over the original file (overwriting it)
|
||||
shutil.move(str(new_file), str(file))
|
||||
print(f"Successfully updated: {file.name}")
|
||||
else:
|
||||
print(f"FFmpeg Error:\n{result.stderr}")
|
||||
# Clean up the leftover file if the process failed
|
||||
if new_file.exists():
|
||||
new_file.unlink()
|
||||
|
||||
shift_mka(Path("/home/honney/mount/Media/tmp/serien/ready/Classroom of the Elite/Season 01/E01_german_audio.mka"), 0.9592916666666667)
|
||||
Reference in new issue
Block a user