This repository has no description
1#!/bin/sh
2# TODO: Add hardware acceleration support
3# TODO: More consistently find a way to delete ffmpeg log files
4usage() {
5 printf '%b\n' "${0##*/} -i <input_file> [OPTIONS]" \
6 "\noptions:" \
7 "\t-c CODEC - video codec to use (default: libx264)" \
8 "\t-h - display this message" \
9 "\t-o FILE - output file (default: out.ext)" \
10 "\t-p PRESET - encoding preset to use" \
11 "\t-t FILE_SIZE - file size to target in MiB (default: 50)"
12}
13
14while [ "$*" ]
15do
16 case $1 in
17 - ) shift; continue ;;
18 -- ) shift; break ;;
19 -* ) flag=${1#-}; shift ;;
20 * ) shift; continue ;;
21 esac
22
23 while [ "$flag" ]
24 do
25 arg=${flag%"${flag#?}"}
26
27 case $arg in
28 c ) VIDCOMP_CODEC=$1; shift ;;
29 h ) usage; exit 0 ;;
30 i ) VIDCOMP_INPUT_FILE=$1; shift ;;
31 o ) VIDCOMP_OUTPUT_FILE=$1; shift ;;
32 p ) VIDCOMP_PRESET=$1; shift ;;
33 t ) VIDCOMP_TARGET_SIZE=$1; shift ;;
34 * ) printf '%s\n' "${0##*/}: -$arg: invalid argument" 1>&2
35 usage 1>&2; exit 1 ;;
36 esac
37
38 flag=${flag#?}
39 done
40done
41
42[ -z "$VIDCOMP_INPUT_FILE" ] && {
43 printf '%s\n' "${0##*/}: missing input video" 1>&2
44 usage 1>&2; exit 1
45}
46
47ext="${VIDCOMP_INPUT_FILE##.*}"
48: "${VIDCOMP_OUTPUT_FILE:=out.${ext}}"
49
50: "${VIDCOMP_CODEC:=libx264}"
51: "${VIDCOMP_TARGET_SIZE:=50}"
52
53a_bitrate="$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 \
54 "${VIDCOMP_INPUT_FILE}")"
55secs="$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${VIDCOMP_INPUT_FILE}")"
56bitrate="$(fend floor \("${VIDCOMP_TARGET_SIZE} * 8388.608 / ${secs}" - \("${a_bitrate}" / 1000\)\))"
57
58
59ffmpeg -hide_banner -loglevel info \
60 -i "${VIDCOMP_INPUT_FILE}" -c:v "${VIDCOMP_CODEC}" -b:v "${bitrate}k" \
61 ${VIDCOMP_PRESET:+"-preset" "$VIDCOMP_PRESET"} -pass 1 \
62 -an -f null -y /dev/null && \
63ffmpeg -hide_banner -loglevel info \
64 -i "${VIDCOMP_INPUT_FILE}" -c:v "${VIDCOMP_CODEC}" -b:v "${bitrate}k" \
65 ${VIDCOMP_PRESET:+"-preset" "$VIDCOMP_PRESET"} -pass 2 \
66 -c:a copy "${VIDCOMP_OUTPUT_FILE}"