-    while read -r line; do
-        yell "$(printf "file '%s'\n" "${line#./}")";
-        printf "file '%s'\n" "${line#./}" >> "$file_flist";
-    done < <(find "$dir_in" -type f -iname "*.mp3" | sort);
+    find "$dir_in" -type f -iname "*.mp3" | sort | while read -r line; do
+        local filename="${line#./}";
+        yell "$(printf "file '%s'\n" "$filename")";
+        printf "file '%s'\n" "$filename" >> "$file_flist";
+
+        # Get duration of the current file
+        duration=$(get_media_length "$filename");
+        chapter_end=$(echo "$chapter_start + $duration" | bc -l);
+
+        # Write chapter info
+        {
+            echo "[CHAPTER]";
+            echo "TIMEBASE=1/1000";
+            echo "START=$(echo "scale=0; $chapter_start * 1000" | bc -l)";
+            echo "END=$(echo "scale=0; $chapter_end * 1000" | bc -l)";
+            echo "title=$(basename "$filename" .mp3)";
+        } >> "$file_chapters";
+
+        chapter_start=$chapter_end;
+    done