From 3a223888ac06deeb30cc4e0c9e424ae776adee39 Mon Sep 17 00:00:00 2001 From: Gabe Farrell Date: Thu, 20 Mar 2025 09:30:17 +0000 Subject: [PATCH] more scripts --- .gitignore | 1 + TODO.md | 0 add_track_statistics.sh | 2 ++ encode_x265_crf18_aac_192k.sh | 30 ++++++++++++++++ missing_fonts_cleaner.py | 0 mux_replace_subtitles.sh | 37 ++++++++++++++++++++ mux_subtitles.sh | 27 +++++++++++++++ replace_nfo.py | 65 +++++++++++++++++++++++++++++++++++ set_default_sub.sh | 43 +++++++++++++++++++++++ 9 files changed, 205 insertions(+) create mode 100644 .gitignore mode change 100644 => 100755 TODO.md create mode 100755 add_track_statistics.sh create mode 100755 encode_x265_crf18_aac_192k.sh mode change 100644 => 100755 missing_fonts_cleaner.py create mode 100755 mux_replace_subtitles.sh create mode 100755 mux_subtitles.sh create mode 100755 replace_nfo.py create mode 100755 set_default_sub.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9be002e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +my-venv diff --git a/TODO.md b/TODO.md old mode 100644 new mode 100755 diff --git a/add_track_statistics.sh b/add_track_statistics.sh new file mode 100755 index 0000000..94eb76f --- /dev/null +++ b/add_track_statistics.sh @@ -0,0 +1,2 @@ +#!/bin/bash +for file in *.mkv; do mkvpropedit "$file" --add-track-statistics-tags; done diff --git a/encode_x265_crf18_aac_192k.sh b/encode_x265_crf18_aac_192k.sh new file mode 100755 index 0000000..d0d2c21 --- /dev/null +++ b/encode_x265_crf18_aac_192k.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Check if argument is provided +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + +# Function to process a single file +process_file() { + local input_file="$1" + local output_file="${input_file%.*}_converted.mkv" + echo "Processing: $input_file -> $output_file" + ffmpeg -i "$input_file" -map 0 -pix_fmt yuv420p10le -c:v libx265 -preset slow -crf 18 -c:a aac -b:a 192k -c:s copy -x265-params profile=main10 "$output_file" +} + +# If argument is a file, process it +if [ -f "$1" ]; then + process_file "$1" +elif [ -d "$1" ]; then + # If argument is a directory, process all .mkv files in it + for file in "$1"/*.mkv; do + [ -e "$file" ] || continue # Skip if no matching files + process_file "$file" + done +else + echo "Error: $1 is not a valid file or directory." + exit 1 +fi + diff --git a/missing_fonts_cleaner.py b/missing_fonts_cleaner.py old mode 100644 new mode 100755 diff --git a/mux_replace_subtitles.sh b/mux_replace_subtitles.sh new file mode 100755 index 0000000..91c6edb --- /dev/null +++ b/mux_replace_subtitles.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# Check if mkvmerge is installed +if ! command -v mkvmerge &> /dev/null; then + echo "Error: mkvmerge not found. Please install mkvtoolnix." + exit 1 +fi + +# Process each .mkv file in the current directory +for mkv in *.mkv; do + # Skip if no .mkv files exist + [[ -e "$mkv" ]] || continue + + base_name="${mkv%.mkv}" + + # Unset all subtitle tracks in the MKV file before adding new ones + echo "Executing command: mkvmerge -o temp.mkv --no-subtitles \"$mkv\"" + if mkvmerge -o temp.mkv --no-subtitles "$mkv"; then + echo "Subtitle tracks unset successfully." + + # Mux the new subtitle files with the language "eng" + echo "Executing command: mkvmerge -o out.mkv \"temp.mkv\" --language 0:eng \"${base_name}\"*.srt" + if mkvmerge -o out.mkv "temp.mkv" --language 0:eng "${base_name}"*.srt; then + echo "Muxing successful, replacing original file and deleting subtitles." + mv out.mkv "$mkv" + rm -f "${base_name}"*.srt + else + echo "Muxing failed, keeping original files." + fi + + # Clean up temporary file + rm -f temp.mkv + else + echo "Failed to unset subtitles, keeping original files." + fi +done + diff --git a/mux_subtitles.sh b/mux_subtitles.sh new file mode 100755 index 0000000..884df79 --- /dev/null +++ b/mux_subtitles.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Check if mkvmerge is installed +if ! command -v mkvmerge &> /dev/null; then + echo "Error: mkvmerge not found. Please install mkvtoolnix." + exit 1 +fi + +# Process each .mkv file in the current directory +for mkv in *.mkv; do + # Skip if no .mkv files exist + [[ -e "$mkv" ]] || continue + + base_name="${mkv%.mkv}" + + # Execute the mkvmerge command with a simplified approach + echo "Executing command: mkvmerge -o out.mkv \"$mkv\" \"${base_name}\"*.srt" + if mkvmerge -o out.mkv "$mkv" "${base_name}"*.srt; then + echo "Muxing successful, replacing original file and deleting subtitles." + mv out.mkv "$mkv" + rm -f "${base_name}"*.srt + else + echo "Muxing failed, keeping original files." + fi + +done + diff --git a/replace_nfo.py b/replace_nfo.py new file mode 100755 index 0000000..4ad0c32 --- /dev/null +++ b/replace_nfo.py @@ -0,0 +1,65 @@ +import os +import sys +import glob +from lxml import etree + +def parse_xml(file_path): + """Parse XML content from a file and return an ElementTree object.""" + try: + with open(file_path, "r", encoding="utf-8") as file: + return etree.parse(file) + except etree.XMLSyntaxError: + print(f"Error parsing XML in {file_path}") + return None + +def update_target_file(source_tree, target_tree): + """Update XML properties in the target file only if they exist in the source file.""" + source_root = source_tree.getroot() + target_root = target_tree.getroot() + + source_elements = {elem.tag: elem for elem in source_root} + + for target_elem in target_root: + if target_elem.tag in source_elements: + target_elem.text = source_elements[target_elem.tag].text + +def process_files(source_dir, target_dir): + """Process all .nfo files in source_dir and update matching files in target_dir.""" + source_files = glob.glob(os.path.join(source_dir, "*.nfo")) + target_files = glob.glob(os.path.join(target_dir, "*.nfo")) + + for src_file in source_files: + src_filename_base = os.path.splitext(os.path.basename(src_file))[0] # Remove .nfo extension + matching_targets = [tgt for tgt in target_files if src_filename_base in os.path.splitext(os.path.basename(tgt))[0]] + + if not matching_targets: + print(f"No matching target file found for: {src_filename_base}") + continue + + source_tree = parse_xml(src_file) + if source_tree is None: + continue + + for tgt_file in matching_targets: + target_tree = parse_xml(tgt_file) + if target_tree is None: + continue + + update_target_file(source_tree, target_tree) + + # Save the updated target file + with open(tgt_file, "wb") as f: + target_tree.write(f, encoding="utf-8", xml_declaration=True) + + print(f"Updated: {tgt_file}") + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python script.py ") + sys.exit(1) + + source_directory = sys.argv[1] + target_directory = sys.argv[2] + + process_files(source_directory, target_directory) + diff --git a/set_default_sub.sh b/set_default_sub.sh new file mode 100755 index 0000000..0a96c37 --- /dev/null +++ b/set_default_sub.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# Check if mkvpropedit is installed +if ! command -v mkvpropedit &> /dev/null; then + echo "Error: mkvpropedit not found. Please install mkvtoolnix." + exit 1 +fi + +# Ensure correct number of arguments +if [ "$#" -ne 2 ]; then + echo "Usage: $0 " + exit 1 +fi + +mkv_file="$1" +default_track="$2" + +# Ensure the file exists +if [[ ! -f "$mkv_file" ]]; then + echo "Error: File '$mkv_file' not found." + exit 1 +fi + +# Get subtitle track IDs +subtitle_tracks=$(mkvmerge -i "$mkv_file" | grep 'subtitles' | awk -F ': ' '{print $1}' | awk '{print $3}') + +if [[ -z "$subtitle_tracks" ]]; then + echo "No subtitle tracks found in '$mkv_file'." + exit 1 +fi + +# Remove default and forced flags from all subtitle tracks +for track_id in $subtitle_tracks; do + echo "Running: mkvpropedit \"$mkv_file\" --edit track:$track_id --set flag-default=0 --set flag-forced=0" + mkvpropedit "$mkv_file" --edit track:$track_id --set flag-default=0 --set flag-forced=0 +done + +# Set the specified track as default +echo "Running: mkvpropedit \"$mkv_file\" --edit track:$default_track --set flag-default=1" +mkvpropedit "$mkv_file" --edit track:$default_track --set flag-default=1 + +echo "Subtitle track $default_track set as default in '$mkv_file'." +