#! /bin/zsh # # this file: # https://dataswamp.org/~incal/conf/.zsh/video-slice ################### ## slice and dice ## ## video stuff :) ## ################### video-part () { local beg=$1 local end=$2 local src=$3 local dst=${4:-${src:r}-part.${src:e}} ffmpeg \ -y \ -v quiet \ -i $src \ -ss $beg \ -to $end \ -c copy \ $dst } # https://unix.stackexchange.com/a/212518 split-video () { local fs=($@) local s=1 local f local src local name local ext for f in $fs; do src=$f name=${src:r} ext=${src:e} ffmpeg \ -i $src \ -c copy \ -map 0 \ -segment_time 00:00:${s} \ -f segment \ -reset_timestamps 1 \ ${name}-%03d.${ext} done } # https://video.stackexchange.com/questions/4563/how-can-i-crop-a-video-with-ffmpeg video-crop () { local x=$1 local y=$2 local w=$3 local h=$4 local src=$5 local name=${src:r} local ext=${src:e} local dst=${name}-crop.${ext} ffmpeg \ -y \ -i $src \ -filter:v "crop=${w}:${h}:${x}:${y}" \ -c:a copy \ $dst } video-remove-border () { local border=$1 local src=$2 local x_border=$(( 2 * $border )) local y_border=$x_border local name=${src:r} local ext=${src:e} local dst=${name}-no-border.${ext} ffmpeg \ -y \ -i $src \ -vf "crop=in_w-${x_border}:in_h-${y_border}" \ -c:a copy \ $dst } video-add-border () { local src=$1 local w=${2:-1080} local h=${3:-1920} local name=${src:r} local ext=${src:e} local dst=${name}-border.${ext} local ar="${w}:${h}" ffmpeg \ -y \ -i $src \ -vf "scale=${ar}:force_original_aspect_ratio=decrease,pad=${ar}:(ow-iw)/2:(oh-ih)/2,setsar=1" \ $dst } video-merge () { local src1=$1 local src2=$2 local name=${src1:r}-${src2:r} local ext=${src1:e} local dst=${name}.${ext} local list_file=both.txt echo "file $src1" > $list_file echo "file $src2" >> $list_file ffmpeg \ -y \ -f concat \ -safe 0 \ -i $list_file \ -c copy $dst } slice-video () { # usage: # slice-video FILE [BEG_SEC=-2] LEN_SEC local src=$1 local name=${src:r} local ext=${src:e} local dst=${name}-sliced.${ext} local beg local len case $# in (2) beg=-2 len=$2 ;; (3) beg=$2 len=$3 ;; (*) wrong-number-of-arguments-message $(( $# - 1 )) "1 or 2" return ;; esac ffmpeg \ -y \ -v quiet \ -i $src \ -ss $beg \ -t $len \ -c:a copy \ -c:v copy \ $dst len $dst } video-drop-after () { local after=$1 local f=$2 slice-video $f $after }