Skip to content

Instantly share code, notes, and snippets.

@ManeFunction
Last active October 21, 2023 00:26
Show Gist options
  • Save ManeFunction/8351b01b6a601d5545ef2ca1f00ee022 to your computer and use it in GitHub Desktop.
Save ManeFunction/8351b01b6a601d5545ef2ca1f00ee022 to your computer and use it in GitHub Desktop.
MKA + MKV batch combiner (py)
# This script can easily combine MKV and MKA files to the single MKV package
# Required installed ffmpeg (can be easily done with brew install ffmpeg)
# As a bonus, can copy ass subtites as well
# Enjoy :)
# by Mane Function (2023)
import os
import shutil
import subprocess
# Define the root folder and output path
root_folder = '/path/to/source/folder' # Source folder path
output_path = '/path/to/target/folder' # Target folder path
# Recursively traverse the root folder
for folder, subfolders, files in os.walk(root_folder):
for file in files:
if file.endswith('.mkv'):
input_video = os.path.join(folder, file)
input_audio = os.path.join(folder, file.replace('.mkv', '.mka'))
output_file = os.path.join(output_path, os.path.relpath(input_video, root_folder))
# Create the output directory structure if it doesn't exist
output_dir = os.path.dirname(output_file)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Construct the FFmpeg command
ffmpeg_command = [
'ffmpeg', '-i', input_video, '-i', input_audio,
'-codec', 'copy', output_file
]
# Run the FFmpeg command
subprocess.call(ffmpeg_command)
# Copy the corresponding .ass file
subtitle_file = os.path.join(folder, file.replace('.mkv', '.ass'))
if os.path.exists(subtitle_file):
output_subtitle = os.path.join(output_path, os.path.relpath(subtitle_file, root_folder))
output_subtitle_dir = os.path.dirname(output_subtitle)
if not os.path.exists(output_subtitle_dir):
os.makedirs(output_subtitle_dir)
shutil.copy(subtitle_file, output_subtitle)
print("Conversion and copying completed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment