-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscode
More file actions
executable file
·110 lines (98 loc) · 2.37 KB
/
Copy pathtranscode
File metadata and controls
executable file
·110 lines (98 loc) · 2.37 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
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
#!/bin/bash
# Usage: ./transcode -i <input_file> [-codec av1|h265] <bitrate>
# Parse arguments
INPUT=""
CODEC=""
BITRATE_CHOICE=""
while [[ $# -gt 0 ]]; do
case $1 in
-i)
INPUT="$2"
shift 2
;;
-codec)
CODEC="$2"
shift 2
;;
*)
BITRATE_CHOICE="$1"
shift
;;
esac
done
# Validate required arguments
if [[ -z "$INPUT" || -z "$BITRATE_CHOICE" ]]; then
echo "Usage: $0 -i <input_file> [-codec av1|h265] <bitrate>"
echo ""
echo "AV1/H265 bitrate options: 1M, 5M, 20M, 40M, 100M"
exit 1
fi
# Validate codec
if [[ "$CODEC" != "av1" && "$CODEC" != "h265" ]]; then
echo "Error: Invalid codec. Use 'av1' or 'h265'."
exit 1
fi
# Map Bitrate Selection to Numeric Values
case "$BITRATE_CHOICE" in
"1M")
BV="1000000"
MAX="2000000"
;;
"5M")
BV="5000000"
MAX="10000000"
;;
"20M")
BV="20000000"
MAX="50000000"
;;
"40M")
BV="40000000"
MAX="80000000"
;;
"100M")
BV="100000000"
MAX="200000000"
;;
*)
echo "Error: Invalid bitrate. Choose 1M, 5M, 20M, 40M, or 100M."
exit 1
;;
esac
# Get current working directory for output
SCRIPT_DIR="$(pwd)"
# Extract just filename (handles relative or absolute input paths)
BASENAME=$(basename "$INPUT")
BASE="${BASENAME%.*}"
# Build output filename
OUTPUT="${SCRIPT_DIR}/${BASE}_${CODEC}_${BITRATE_CHOICE}.mp4"
# Overwrite Protection: Increment suffix if file already exists
COUNTER=1
while [[ -f "$OUTPUT" ]]; do
OUTPUT="${SCRIPT_DIR}/${BASE}_${CODEC}_${BITRATE_CHOICE}-${COUNTER}.mp4"
((COUNTER++))
done
# Run FFmpeg
if [[ "$CODEC" == "av1" ]]; then
# AV1 codec with scaling filter to ensure even resolution
ffmpeg -i "$INPUT" \
-vf "scale='trunc(iw/2)*2':'trunc(ih/2)*2',format=nv12" \
-c:v av1_amf \
-preset quality \
-rc vbr_peak \
-b:v "$BV" \
-maxrate "$MAX" \
-bufsize "$MAX" \
"$OUTPUT"
else
# H265 codec
ffmpeg -i "$INPUT" \
-vf "scale='trunc(iw/2)*2':'trunc(ih/2)*2',format=nv12" \
-c:v hevc_amf \
-preset quality \
-rc vbr_peak \
-b:v "$BV" \
-maxrate "$MAX" \
"$OUTPUT"
fi
echo "Encoding complete: $OUTPUT"