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