Certainly! To concatenate 50 MP3 files in numerical order and add chapters to the resulting MP3 file using ffmpeg, you can follow these steps:
-
Create a Text File for Concatenation:
First, create a text file (e.g., filelist.txt) that lists all the MP3 files in the order you want to concatenate them. You can automate this with a simple script.
for i in {1..50}; do echo "file 'file$i.mp3'" >> filelist.txt; done
This will create a filelist.txt with the following content:
file 'file1.mp3'
file 'file2.mp3'
...
file 'file50.mp3'
-
Create a Chapters File:
Next, create a chapters file (e.g., chapters.txt) that defines the chapters. You’ll need the timestamps where each file starts. You can get these timestamps by summing the durations of the MP3 files.
Here’s a script to generate the chapters file:
echo "00:00:00.00" > timestamps.txt
total_duration=0
for i in {1..50}; do
duration=$(ffprobe -i "file$i.mp3" -show_entries format=duration -v quiet -of csv="p=0")
total_duration=$(echo "$total_duration + $duration" | bc)
hours=$(echo "$total_duration / 3600" | bc)
minutes=$(echo "($total_duration % 3600) / 60" | bc)
seconds=$(echo "$total_duration % 60" | bc)
printf "%02d:%02d:%06.3f\n" $hours $minutes $seconds >> timestamps.txt
done
This will create a timestamps.txt file with the start times of each chapter. Now, create the chapters.txt file:
echo "[CHAPTER]" > chapters.txt
for i in {0..49}; do
start=$(sed -n "${i}p" timestamps.txt)
end=$(sed -n "$((i+1))p" timestamps.txt)
if [ -z "$end" ]; then
end=$(sed -n "$((i))p" timestamps.txt)
fi
echo "TIMEBASE=1/1000" >> chapters.txt
echo "START=$start" >> chapters.txt
echo "END=$end" >> chapters.txt
echo "title=Chapter $i" >> chapters.txt
done
-
Concatenate and Add Chapters:
Finally, use ffmpeg to concatenate the files and add the chapters:
ffmpeg -f concat -safe 0 -i filelist.txt -i chapters.txt -c copy -map_metadata 1 -map 0 -write_xing 0 output.mp3
This command will concatenate the MP3 files and add the chapters to the output file output.mp3.
Note: Ensure that all your MP3 files have the same codec, bitrate, and sample rate for seamless concatenation. If they differ, you may need to re-encode them to match.
Here’s a summary of the entire process in a script:
#!/bin/bash
# Create filelist.txt
for i in {1..50}; do echo "file 'file$i.mp3'" >> filelist.txt; done
# Create timestamps.txt
echo "00:00:00.00" > timestamps.txt
total_duration=0
for i in {1..50}; do
duration=$(ffprobe -i "file$i.mp3" -show_entries format=duration -v quiet -of csv="p=0")
total_duration=$(echo "$total_duration + $duration" | bc)
hours=$(echo "$total_duration / 3600" | bc)
minutes=$(echo "($total_duration % 3600) / 60" | bc)
seconds=$(echo "$total_duration % 60" | bc)
printf "%02d:%02d:%06.3f\n" $hours $minutes $seconds >> timestamps.txt
done
# Create chapters.txt
echo "[CHAPTER]" > chapters.txt
for i in {0..49}; do
start=$(sed -n "${i}p" timestamps.txt)
end=$(sed -n "$((i+1))p" timestamps.txt)
if [ -z "$end" ]; then
end=$(sed -n "$((i))p" timestamps.txt)
fi
echo "TIMEBASE=1/1000" >> chapters.txt
echo "START=$start" >> chapters.txt
echo "END=$end" >> chapters.txt
echo "title=Chapter $i" >> chapters.txt
done
# Concatenate and add chapters
ffmpeg -f concat -safe 0 -i filelist.txt -i chapters.txt -c copy -map_metadata 1 -map 0 -write_xing 0 output.mp3
Run this script in your terminal, and it will produce the concatenated MP3 file with chapters.