dotfiles/.config/zsh/functions/cutvid

61 lines
2.1 KiB
Plaintext
Raw Normal View History

2022-05-07 17:26:51 +02:00
#!/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
2022-05-07 17:26:51 +02:00
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>]" \
"[--hardcode-subs <NUM>] <INPUT FILE> <OUTPUT FILE> [FFMPEG OPTIONS]"
2023-09-12 16:48:25 +02:00
print "\e[3myou can set bitrates with -b:v and -b:a in FFMPEG OPTIONS." \
"the -vn / -an / -sn / -dn options can be used to skip inclusion of" \
2023-01-26 09:51:17 +01:00
"video, audio, subtitle and data streams respectively." \
2023-09-12 16:48:25 +02:00
"use -s WIDTHxHEIGHT to scale." \
"for x264, use -crf 18 and -tune {animation,film,…}"
2022-08-13 02:49:54 +02:00
print "Visit <https://trac.ffmpeg.org/wiki#Encoding> for tips\e[0m"
2022-05-07 17:26:51 +02:00
return ${ret}
fi
2022-05-07 17:26:51 +02:00
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]}
local subnumber=0
[[ ${#o_hardcode_subs} -eq 2 ]] && subnumber=${o_hardcode_subs[2]}
local subtitle_options=(-codec:s copy)
local compat_options=(-pix_fmt yuv420p) # Android doesn't like yuv420p10le
2023-08-15 21:12:21 +02:00
# extract subtitles
if [[ ${#o_hardcode_subs} -ne 0 ]]; then
local tmpass=$(mktemp --suffix='.cutvid.ass')
ffmpeg -i ${input} -map 0:s:${subnumber} -y ${tmpass}
subtitle_options=(-sn -vf ass=${tmpass} )
fi
2022-05-07 17:26:51 +02:00
ffmpeg -i ${input} \
-ss ${from} -to ${to} \
-codec:v ${vencoder} -codec:a ${aencoder} ${subtitle_options} \
2023-08-15 21:12:21 +02:00
-preset slower \
${compat_options} ${ffmpeg_options} ${output}
2022-05-07 17:26:51 +02:00
[[ ${#o_hardcode_subs} -ne 0 ]] && rm --verbose ${tmpass}