Skip to content
Snippets Groups Projects
ffmpegsplit 2.52 KiB
Newer Older
file=""
lengths=""
quiet=0
debug=false
version() {
	echo "ffmpegsplit v0.1"
}
display_help() {
cat << EOH
Usage:
$0 [options] -i VIDEO-FILE -t TIMES-FILE

Video file will be split into parts at times specified in times-file

Times file contains times in seconds each on separate line

Empty lines (except for trailing) will confuse this script

(Not implemented options are marked with !)

Options:
-q --quiet	Surpresses ffmpeg output. Put twice to surpress all output
-d --debug	Do not invoke ffmpeg and only show commands to run
-v --version	Shows version information
-h --help	Shows this help message
-i --input	Input video file (required)
-t --times	Input times file (required)

Written by Jakub Skořepa (jakub at skorepa.info) in 2015-4
Distributed under:
GNU General Public License 3 or newer as published by Free Software Foundation
EOH
}
while [ $# -gt 0 ]
do
	key="$1"

	case $key in
		-q|--quiet)
			let quiet++
			;;
		-d|--debug)
			debug=true
			;;
		-v|--version)
			version
			exit
			;;
		-h|--help)
			display_help
			exit
			;;
		-i|--input)
			file="$2"
			shift
			;;
		-t|--times)
			lengths="$2"
			shift
			;;
		*)
			echo "Warning: unknown option $key"
			;;
	esac
	shift
done

if [ -z "$file" ] && [ -z "$lengths" ]
then
	echo "File and times file was not specified"
	echo "See $0 --help for more information"
	exit
fi

if [ -z "$file" ]
then
	echo "File was not specified"
	echo "See $0 --help for more information"
	exit
fi

if [ -z "$lengths" ]
then
	echo "Times file was not specified"
	echo "See $0 --help for more information"
	exit
fi
echo "ffmpeg -i \"$file\" -to "$(sed "1q;d" $lengths)" 1.mp4"
if [ ! $debug ]
then
	if [ $quiet -ge 1 ]
	then
		ffmpeg -i "$file" -to $(sed "1q;d" $lengths) 1.mp4 > /dev/null 2>&1
	else
		ffmpeg -i "$file" -to $(sed "1q;d" $lengths) 1.mp4
	fi
fi
N=$(cat "$lengths" | wc -l)
Nm=$[N-1]
for I in $(eval echo "{1..$Nm}")
do
	t=$(sed $I"q;d" "$lengths") # start time
	t2=$(sed $[I+1]"q;d" "$lengths") # end time
	t0=$[t-10] # start time - 10
	t1=$[t2-t0] # length of segment + 10
	echo "ffmpeg -ss $t0 -i \"$file\" -ss 10 -to $t1 $I.mp4"
	if [ ! $debug ]
	then
		if [ $quiet -ge 1 ]
		then
			ffmpeg -ss $t0 -i "$file" -ss 10 -to $t1 "$I"".mp4" > /dev/null 2>&1
		else
			ffmpeg -ss $t0 -i "$file" -ss 10 -to $t1 "$I"".mp4"
		fi
	fi
t=$(sed "$N""q;d" "$lengths")
echo "ffmpeg -ss "$[t-10]" -i \"$file\" -ss 10 $N.mp4"
if [ ! debug ]
then
	if [ $quiet -ge 1 ]
	then
		ffmpeg -ss $[t-10] -i "$file" -ss 10 "$N"".mp4" > /dev/null 2>&1
	else
		ffmpeg -ss $[t-10] -i "$file" -ss 10 "$N"".mp4"
	fi
fi