1
0
Fork 0

Add cutvid function

This commit is contained in:
tastytea 2022-05-07 17:26:51 +02:00
parent 4ee486ccfc
commit a6aa229368
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,43 @@
#compdef cutvid
function _cutvid_vencoders()
{
local -a encoders=(
copy
vp9_vaapi
libvpx-vp9
hevc_vaapi
libx265
h264_vaapi
libx264
)
_values "video encoder" ${encoders}
}
function _cutvid_aencoders()
{
local -a encoders=(
copy
libopus
flac
libvorbis
aac
mp3
)
_values "audio encoder" ${encoders}
}
_arguments '-h[short help text]' \
'--help[short help text]' \
'--from[timestamp]:language:' \
'--to[timestamp]:language:' \
'--vencoder[video encoder]:encoder:_cutvid_vencoders' \
'--aencoder[audio encoder]:encoder:_cutvid_aencoders' \
'--hardcode-subs[hardcode ASS subtitles]' \
'1:input file:_files' \
'2:output file:_files' \
'*:ffmpeg options:_ffmpeg'
# Local Variables:
# mode: shell-script
# End:

52
.config/zsh/functions/cutvid Executable file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env zsh
# Cut a clip from a video
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
zmodload zsh/zutil
local -a o_help=()
local -a o_from=()
local -a o_to=()
local -a o_vencoder=()
local -a o_aencoder=()
local -a o_hardcode_subs=()
zparseopts -D -K -- h=o_help -help=o_help -from:=o_from -to:=o_to \
-vencoder:=o_vencoder -aencoder:=o_aencoder \
-hardcode-subs=o_hardcode_subs
if [[ ${#o_help} -ne 0 || ! -v 2 || ${#o_from} -ne 2 || ${#o_to} -ne 2 ]]; then
local ret=$(( ${#o_help} ^ 1 ))
print -u $(( 1 + ${ret} )) "usage: ${0} [-h|--help]" \
"--from <HH:MM:SS.MMM> --to <HH:MM:SS.MMM>" \
"[--vencoder <ENCODER>] [--aencoder <ENCODER>]" \
"<INPUT FILE> <OUTPUT FILE> [FFMPEG OPTIONS]"
print -l "\e[3mYou can set bitrates with -b:v and -b:a in FFMPEG OPTIONS" \
"Visit <https://trac.ffmpeg.org/wiki#Encoding> for tips\e[0m"
return ${ret}
fi
local input=${1}
shift
local output=${1}
shift
local ffmpeg_options=(${@})
local from=${o_from[2]}
local to=${o_to[2]}
local vencoder="copy"
local aencoder="copy"
[[ ${#o_vencoder} -eq 2 ]] && vencoder=${o_vencoder[2]}
[[ ${#o_aencoder} -eq 2 ]] && aencoder=${o_aencoder[2]}
ffmpeg -i ${input} \
-ss ${from} -to ${to} \
-codec:v ${vencoder} -codec:a ${aencoder} -codec:s copy \
${ffmpeg_options} ${output}
# NOTE: only works with ASS at the moment
if [[ ${#o_hardcode_subs} -ne 0 ]]; then
local tmpass=$(mktemp --suffix='.cutvid.ass')
ffmpeg -i ${output} -y ${tmpass}
ffmpeg -sn -i ${output} -vf ass=${tmpass} \
-codec:v ${vencoder} -codec:a copy \
hardcoded_sub_${output}
mv --verbose hardcoded_sub_${output} ${output}
rm --verbose ${tmpass}
fi