-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert-a-to-b
More file actions
executable file
·136 lines (112 loc) · 3.46 KB
/
convert-a-to-b
File metadata and controls
executable file
·136 lines (112 loc) · 3.46 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
#!/usr/bin/env bash
clear_line() {
printf "\r\033[2K"
}
print_status() {
printf "\e[3;4m%s\e[0m\r" "$1"
}
print_status_report() {
clear_line
print_status "$1"
}
print_status_success() {
printf "\e[30;42m %s \e[0m" "$1"
shift
if [ -n "$1" ]; then
printf " \e[4m%s\e[0m" "$*"
fi
printf "\n"
}
print_status_error() {
printf "\e[1;101m %s \e[0m\n" "$1"
}
if [[ $# -lt 2 ]]; then
echo "convert-a-to-b"
echo " Convert all files between (media) file types"
echo " If only target and source formats are specified, it will convert all files in the current directory"
echo " Optionally, more parameters can be provided to specify which files to convert"
echo ""
echo "Usage: convert-a-to-b FROM_FORMAT TO_FORMAT [file file2 file3]"
echo " eg. convert-a-to-b \"webm\" \"mp4\""
exit
fi
to_extension="$2"
from_extension="$1"
shift
shift
if [[ "$to_extension" == "$from_extension" ]]; then
exit
fi
FILES=()
if [ $# -eq 0 ]; then
print_status "Scanning..."
while IFS= read -r -d $'\0'; do
FILES+=("$REPLY")
done < <(find . -name "*.$from_extension" -maxdepth 1 -type f -print0 2>/dev/null)
print_status_report "Scanned"
else
for file in "$@"; do
filename="$(basename -- "$file")"
extension="${filename##*.}"
if [[ "$extension" == "$from_extension" ]]; then
FILES+=("$file")
fi
done
fi
MAX_FILE_LEN=-1
for str in "${FILES[@]}"; do
filename="$(basename -- "$str")"
extension="${filename##*.}"
filename="${filename%.*}"
if [[ ${#filename} -gt ${MAX_FILE_LEN} ]]; then
MAX_FILE_LEN=${#filename}
fi
done
total="${#FILES[@]}"
success=0
for filepath in "${FILES[@]}"; do
location="$PWD"
filename="$(basename -- "$filepath")"
extension="${filename##*.}"
filename="${filename%.*}"
oldName="${filename}.${extension}"
newName="${filename}.${to_extension}"
oldFull="${location}/${oldName}"
newFull="${location}/${newName}"
if [ -f "$newFull" ]; then
if [ -f "$oldFull" ]; then
if command -v kioclient5 &>/dev/null; then
kioclient5 move "$newFull" trash:/ 2>/dev/null
elif command -v trash-put &>/dev/null; then
trash-put "$newFull"
fi
fi
fi
printf "Converting \e[1;36;40m %-${MAX_FILE_LEN}s \e[0m from \e[1m\`%s\`\e[0m to \e[1m\`%s\`\e[0m... " "$filename" "$extension" "$to_extension"
__OLD_TIMEFORMAT=$TIMEFORMAT
export TIMEFORMAT='%3lR'
res="$( (time ffmpeg -y -loglevel panic -i "$oldFull" -max_muxing_queue_size 1024 -vf 'scale=ceil(iw/2)*2:ceil(ih/2)*2' -ab 320k -map_metadata 0 "$newFull") 2>&1)"
res_exit_code=$?
cmd_duration="$(echo "$res" | tail -n1)"
export TIMEFORMAT=${__OLD_TIMEFORMAT}
unset __OLD_TIMEFORMAT
if [[ "$res_exit_code" -ne '0' ]]; then
print_status_error "DEAD ($res_exit_code)"
rm "$newFull" 2>/dev/null
continue
fi
if [ -z "$(find "$newFull" -maxdepth 1 -type f -size +1k 2>/dev/null)" ]; then
print_status_error "FAIL"
rm "$newFull" 2>/dev/null
continue
fi
touch -r "$oldFull" "$newFull"
if command -v kioclient5 &>/dev/null; then
kioclient5 move "$oldFull" trash:/ 2>/dev/null
elif command -v trash-put &>/dev/null; then
trash-put "$oldFull"
fi
print_status_success "DONE" "$cmd_duration"
((success++))
done
print_status_success "FINISHED $success/$total"