Created
June 25, 2020 19:42
-
-
Save WANGJIEKE/23f858e82ba0693d67f346c56a975c17 to your computer and use it in GitHub Desktop.
Convert mkv to mp4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
mkv2mp4.py | |
This script needs MKVToolNix and FFmpeg to run | |
""" | |
USAGE = 'usage: mkv2mp4.py INPUT' | |
from pathlib import Path | |
import subprocess | |
import sys | |
import tempfile | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print(f'error: invalid argument(s)', file=sys.stderr) | |
print(USAGE, file=sys.stderr) | |
sys.exit(1) | |
input_path = Path(sys.argv[1]).resolve(strict=True) | |
with tempfile.TemporaryDirectory() as temp_dir: | |
temp_dir = Path(temp_dir) | |
video = temp_dir / 'video' | |
audio = temp_dir / 'audio' | |
subprocess.run( | |
['mkvextract', str(input_path), 'tracks', f'0:{video}', f'1:{audio}'], | |
check=True | |
) | |
subprocess.run( | |
['ffmpeg', '-i', str(video), '-i', str(audio), '-c' , 'copy', f'{input_path.parent / f"{input_path.stem}.mp4"}'], | |
check=True | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment