Created
April 26, 2024 00:03
-
-
Save Meettya/8a21eb6503a7bd42831905cf134fbbde to your computer and use it in GitHub Desktop.
MKV repacker for ffmpeg docker
This file contains 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
#!/bin/bash | |
# Function to display usage information | |
usage() { | |
echo "Usage: $0 [--extension <file_extension>] --audio <audio_directory> --subtitle <subtitle_directory> <source_directory> <destination_root_directory>" | |
exit 1 | |
} | |
# Function to check write permission | |
check_write_permission() { | |
local directory="$1" | |
if [ ! -w "$directory" ]; then | |
echo "Error: Destination directory '$directory' is not writable." | |
exit 1 | |
fi | |
} | |
# Default extension if not provided | |
extension=".mkv" | |
# Parse command line options | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--extension) | |
shift | |
extension=".$1" | |
shift | |
;; | |
--audio) | |
shift | |
audio_directory="$1" | |
shift | |
;; | |
--subtitle) | |
shift | |
subtitle_directory="$1" | |
shift | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
# Check if all required options are provided | |
if [[ -z $audio_directory || -z $subtitle_directory || "$#" -ne 2 ]]; then | |
usage | |
fi | |
# Source directory containing the files | |
source_directory="$1" | |
# Destination directory for processed files | |
destination_directory="$2" | |
# Check if destination directory is writable | |
check_write_permission "$destination_directory" | |
# Get the permissions of the parent directory | |
parent_permissions=$(stat -c "%a" "$destination_directory") | |
# Get the last part of the source directory name | |
source_dir_name=$(basename -- "$source_directory") | |
# Check if the destination directory contains the last part of the source directory name | |
if [[ ! "$destination_directory" == *"$source_dir_name"* ]]; then | |
destination_directory="$destination_directory/$source_dir_name" | |
fi | |
# Create destination directory if it doesn't exist | |
mkdir -m "$parent_permissions" -p "$destination_directory" | |
# Function to auto-detect file extension in a directory | |
detect_extension() { | |
local directory="$1" | |
local extension="" | |
# Loop through each file in the directory | |
for file in "$directory"/*; do | |
# Check if file exists and is a regular file | |
if [ -f "$file" ]; then | |
# Extract file extension, as all after FIRST dot | |
extension=$(basename "$file" | cut -d. -f2-) | |
# Check if extension is not empty | |
if [ -n "$extension" ]; then | |
break # Exit loop once first non-empty extension is found | |
fi | |
fi | |
done | |
echo ".$extension" | |
} | |
# Detect extensions for audio and subtitle directories | |
audio_extension=$(detect_extension "$source_directory/$audio_directory") | |
subtitle_extension=$(detect_extension "$source_directory/$subtitle_directory") | |
# Loop through each file in the source directory with the specified extension | |
for file in "$source_directory"/*"$extension"; do | |
# Check if file exists | |
if [ -f "$file" ]; then | |
# Extract filename without extension | |
filename=$(basename -- "$file") | |
filename_no_ext="${filename%.*}" | |
# Your ffmpeg command using the current file | |
ffmpeg -i "$file"\ | |
-i "$source_directory/$audio_directory/$filename_no_ext$audio_extension"\ | |
-i "$source_directory/$subtitle_directory/$filename_no_ext$subtitle_extension"\ | |
-map 0:v -map 0:a -map 1:a -map 2:s\ | |
-codec copy -disposition:a:0 none -disposition:a:1 default\ | |
"$destination_directory/$filename_no_ext$extension" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage examples -
./repack.sh --audio 'Studio Band' --subtitle SUB /data/'Ore dake Level Up na Ken' /xdata
./repack.sh --audio 'RUS Sound'/'[StudioBand]' --subtitle 'RUS Subs'/'[Wakanim]' /data/'[ReinForce] Tatoeba Last Dungeon (BDRip 1920x1080 x264 FLAC)' /xdata
Script can proceed audio or subtitle file naming with addons, f.e. if main file named 'foo.mkv' than audio track may has name 'foo.baz.mka'
Important notes - as destination script use destination ROOT, where NEW identical directory will be created for processed files.