Skip to content

Instantly share code, notes, and snippets.

@jodaka
Last active June 2, 2025 13:23
Show Gist options
  • Save jodaka/1a69a12fa671f0f810593305b9b91005 to your computer and use it in GitHub Desktop.
Save jodaka/1a69a12fa671f0f810593305b9b91005 to your computer and use it in GitHub Desktop.
Script for converting a bunch of mp3 into a single m4b.
# brew tap homebrew-ffmpeg/ffmpeg
# brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-fdk-aac
# brew install AtomicParsley mp4v2
#!/usr/bin/env fish
if not test -f metadata.txt
echo "Error: metadata.txt not found. Create one and put author on the first line and title on the second"
exit 1
end
if not test -f cover.jpg
echo "Error: cover.jpg not found. Please find some and put into current catalog"
exit 1
end
echo "" > output.chapters.txt
set total_duration 0
set chapter_counter 1
for i in (ls -v *.mp3)
# for i in (ls -v *.aac)
set output_file (string replace ".mp3" ".aac" -- $i)
ffmpeg -i $i -c:a libfdk_aac -b:a 64k -map_metadata 0 $output_file
set duration (math -s0 (ffprobe -i $i -show_entries format=duration -v quiet -of csv="p=0"))
set hours (math -s0 $total_duration/3600)
set remainder (math -s0 $total_duration % 3600)
set minutes (math -s0 $remainder/60)
set seconds (math -s0 $remainder % 60)
printf "%02d:%02d:%02d.000 Глава %02d\n" $hours $minutes $seconds $chapter_counter >> output.chapters.txt
set total_duration (math $total_duration + $duration)
set chapter_counter (math $chapter_counter + 1)
end
for f in (ls -v *.aac)
echo "file '$PWD/$f'" >> files.txt
end
ffmpeg -f concat -safe 0 -i files.txt -c copy output.m4b
rm files.txt
mp4chaps --import output.m4b
set title (sed -n '2p' metadata.txt)
set author (sed -n '1p' metadata.txt)
AtomicParsley output.m4b --title "$title" --artist "$author" --artwork cover.jpg --overWrite
rm output.chapters.txt
rm *.aac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment