Beijar Crueza Coisa

.oOo.

clips de filmes com ffmpeg

tenho um script “,clip-video” sempre disponível para fazer pequenos clips de vídeos utilzando o ffmpeg. Tal como está, exporta os clips para 720p e 30fps, sem áudio.

#!/usr/bin/env bash

validate () {
	local fn="$1"
	local ts="$2"
	local te="$3"
	local time_pattern='^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$'

	if ! [[ -e "$fn" ]]; then
		>&2 echo "ERROR - file $fn not found"
		return 1
	fi

	if ! [[ "$ts" =~ $time_pattern ]]; then
		>&2 echo "ERROR - time_start $ts not in format 00:00:00"
		return 1
	fi

	if ! [[ "$te" =~ $time_pattern ]]; then
		>&2 echo "ERROR - time_end $te not in format 00:00:00"
		return 1
	fi

	return 0
}

clip () {
	local filename directory extension path te ts
	path="$1"
	filename="${path##*/}"
	filename="${filename%.*}"
	
	ts="${2//:}"
	te="${3//:}"

	if [[ "$path" == */* ]]; then
		directory="${path%/*}"
	else
		directory="."
	fi
	extension="${path##*.}"
	ffmpeg -i "$path" -ss "$2" -to "$3" -vf 'scale=-2:720,fps=30' \
		-c:v libx264 -crf 23 -preset fast -an \
		"$directory/$filename-clip-$ts-$te.$extension"
}

usage() {
	echo
	echo "Usage:"
	echo
	echo "	,clip-video videofile time_start time_end"
	echo
	echo "		* time_start and time_end in format 00:00:00"
	echo "		* videofile can be a full path"
	echo "		* it will use ffmpeg to create a new file with the"
	echo "		* the text '-clip-time_start-time-end' appended to filename"
	echo "		  so clips are easy to find with a ls **/*clip*"
	echo
	exit 1
}


if [[ $# != 3 ]]; then
	>&2 echo "ERROR - Number of arguments is not 3"
	usage
fi

if validate "$1" "$2" "$3"; then
	clip "$1" "$2" "$3"
else
	usage
fi

.oOo.