#!/bin/bash # split2components.sh ################################################################################ # split2components.sh - Script for splitting video files into components ################################################################################ # This script processes video files, extracts video streams to MKV format, # extracts subtitles to SRT format, and converts audio streams to separate FLAC files. # It supports various video file formats like MP4, MKV, AVI, FLV, and MOV. # The script requires ffmpeg and pv to be installed on the system. # # Usage: ./split2components.sh # - : The path where the files will be processed and components stored. # # Dependencies: # - ffmpeg: A multimedia processing tool (https://ffmpeg.org/) # - pv: A terminal-based tool for processing data (https://www.ivarch.com/programs/pv.shtml) # # Note: Make sure to install the required dependencies before using this script. ################################################################################ # Check if ffmpeg and pv are installed if ! command -v ffmpeg &> /dev/null || ! command -v pv &> /dev/null then echo "Error: ffmpeg or pv is not installed. Please install them first." exit fi # Check if a target path is provided if [ "$#" -eq 0 ] then echo "Error: Please provide the target path where the files will be processed." exit fi target_path="$1" # Check if the target path exists if [ ! -d "$target_path" ] then echo "Error: The specified target path does not exist." exit fi # Create the required directories mkdir -p "$target_path/mkv" "$target_path/flac" "$target_path/srt" # Create a temporary directory for subtitle files temp_dir="temp_subs" mkdir -p "$temp_dir" || { echo "Error creating $temp_dir"; exit 1; } # Loop through all supported video file formats in the specified directory find "$target_path" -type f \( -iname \*.mp4 -o -iname \*.mkv -o -iname \*.avi -o -iname \*.flv -o -iname \*.mov \) -print0 | while IFS= read -r -d '' video_file do # Check if there is a suitable video file if [ -e "$video_file" ] then # Extract the filename without extension filename_no_ext=$(basename -- "$video_file" | cut -f 1 -d '.') new_video_file="$target_path/mkv/$filename_no_ext.mkv" srt_subtitle_file="$target_path/srt/$filename_no_ext.srt" # Convert video to MKV format ffmpeg -fflags +genpts -i "$video_file" -c:v copy -pix_fmt yuv420p -an -sn -fflags +genpts -avoid_negative_ts make_zero "$new_video_file" -y < /dev/null # Extract subtitles to SRT format (if available) subtitle_file="$temp_dir/$filename_no_ext.srt" ffmpeg -i "$video_file" -map 0:s:0 "$subtitle_file" # Convert subtitles to SRT format if available if [ -e "$subtitle_file" ] then mv "$subtitle_file" "$srt_subtitle_file" fi # Loop through all audio streams and convert them to separate FLAC files audio_stream_index=0 while true do flac_audio_file="$target_path/flac/${filename_no_ext}_audio$audio_stream_index.flac" if ! ffmpeg -i "$video_file" -map 0:a:"$audio_stream_index" -c:a flac -y "$flac_audio_file" < /dev/null then break fi echo "Audio Stream $audio_stream_index converted." ((audio_stream_index++)) done echo "Processing of $filename_no_ext completed." fi done # Delete temporary directory rm -r "$temp_dir"