-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathffmpeg_utils.sh
More file actions
327 lines (269 loc) · 9.88 KB
/
ffmpeg_utils.sh
File metadata and controls
327 lines (269 loc) · 9.88 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env bash
# ffmpeg.x2.cmp() {
# local videofiles = $@
# ffmpeg -i "$input" -vf "setpts=0.5*PTS" -filter:a "atempo=2.0" -vcodec libx265 -crf 28 "$output.mkv"
# return $?
# }
# ffmpeg.get.wav() {
# local videofiles = $@
# ffmpeg -y -i "$output.mkv" -ar 16000 -ac 2 -c:a pcm_s16le "$output.wav"
# return $?
# }
# ffmpeg.get.srt() {
# local videofiles = $@
# ffmpeg -i "$input" -map 0:s:0 -c:s srt -f srt -y "$output.srt"
# return $?
# }
# for input in $videofiles; do
# output="${input%.*}.cmp.x2"
# ffmpeg -i "$input" -vf "setpts=0.5*PTS" -filter:a "atempo=2.0" -vcodec libx265 -crf 28 "$output.mkv"
# done
# # Convert video to MP4
# set -euo pipefail
# # Supported formats
# readonly SUPPORTED_EXTS=("mkv" "avi" "mov" "flv" "wmv" "webm" "m4v" "mpg" "mpeg")
# readonly SUPPORTED_MIMES=("video/x-matroska" "video/x-msvideo" "video/quicktime"
# "video/x-flv" "video/x-ms-wmv" "video/webm" "video/mp4"
# "video/mpeg")
# usage() {
# cat <<EOF
# Usage: $(basename "$0") [OPTIONS] INPUT_FILE
# Convert video file to MP4 format.
# OPTIONS:
# -h, --help Show this help message
# -o, --output FILE Output file path (default: INPUT.mp4)
# -q, --quality PRESET FFmpeg preset: ultrafast, fast, medium, slow (default: medium)
# SUPPORTED FORMATS:
# Extensions: ${SUPPORTED_EXTS[*]}
# ENVIRONMENT:
# NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
# NAUTILUS_SCRIPT_CURRENT_URI
# EOF
# }
# convert_to_mp4() {
# local input="$1"
# local output="${2:-${input%.*}.mp4}"
# local quality="${3:-medium}"
# [[ ! -f "$input" ]] && { echo "Error: Input file not found: $input" >&2; return 1; }
# local ext="${input##*.}"
# local supported=0
# for e in "${SUPPORTED_EXTS[@]}"; do
# [[ "${ext,,}" == "$e" ]] && { supported=1; break; }
# done
# [[ $supported -eq 0 ]] && { echo "Error: Unsupported extension: $ext" >&2; return 1; }
# [[ -f "$output" ]] && { echo "Error: Output file exists: $output" >&2; return 1; }
# ffmpeg -i "$input" -c:v libx264 -preset "$quality" -c:a aac -strict experimental \
# -movflags +faststart "$output" 2>&1 | grep -v "^frame=" || return 1
# echo "$output"
# }
# # Parameter processor
# process_params() {
# local input="" output="" quality="medium"
# while [[ $# -gt 0 ]]; do
# case "$1" in
# -h|--help)
# usage
# exit 0
# ;;
# -o|--output)
# [[ -z "${2:-}" ]] && { echo "Error: --output requires argument" >&2; exit 1; }
# output="$2"
# shift 2
# ;;
# -q|--quality)
# [[ -z "${2:-}" ]] && { echo "Error: --quality requires argument" >&2; exit 1; }
# quality="$2"
# shift 2
# ;;
# -*)
# echo "Error: Unknown option: $1" >&2
# usage >&2
# exit 1
# ;;
# *)
# [[ -n "$input" ]] && { echo "Error: Multiple input files not supported" >&2; exit 1; }
# input="$1"
# shift
# ;;
# esac
# done
# [[ -z "$input" ]] && { echo "Error: No input file specified" >&2; usage >&2; exit 1; }
# echo "$input|$output|$quality"
# }
# # Nautilus integration
# nautilus_handler() {
# local input output result
# if [[ -n "${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS:-}" ]]; then
# input="$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | head -n1)"
# elif [[ -n "${NAUTILUS_SCRIPT_CURRENT_URI:-}" ]]; then
# input="$(echo "$NAUTILUS_SCRIPT_CURRENT_URI" | sed 's|^file://||' | xargs -0 printf '%b')"
# else
# notify-send -u critical "Conversion Error" "No file selected"
# exit 1
# fi
# [[ ! -f "$input" ]] && { notify-send -u critical "Error" "File not found: $input"; exit 1; }
# output="${input%.*}.mp4"
# if result=$(convert_to_mp4 "$input" "$output" "medium" 2>&1); then
# notify-send -u normal "Conversion Complete" "$(basename "$result")" \
# -A "open=Open Folder" \
# -A "delete=Delete Original" | while read -r action; do
# case "$action" in
# open) xdg-open "$(dirname "$result")" ;;
# delete) rm "$input" && notify-send "Deleted" "$(basename "$input")" ;;
# esac
# done
# else
# notify-send -u critical "Conversion Failed" "$result"
# exit 1
# fi
# }
# # Main entry point
# main() {
# if [[ -n "${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS:-}" ]] || [[ -n "${NAUTILUS_SCRIPT_CURRENT_URI:-}" ]]; then
# nautilus_handler
# else
# IFS='|' read -r input output quality <<< "$(process_params "$@")"
# convert_to_mp4 "$input" "$output" "$quality"
# fi
# }
# WARNING: DO NOT USE THIS!
# main "$@"
# # ID3 tag from filename regex pattern
# tag_from_filename() {
# local pattern='(.+) - (.+) - (.+)\.mp3$'
# local dryrun=false
# while [[ $# -gt 0 ]]; do
# case $1 in
# -h|--help)
# cat <<EOF
# Usage: tag_from_filename [OPTIONS] [FILES...]
# Extract artist/album/title from filenames using regex, write ID3 tags.
# Options:
# -h, --help Show help
# -n, --dryrun Show actions without executing
# -r, --regex PATTERN Regex with 3 capture groups: artist, album, title
# Default: '(.+) - (.+) - (.+)\.mp3$'
# Example:
# tag_from_filename -r '^(\w+)-(\w+)-(.+)\.mp3$' *.mp3
# EOF
# return 0
# ;;
# -n|--dryrun) dryrun=true; shift ;;
# -r|--regex) pattern="$2"; shift 2 ;;
# *) break ;;
# esac
# done
# for f in "$@"; do
# [[ "$f" =~ $pattern ]] || { echo "Skip: $f (no match)"; continue; }
# local artist="${BASH_REMATCH[1]}"
# local album="${BASH_REMATCH[2]}"
# local title="${BASH_REMATCH[3]}"
# if $dryrun; then
# echo "Would tag: $f -> artist='$artist' album='$album' title='$title'"
# else
# ffmpeg -i "$f" -metadata artist="$artist" -metadata album="$album" \
# -metadata title="$title" -codec copy -y "tagged_$f" 2>/dev/null && \
# mv "tagged_$f" "$f"
# echo "Tagged: $f"
# fi
# done
# }
# # Organize files by metadata into folder structure
# organize_by_metadata() {
# local pattern='${artist}/${album}/${title}.mp3'
# local dryrun=false
# while [[ $# -gt 0 ]]; do
# case $1 in
# -h|--help)
# cat <<EOF
# Usage: organize_by_metadata [OPTIONS] [FILES...]
# Read ID3 tags, organize into folder structure.
# Options:
# -h, --help Show help
# -n, --dryrun Show actions without executing
# -r, --pattern STR Path pattern with variables: artist, album, title
# Default: '\${artist}/\${album}/\${title}.mp3'
# Example:
# organize_by_metadata -r '\${album}/\${title}.mp3' *.mp3
# EOF
# return 0
# ;;
# -n|--dryrun) dryrun=true; shift ;;
# -r|--pattern) pattern="$2"; shift 2 ;;
# *) break ;;
# esac
# done
# for f in "$@"; do
# local artist=$(ffprobe -v error -show_entries format_tags=artist \
# -of default=noprint_wrappers=1:nokey=1 "$f" 2>/dev/null)
# local album=$(ffprobe -v error -show_entries format_tags=album \
# -of default=noprint_wrappers=1:nokey=1 "$f" 2>/dev/null)
# local title=$(ffprobe -v error -show_entries format_tags=title \
# -of default=noprint_wrappers=1:nokey=1 "$f" 2>/dev/null)
# [[ -z "$artist" || -z "$album" || -z "$title" ]] && \
# { echo "Skip: $f (missing tags)"; continue; }
# local target=$(eval echo "$pattern")
# local target_dir=$(dirname "$target")
# if $dryrun; then
# echo "Would move: $f -> $target"
# else
# mkdir -p "$target_dir"
# mv "$f" "$target"
# echo "Moved: $f -> $target"
# fi
# done
# }
# # Combined: tag from filename, then organize
# tag_and_organize() {
# local tag_pattern='(.+) - (.+) - (.+)\.mp3$'
# local org_pattern='${artist}/${album}/${title}.mp3'
# local dryrun=false
# while [[ $# -gt 0 ]]; do
# case $1 in
# -h|--help)
# cat <<EOF
# Usage: tag_and_organize [OPTIONS] [FILES...]
# Extract tags from filenames, write ID3, organize into folders.
# Options:
# -h, --help Show help
# -n, --dryrun Show actions without executing
# -r, --regex PATTERN Filename regex (3 groups: artist, album, title)
# -p, --pattern PATH Output path pattern
# Default regex: '(.+) - (.+) - (.+)\.mp3$'
# Default pattern: '\${artist}/\${album}/\${title}.mp3'
# EOF
# return 0
# ;;
# -n|--dryrun) dryrun=true; shift ;;
# -r|--regex) tag_pattern="$2"; shift 2 ;;
# -p|--pattern) org_pattern="$2"; shift 2 ;;
# *) break ;;
# esac
# done
# for f in "$@"; do
# [[ "$f" =~ $tag_pattern ]] || { echo "Skip: $f (no match)"; continue; }
# local artist="${BASH_REMATCH[1]}"
# local album="${BASH_REMATCH[2]}"
# local title="${BASH_REMATCH[3]}"
# local target=$(eval echo "$org_pattern")
# local target_dir=$(dirname "$target")
# if $dryrun; then
# echo "Would process: $f"
# echo " Tag: artist='$artist' album='$album' title='$title'"
# echo " Move: -> $target"
# else
# ffmpeg -i "$f" -metadata artist="$artist" -metadata album="$album" \
# -metadata title="$title" -codec copy -y "tagged_$f" 2>/dev/null || \
# { echo "Failed: $f"; continue; }
# mkdir -p "$target_dir"
# mv "tagged_$f" "$target"
# echo "Processed: $f -> $target"
# fi
# done
# }
# # Dryrun first
# tag_and_organize -n -r '^(\w+)-(\w+)-(.+)\.mp3$' *.mp3
# # Execute
# tag_and_organize -r '^(\w+)-(\w+)-(.+)\.mp3$' *.mp3
# # Custom output pattern
# tag_and_organize -p 'Music/${artist}/${title}.mp3' *.mp3