#!/bin/bash show_help() { echo """Usage: $(basename $0) \ [--input|-i input_file] \ [--crop|-c width:height] \ [--trim-start start] \ [--trim-end end] \ [--crf crf_value] \ [--fps new_fps] \ [--scale new_video_resolution] \ [--merge-audio] \ [--remove-audio] \ [--volume-multiplier] \ --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 " --scale Scale the video to a new resolution." echo " --merge-audio Merge all audio tracks into the main track." echo " --remove-audio Suppress all audio and remove audio tracks." echo " --volume-multiplier Changes the video's volume with a multiplier." echo " --audio-track-no Selects a specific audio track for the output" 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="" scale="" merge_audio=false remove_audio=false volume_multiplier="" audio_track_no=1 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 ;; --scale) scale="$2" shift 2 ;; --merge-audio) merge_audio=true shift ;; --remove-audio) remove_audio=true shift ;; --volume-multiplier) volume_multiplier="$2" shift 2 ;; --audio-track-no) audio_track_no="$2" 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 ] && [ -z "$scale" ] && [ "$remove_audio" = false ] && [ -z "$volume_multiplier" ]; 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" ] && [ "$scale" = true ]; then echo "Error: Cannot use both --crop and --scale 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 if [ "$merge_audio" = true ] && [ "$audio_track_no" != 1 ]; then echo "Error: Cannot use both --merge-audio and --audio-track-no together." exit 1 fi if [ "$remove_audio" = true ] && [ "$audio_track_no" != 1 ]; then echo "Error: Cannot use both --remove-audio and --audio-track-no together." exit 1 fi ffmpeg_command="ffmpeg -i \"$input_file\"" # ffmpeg_command="/mnt/e/programs/ffmpeg-2024-08-15-git-1f801dfdb5-full_build/bin/ffmpeg.exe -i \"$input_file\"" # if command -v ffmpeg.exe &> /dev/null; then # ffmpeg_command="ffmpeg.exe -hwaccel d3d11va" # fi 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 libx265 -crf $crf" # ffmpeg_command+=" -c:v hevc_amf -rc cqp -qp_i $crf -qp_p $crf" fi if [ -n "$new_fps" ]; then ffmpeg_command+=" -r $new_fps" fi if [ -n "$scale" ]; then ffmpeg_command+=" -s $scale" fi if [ -n "$volume_multiplier" ]; then ffmpeg_command+=" -filter:a \"volume=$volume_multiplier\"" 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 if [ "$audio_track_no" != 1 ]; then ffmpeg_command+=" -map 0:0 -map 0:$audio_track_no" fi ffmpeg_command+=" \"$output_file\"" echo "Running ffmpeg command:" echo "$ffmpeg_command" eval "$ffmpeg_command"