Last active
February 6, 2025 17:59
-
-
Save Akczht/836edd4744ce1dc1e4416594da8d6a5e to your computer and use it in GitHub Desktop.
A script to run ffmpeg on all the files in a folder and remux videos also you can add other files in the sec folder
This file contains hidden or 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 | |
set -e | |
# Set input and output file extensions and additional ffmpeg arguments here | |
input_ext="mkv" # Input file extension (e.g., mp4, mkv) without the dot | |
output_ext="mkv" # Output file extension (e.g., mp4, mkv) without the dot | |
# Create the output directory if it doesn't exist | |
output_dir="$PWD/dir" | |
mkdir -p "$output_dir" | |
# Get the list of input files in the current directory | |
input_dir="$PWD" | |
sec_dir="$PWD/sec" | |
# Get a list of files with the specified input extension in the input directory | |
input_files=("$input_dir"/*."$input_ext") | |
# Check if the sec directory exists and get the list of files in the sec directory if it does | |
if [ -d "$sec_dir" ]; then | |
sec_files=("$sec_dir"/*."$input_ext") | |
else | |
echo "Warning: The '$sec_dir' directory does not exist. Only input files will be processed." | |
sec_files=() # If sec doesn't exist, set sec_files to an empty array | |
fi | |
# Check if the number of input files match with sec files (if sec directory exists) | |
if [ ${#sec_files[@]} -gt 0 ] && [ ${#input_files[@]} -ne ${#sec_files[@]} ]; then | |
echo "Error: The number of files in $input_dir and $sec_dir do not match." | |
exit 1 | |
fi | |
# Process each input file | |
for ((i=0; i<${#input_files[@]}; i++)); do | |
input_file="${input_files[$i]}" | |
# If sec directory exists, use the corresponding sec file | |
if [ ${#sec_files[@]} -gt 0 ]; then | |
sec_file="${sec_files[$i]}" | |
else | |
sec_file="" # No corresponding sec file if sec doesn't exist | |
fi | |
# Get the filename without extension for output naming | |
filename=$(basename "$input_file" ."$input_ext") | |
# Example ffmpeg arguments (can be customized) | |
ffmpeg_args=(-c:v copy -c:a copy -c:s copy -map 0:v:m:language:eng -map 0:a:m:language:eng -map 0:s:m:language:eng -metadata title=${filename:0}) | |
# Construct the output file name with the desired extension in the 'dir' folder | |
output_file="$output_dir/$filename.$output_ext" | |
# Run ffmpeg with the chosen arguments | |
if [ -z "$sec_file" ]; then | |
# If sec file doesn't exist, process only the input file | |
echo "Processing: $input_file -> $output_file" | |
ffmpeg -i "$input_file" "${ffmpeg_args[@]}" -y "$output_file" | |
else | |
# If sec file exists, process both input and sec files | |
echo "Processing: $input_file + $sec_file -> $output_file" | |
ffmpeg -i "$input_file" -i "$sec_file" "${ffmpeg_args[@]}" -y "$output_file" | |
fi | |
done | |
echo "Processing complete. Output files are in '$output_dir'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment