-
Notifications
You must be signed in to change notification settings - Fork 11
/
discord-video.sh
executable file
·77 lines (61 loc) · 1.76 KB
/
discord-video.sh
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
#!/bin/bash
# 200 000 000 bits / video length = target bitrate
MAX_VIDEO_SIZE="187500000"
#MAX_AUDIO_SIZE="12500000"
# Check argument
if [[ "$1" == "" ]]; then
echo "Error: No video file selected"
echo "Usage: $0 <video-file>"
exit
fi
# Check if file is video, get duration if it is
DURATION=$(ffprobe -hide_banner "$1" -show_entries format=duration -v quiet -of csv="p=0")
if [[ "$DURATION" == "" ]]; then
echo Error, ffprobe returned no duration. "$1" is possibly not a video file
exit
fi
echo "$1" is a video file and is "$DURATION" seconds long
WIDTH=$(ffprobe -hide_banner "$1" -v quiet -show_entries stream=width -of csv=p=0)
HEIGHT=$(ffprobe -hide_banner "$1" -v quiet -show_entries stream=height -of csv=p=0)
ROTATION=$(ffprobe -hide_banner "$1" -v quiet -show_entries stream_side_data=rotation -of csv=p=0)
WIDTH=${WIDTH%%,}
HEIGHT=${HEIGHT%%,}
ROTATION=${ROTATION%%,}
[ "$ROTATION" ] || ROTATION=0
if [ "$WIDTH" -gt "$HEIGHT" ] && [ "${ROTATION##-}" != 90 ]; then
[ "$WIDTH" -gt 1280 ] && WIDTH=1280
HEIGHT=-1
echo "$1 is horizontal video; scaling to $WIDTH x $HEIGHT"
else
WIDTH=-1
[ "$HEIGHT" -gt 1280 ] && HEIGHT=1280
echo "$1 is vertical video; scaling to $WIDTH x $HEIGHT"
fi
# Calculate bitrate
ADJUSTED_DURATION=$(printf "%.0f\n" "$DURATION")
VIDEO_BITRATE=$((MAX_VIDEO_SIZE / ADJUSTED_DURATION))
#AUDIO_BITRATE=$(echo $((MAX_AUDIO_SIZE / ADJUSTED_DURATION)))
set -e
ffmpeg \
-hide_banner \
-i "$1" \
-c:v libvpx-vp9 \
-row-mt 1 \
-b:v "$VIDEO_BITRATE" \
-pix_fmt yuv420p \
-vf scale=$WIDTH:$HEIGHT \
-pass 1 \
-an \
-f null \
/dev/null
ffmpeg \
-hide_banner \
-i "$1" \
-c:v libvpx-vp9 \
-cpu-used 3 \
-row-mt 1 \
-b:v "$VIDEO_BITRATE" \
-pix_fmt yuv420p \
-vf scale=$WIDTH:$HEIGHT \
-pass 2 \
"$1-compressed.mp4"