forked from kjunggithub/audiobook-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.sh
executable file
·74 lines (65 loc) · 1.85 KB
/
convert.sh
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
#!/bin/bash
# variables
i=0
green='\033[0;32m'
reset='\033[0m'
output_file="output/output.mp3"
mkdir -p input
mkdir -p output
# flatten folders if any
parent=./input
newfolder=./input
for folder in "$parent"/*; do
if [[ -d "$folder" ]]; then
foldername="${folder##*/}"
for file in "$parent"/"$foldername"/*; do
filename="${file##*/}"
newfilename="$foldername"_"$filename"
mv "$file" "$newfolder"/"$newfilename"
done
fi
done
echo -e "${green}Combining the following audiobook parts: ${reset}"
for audiobook in input/*.mp3; do
# get audiobook name without the input prefix
name="${audiobook#"input/"}"
# get the filename without the extension
filename="${name%.*}"
# concat parts file into a single string for
if [[ $i < 1 ]]; then
parts="${audiobook}"
else
parts="$parts|${audiobook}"
fi
echo -e "${name}"
(( i++ ))
done
echo -e "${green}Ready to combine and convert to m4b (y/n)? ${reset}"
read response
if echo -e "$response" | grep -iq "^y" ;
then
echo "${green}Combining audiobook parts... ${reset}"
ffmpeg -i concat:"${parts}" -acodec copy output/output0.mp3
for cover in input/*.png
do
if [ -f "$cover" ]
then
ffmpeg -i output/output0.mp3 -i "${cover}" -map 0:0 -map 1:0 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" output/output.mp3
else
mv output/output0.mp3 output/output.mp3
fi
done
if [ -f "$output_file" ]
then
echo -e "${green}Converting mp3 to m4a... ${reset}"
ffmpeg -i "${output_file}" -c:a libfdk_aac -c:v copy "output/${filename}-converted.m4a"
if [[ -f "output/${filename}-converted.m4a" ]]; then
echo -e "${green}Renaming .m4a to .m4b... ${reset}"
rm -rf ${output_file}
rm -rf output/output0.mp3
cd output/ && rename m4a m4b *
mp4file --optimize *.m4b
echo "done"
fi
fi
fi