-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompress
More file actions
executable file
·85 lines (77 loc) · 2.15 KB
/
compress
File metadata and controls
executable file
·85 lines (77 loc) · 2.15 KB
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
#!/usr/bin/env bash
#
# Compress a video file using ffmpeg with libx264 codec
# Usage: compress <video_path>
#
# lib265 was not used because nobody uses it e.g. whatsapp, ...
# It is not universally used like libx264.
#
# Interesting read: https://news.ycombinator.com/item?id=31317989
#
show_help() {
echo "Compress a video file using ffmpeg with libx264 codec"
echo "Usage: $(basename "$0") [-h] [-s <speed>] <video_path>"
echo " -h Show this help message"
echo " -s Set compression speed (1-7):"
echo " 1 - ultrafast"
echo " 2 - superfast"
echo " 3 - veryfast (default)"
echo " 4 - faster"
echo " 5 - fast"
echo " 6 - medium"
echo " 7 - slow"
}
if [ $# -eq 0 ]; then
show_help
exit 0
fi
preset="veryfast"
while getopts ":hs:" opt; do
case ${opt} in
h )
show_help
exit 0
;;
s )
case ${OPTARG} in
1) preset="ultrafast" ;;
2) preset="superfast" ;;
3) preset="veryfast" ;;
4) preset="faster" ;;
5) preset="fast" ;;
6) preset="medium" ;;
7) preset="slow" ;;
*)
echo "Invalid preset option: $OPTARG"
show_help
exit 1
;;
esac
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
show_help
exit 1
;;
: )
echo "Invalid option: -$OPTARG requires an argument" 1>&2
show_help
exit 1
;;
esac
done
shift "$((OPTIND -1))"
video_path="$1"
if [[ ! -f "$video_path" ]]; then
echo "Error: '$video_path' not found."
exit 1
fi
output_path="${video_path%.*}_compressed.${video_path##*.}"
echo "ffmpeg -i \"$video_path\" -c:v libx264 -preset \"$preset\" -c:a copy \"$output_path\""
ffmpeg -i "$video_path" -c:v libx264 -preset "$preset" -c:a copy "$output_path"
if [[ $? -eq 0 ]]; then
echo "Compression completed successfully: $output_path"
else
echo "Error: Compression failed."
exit 1
fi