This repository was archived by the owner on Mar 28, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtranscode-video
More file actions
executable file
·188 lines (150 loc) · 5.2 KB
/
Copy pathtranscode-video
File metadata and controls
executable file
·188 lines (150 loc) · 5.2 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
source "$HOME/.local/share/dotfiles/bin/lib/helpers.sh"
set -e
get_input_file() {
log_step "Select input file" >&2
local method
method=$(gum choose "Browse with fzf" "Paste file path" --header "How would you like to select the input file?") || exit 1
local input_file
if [[ "$method" == "Browse with fzf" ]]; then
input_file=$(find "$HOME" -type f \( \
-iname "*.mp4" -o -iname "*.mkv" -o -iname "*.avi" -o \
-iname "*.mov" -o -iname "*.webm" -o -iname "*.m4v" -o \
-iname "*.flv" -o -iname "*.wmv" -o -iname "*.mpg" -o \
-iname "*.mpeg" -o -iname "*.m2ts" \) \
! -path "*/.*" \
! -path "*/.cache/*" \
! -path "*/.local/share/Trash/*" 2>/dev/null |
sed "s|^$HOME|~|" | sort |
fzf --prompt="Select video file: " --height=80% --color='pointer:green,marker:green' |
sed "s|^~|$HOME|")
else
input_file=$(gum input --placeholder "Enter full path to video file")
fi
if [[ -z "$input_file" ]]; then
log_error "No file selected or provided" >&2
exit 1
fi
if [[ ! -f "$input_file" ]]; then
log_error "File not found: $input_file" >&2
exit 1
fi
log_success "Selected: $input_file" >&2
echo "$input_file"
}
get_presets() {
local preset_type="$1"
if [[ "$preset_type" == "general" ]]; then
HandBrakeCLI -z 2>&1 | awk '/^General\//,/^Web\// {if (/^ [^ ]/) print}' | sed 's/^ //'
else
HandBrakeCLI -z 2>&1 | awk '/^[A-Z][a-z]+\// { category = $0; next } /^ [^ ]/ && category { print category " → " $0 }' | sed -e 's/^ //' -e 's/ → / → /'
fi
}
select_preset() {
log_step "Select preset" >&2
local preset
preset=$({
get_presets "general"
echo "──────────────────────────"
echo "» Show All Categories «"
} | fzf --prompt="Select preset (General/): " --height=80% --color='pointer:green,marker:green') || {
log_error "No preset selected" >&2
exit 1
}
if echo "$preset" | grep -q "Show All Categories"; then
preset=$(get_presets "all" |
fzf --prompt="Select preset: " --height=80% --color='pointer:green,marker:green' |
sed 's/.*→[[:space:]]*//')
[[ -z "$preset" ]] && { log_error "No preset selected" >&2; exit 1; }
fi
echo "$preset"
}
generate_output_name() {
local input="$1"
local suffix="$2"
local dir=$(dirname "$input")
local base=$(basename "$input")
echo "${dir}/${base%.*}_${suffix}.${base##*.}"
}
do_compress() {
local input_file="$1"
local preset=$(select_preset)
log_info "Using preset: $preset" >&2
local output_file=$(generate_output_name "$input_file" "compressed")
echo
gum style --foreground 108 "Input: $input_file"
gum style --foreground 108 "Output: $output_file"
gum style --foreground 108 "Preset: $preset"
echo
if ! ask_yes_no "Proceed with encoding?"; then
log_error "Cancelled"
exit 0
fi
log_step "Encoding video..."
echo
HandBrakeCLI -i "$input_file" -o "$output_file" -Z "$preset"
local exit_code=$?
echo
if [[ $exit_code -eq 0 ]]; then
log_success "Encoding complete!"
log_detail "$output_file"
else
log_error "Encoding failed"
exit 1
fi
}
do_transcode() {
local input_file="$1"
log_step "Select output format" >&2
local format=$(gum choose "mp4" "mkv" "mov" "webm" "avi" --header "Select container format") || exit 1
log_success "Format: $format" >&2
local dir=$(dirname "$input_file")
local name=$(basename "$input_file" | sed 's/\.[^.]*$//')
local output_file="${dir}/${name}.${format}"
if [[ -f "$output_file" ]]; then
log_error "Output file already exists: $output_file" >&2
exit 1
fi
echo
gum style --foreground 108 "Input: $input_file"
gum style --foreground 108 "Output: $output_file"
gum style --foreground 108 "Mode: Re-encode (high quality, preserves resolution/fps)"
echo
if ! ask_yes_no "Proceed with transcode?"; then
log_error "Cancelled"
exit 0
fi
log_step "Transcoding video..."
echo
local ffmpeg_cmd=(ffmpeg -i "$input_file" -c:v libx264 -preset fast -crf 18 -c:a aac -b:a 192k -y)
[[ "$format" == "mp4" || "$format" == "mov" ]] && ffmpeg_cmd+=(-movflags +faststart)
ffmpeg_cmd+=("$output_file")
"${ffmpeg_cmd[@]}"
local exit_code=$?
echo
if [[ $exit_code -eq 0 ]]; then
log_success "Transcode complete!"
log_detail "$output_file"
else
log_error "Transcode failed"
exit 1
fi
}
main() {
log_header "Video transcode"
log_step "Select operation"
local operation=$(gum choose \
"Compress / Downscale / Upscale" \
"Transcode" \
--header "What would you like to do?") || exit 1
log_success "Operation: $operation"
echo
local input_file=$(get_input_file)
echo
case "$operation" in
"Compress / Downscale / Upscale") do_compress "$input_file" ;;
"Transcode") do_transcode "$input_file" ;;
esac
show_done
}
main "$@"