Files
dotfiles/my-scripts/shell/ffmpeg_helper.sh
2023-09-24 17:44:23 -03:00

129 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
show_help() {
echo "Usage: $0 [--input|-i input_file] [--crop|-c width:height] [--trim-start start] [--trim-end end] [--crf crf_value] [--fps new_fps] [--scale640] [--merge-audio] [--remove-audio] --output|-o output_file"
echo "Options:"
echo " --input|-i Specify the input video file (required)."
echo " --crop|-c Specify the width:height for video cropping."
echo " --trim-start Specify the start timestamp for video trimming."
echo " --trim-end Specify the end timestamp for video trimming."
echo " --crf Specify the CRF value for video compressing."
echo " --fps Specify the new frames per second for the video."
echo " --scale640 Scale the video to 640x320."
echo " --merge-audio Merge all audio tracks into the main track."
echo " --remove-audio Suppress all audio and remove audio tracks."
echo " --output|-o Specify the output filename (required)."
}
remove_audio() {
input_file="$1"
output_file="$2"
ffmpeg -i "$input_file" -an "$output_file"
}
if ! command -v ffmpeg &> /dev/null; then
echo "Error: 'ffmpeg' command not found. Please install ffmpeg."
exit 1
fi
input_file=""
crop=""
trim_start=""
trim_end=""
crf=""
new_fps=""
scale640=false
merge_audio=false
remove_audio=false
output_file=""
while [ "$#" -gt 0 ]; do
case "$1" in
--help|-h) show_help; exit ;;
--input|-i) input_file="$2"; shift 2 ;;
--crop|-c) crop="$2"; shift 2 ;;
--trim-start) trim_start="$2"; shift 2 ;;
--trim-end) trim_end="$2"; shift 2 ;;
--crf) crf="$2"; shift 2 ;;
--fps) new_fps="$2"; shift 2 ;;
--scale640) scale640=true; shift ;;
--merge-audio) merge_audio=true; shift ;;
--remove-audio) remove_audio=true; shift;;
--output|-o) output_file="$2"; shift 2 ;;
*) shift ;;
esac
done
if [ -z "$input_file" ]; then
echo "Error: Input filename (--input or -i) is required."
exit 1
fi
if [ -z "$output_file" ]; then
echo "Error: Output filename (--output or -o) is required."
exit 1
fi
if [ -z "$crop" ] && [ -z "$trim_start" ] && [ -z "$trim_end" ] && [ -z "$crf" ] && [ -z "$new_fps" ] && [ "$merge_audio" = false ] && [ "$scale640" = false ] && [ "$remove_audio" = false ]; then
echo "Error: At least one optional parameter is required."
exit 1
fi
if [ -n "$trim_start" ] && [ -z "$trim_end" ]; then
echo "Error: If using --trim-start, you must also specify --trim-end."
exit 1
fi
if [ -n "$trim_end" ] && [ -z "$trim_start" ]; then
echo "Error: If using --trim-end, you must also specify --trim-start."
exit 1
fi
if [ -n "$crop" ] && [ "$scale640" = true ]; then
echo "Error: Cannot use both --crop and --scale640 together."
exit 1
fi
if [ "$merge_audio" = true ] && [ "$remove_audio" = true ]; then
echo "Error: Cannot use both --merge-audio and --remove-audio together."
exit 1
fi
ffmpeg_command="ffmpeg -i \"$input_file\""
if [ "$remove_audio" = true ]; then
ffmpeg_command+=" -an"
fi
if [ -n "$crop" ]; then
ffmpeg_command+=" -vf crop=$crop"
fi
if [ -n "$trim_start" ] && [ -n "$trim_end" ]; then
ffmpeg_command+=" -ss $trim_start -to $trim_end"
fi
if [ -n "$crf" ]; then
ffmpeg_command+=" -c:v libx264 -crf $crf"
fi
if [ -n "$new_fps" ]; then
ffmpeg_command+=" -r $new_fps"
fi
if [ "$scale640" = true ]; then
ffmpeg_command+=" -vf scale=640:320"
fi
if [ "$merge_audio" = true ]; then
num_audio_streams=$(ffprobe -loglevel error -select_streams a -show_entries stream=codec_type -of csv=p=0 "$input_file" | wc -l)
ffmpeg_command+=" -filter_complex amerge=inputs=$num_audio_streams"
fi
ffmpeg_command+=" \"$output_file\""
echo "Running ffmpeg command:"
echo "$ffmpeg_command"
eval "$ffmpeg_command"