Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/usr/bin/env sh
- #check if argument for input file is set; fail if not
- if [ ! -z ${1+x} ]
- then
- INPUT_FILE="$1"
- #if second argument is given, use as output file name; use default if not
- if [ ! -z ${2+x} ]
- then
- OUTPUT_FILE="$2"
- else
- OUTPUT_FILE="output.webm"
- fi
- #make temporary directories
- TMP_DIR=$(mktemp -d)
- mkdir ${TMP_DIR}/{frames,scaled}
- #extract individual frames from inputfile
- ffmpeg -v error -i "${INPUT_FILE}" -qmin 1 -q:v 1 "${TMP_DIR}/frames/%04d.jpeg"
- #count frames
- FRAMES=$(ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -count_frames -show_entries stream=nb_read_frames -i "${INPUT_FILE}")
- printf 'Frame count: %s\n' ${FRAMES}
- #get framerate from input file; used for intermediary files, can't be changed during concat
- FPS=$(ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate -i "${INPUT_FILE}")
- printf 'Frame rate: %s\n' ${FPS}
- #scale each frame according to its index and transcode to webm again
- #this is EXTREMELY inefficient; the overhead of restarting ffmpeg possibly thousands of times means that even on a powerful machine this script will take ages to run
- for f in $(find ${TMP_DIR}/frames -name '*.jpeg' | sed -En 's#^.*/?frames/([0-9]+).jpeg$#\1#p' | sort); do ffmpeg -v error -i "${TMP_DIR}/frames/${f}.jpeg" -frames:v 1 -filter:v "scale=$(printf '%s + 10\n' ${f} | bc):-1" -c:v libvpx -crf 31 -r "${FPS}" "${TMP_DIR}/scaled/${f}.webm"; printf 'Progress: %s/%s [%s%%] \r' ${f} ${FRAMES} $(printf 'scale=2;%s*100/%s\n' ${f} ${FRAMES} | bc); done; printf '\n'
- #concatenate all intermediary files; try to map audio from input file; if input file has no audio rerun without mapping it
- ffmpeg -thread_queue_size ${FRAMES} -f concat -safe 0 -i <(find ${TMP_DIR}/scaled/ -name '*.webm' -printf "file '%p'\n" | sort) -i "${INPUT_FILE}" -c:v copy -c:a libopus -map "0:v" -map "1:a" ${OUTPUT_FILE} || ffmpeg -thread_queue_size ${FRAMES} -f concat -safe 0 -i <(find ${TMP_DIR}/scaled/ -name '*.webm' -printf "file '%p'\n" | sort) -c:v copy ${OUTPUT_FILE}
- #remove intermediary files
- rm -r "${TMP_DIR}"
- #exit normaly
- exit 0
- else
- #exit with error
- printf 'Usage: %s [INPUT FILE] {OUTPUT FILE}\n' $0
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement