[logseq-plugin-git:commit] 2023-03-16T19:36:37.141Z

This commit is contained in:
matze 2023-03-16 20:36:37 +01:00
parent f7c8106f01
commit cacf1cc5ee

61
journals/2023_03_16.md Normal file
View File

@ -0,0 +1,61 @@
- Mux files together
``` python
import os
import subprocess
import sys
import glob
import re
import shutil
ffmpeg_home:str = 'C:\\Users\\Monzi\\Desktop\\Portables\\ffmpeg-2021-01-01-git-63505fc60a-full_build\\bin'
mkvtoolnix_home:str = 'C:\\Users\\Monzi\\Desktop\\Portables\\mkvtoolnix-64-bit-51.0.0\\mkvtoolnix'
tmp_dir = 'C:\\tmp'
video_source_dir = sys.argv[1]
audio_source_dir = sys.argv[2]
result_dir = sys.argv[3]
def merge(video_source, audio_source, target):
tmp_target = os.path.join(tmp_dir, os.path.basename(video_file).replace('&','And'))
subprocess.call(
['ffmpeg.exe', '-i', video_source, '-i', audio_source,
'-map', '0:v', '-map', '1:a', '-map', '0:a', '-map', '0:s', '-disposition:a:0', 'default', '-disposition:a:1', 'none', '-c', 'copy',
tmp_target], shell = True, cwd=ffmpeg_home)
subprocess.call(
['mkvpropedit.exe', tmp_target, '--edit', 'track:s1', '--set', 'flag-default=0'], shell = True, cwd=mkvtoolnix_home)
shutil.move(tmp_target, target)
def scan(dir):
for path in sorted(glob.glob(f'{dir}/*.[ma][kv][vi]')):
yield path
def extract_episode(f):
return re.search(r"([sS][0-9]+[eE][0-9]+)", f).group(1)
def match_episode(f1, f2):
episode_1 = extract_episode(f1)
episode_2 = extract_episode(f2)
if episode_1.lower() != episode_2.lower():
raise Exception(f'Episode mismatch {episode_1} - {episode_2}')
video_files = list(scan(video_source_dir))
audio_files = list(scan(audio_source_dir))
l1 = len(video_files)
l2 = len(audio_files)
if l1 != l2:
raise Exception(f'Length mismatch: {l1} - {l2}')
for video_file, audio_file in zip(video_files, audio_files):
match_episode(video_file, audio_file)
for video_file, audio_file in zip(video_files, audio_files):
target_file = os.path.join(result_dir, os.path.basename(video_file))
print('Merging: ' + video_file + ' ' + audio_file + ' -> ' + target_file)
merge(video_file, audio_file, target_file)
```