-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhevconv
executable file
·138 lines (99 loc) · 3.06 KB
/
hevconv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
set -euf -o pipefail
#
# Convert to hevc
#
FFMPEG="ffmpeg"
EXTENSION="x265.mp4"
# With -7 switch, commence a new encoding only between these times to save
# energy. See https://en.wikipedia.org/wiki/Economy_7.
ECONOMY7_START=23
ECONOMY7_END=6
function show_help() {
cat << EOF
Usage: ${0##*/} [-h] [options] <INFILE> [INFILE...]
Convert videos to hevc using ffmpeg. The resulting video has .mp4 container &
is renamed to end with ${EXTENSION}.
-h display this help and exit
-b BITRATE write bitrate <BITRATE>. Default 500k. Must end in 'k'.
-f FRAMERATE re-encode with framerate <FRAMERATE>, in fps
-7 only begin processing within economy 7 period to save £££
<INFILE> can be any video format supported by your local ffmpeg build
EOF
}
# Set variables...
bitrate="500k"
economy7=''
framerate=''
OPTIND=1 # Reset is necessary if getopts was used previously in the script. It is a good idea to make this local in a function.
while getopts "hb:f:7" opt; do
case "$opt" in
h)
show_help
exit 0
;;
b) bitrate=$OPTARG
;;
f) framerate=$OPTARG
;;
7)
economy7=1
;;
'?')
show_help >&2
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.
if [ $# -lt 1 ]; then
show_help >&2
exit 1
fi
# Sanity check of econonmy 7 times
if [ $ECONOMY7_START == $ECONOMY7_END ]; then
echo >&2 "Economy 7 start & end times must be different!"
exit 1
fi
# Only process with cheap electricity: block until we are within the
# economy 7 period
function sleep_until_economy7() {
printed=''
while [ 1 ]; do
hour=`date -u +%H`
if [ $ECONOMY7_START -lt $ECONOMY7_END ]; then
if [ $hour -ge $ECONOMY7_START -a $hour -lt $ECONOMY7_END ]; then
return # yes
fi
else
if [ $hour -ge $ECONOMY7_START -o $hour -lt $ECONOMY7_END ]; then
return # yes
fi
fi
! test $printed && echo -e "Hour $hour not within economy 7 period of $ECONOMY7_START - $ECONOMY7_END; sleeping\n"
printed='yes'
sleep 30
done
}
# Check ffmpeg actually present
type $FFMPEG >/dev/null 2>&1 || { echo >&2 "I require ${FFMPEG} but it's not installed. Aborting."; exit 1; }
# Quiet: -hide_banner -loglevel error
# Subtitles: -scodec copy
# Unused -x265-params: no-slow-firstpass
FFMPEG_OPTS="-hide_banner -loglevel fatal -stats -c:a copy -bsf:a aac_adtstoasc -c:v hevc -preset medium -x265-params log-level=error"
if [[ "$framerate" != '' ]]; then
FRAMERATE_OPT="-r ${framerate}"
else
FRAMERATE_OPT=""
fi
# Loop through all args
for source in "$@"; do
test $economy7 && sleep_until_economy7
dest="${source%.*}.${EXTENSION}"
echo "Converting $source -> $dest @ ${bitrate}"
$FFMPEG -y -i "${source}" ${FFMPEG_OPTS} $FRAMERATE_OPT -b:v ${bitrate} -pass 1 -f mp4 /dev/null && \
$FFMPEG -i "${source}" ${FFMPEG_OPTS} $FRAMERATE_OPT -b:v ${bitrate} -pass 2 "${dest}"
rm -f ffmpeg2pass-0.log
ls -lh "${source}" "${dest}"
done
echo