summary refs log tree commit diff
path: root/discordify.sh
blob: 265e692932c272a91e1e7cc24a02fb467700c42a (plain)
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
#!/usr/local/bin/fish

if test -z "$argv[1]" -o ! -e "$argv[1]"
	echo "The input file '$argv[1]' is not present or does not exist"
	exit
end

if test -z "$argv[2]"
	echo "Output file not specified!"
	exit
end

set ifile "$argv[1]"
set ofile "$argv[2]"

# https://stackoverflow.com/q/30977472
# File length (seconds)
set length (\
		ffprobe -i "$ifile" \
		-show_entries format=duration -v quiet -of csv="p=0"\
	)

# I think this is supposed to say KB? But the RHS of the division seems to
# be the filesize in kilobit. It's 7.6 and not 8 (for an 8MB file) because
# i was getting slightly over 8MB. I think because of file overhead?
# - 2023-12-09 genny
#                               v File Size in MB
set file_bitrate (math "floor((7.6 * 8000) / $length)")
set audio_bitrate 128
set video_bitrate (math "$file_bitrate - $audio_bitrate")

echo "Length of '$ifile' is $length and will get a bitrate of "$video_bitrate"kbps"
echo "Output file will be '$ofile'"

#ffmpeg -i $ifile -vf scale=320:-1 -b:v $bitrate"k" $ofile

# Two pass: http://trac.ffmpeg.org/wiki/Encode/H.264#twopass
ffmpeg -y -i $ifile \
	-vf scale=960:-1 \
	-c:v libx264 -b:v $video_bitrate"k" -pass 1 \
	-an \
	-f null /dev/null && \
ffmpeg -i $ifile \
	-vf scale=960:-1 \
	-c:v libx264 -b:v $video_bitrate"k" -pass 2 \
	-c:a aac -b:a $audio_bitrate"k" \
	$ofile