diff --git a/AAXtoMP3 b/AAXtoMP3 index adc91ef..a77242a 100755 --- a/AAXtoMP3 +++ b/AAXtoMP3 @@ -381,6 +381,13 @@ trap 'rm -r -f "${working_directory}"' EXIT # Set up some basic working files ASAP. Note the trap will clean this up no matter what. working_directory=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'` metadata_file="${working_directory}/metadata.txt" +# Creating a temp file to store the chapter data collected in save_metadata, as the output +# folder will only be defined after save_metadata has been executed. +# This file is only required when using audible-cli data and executing in single mode to +# get proper chapter titles in single file m4b output. +if [[ "${audibleCli}" == "1" && "${mode}" == "single" ]] ; then + tmp_chapter_file="${working_directory}/chapter.txt" +fi # ----- # Validate the AAX and extract the metadata associated with the file. @@ -510,6 +517,18 @@ save_metadata() { # Then we delete all ':' since they make a filename invalid. jq -r '.content_metadata.chapter_info.chapters[] | "Chapter # start: \(.start_offset_ms/1000), end: \((.start_offset_ms+.length_ms)/1000) \n#\n# Title: \(.title)"' "${extra_chapter_file}" \ | tr -d ':' >> "$metadata_file" + # In case we want to use a single file m4b we need to extract the + # chapter titles from the .json generated by audible–cli and store + # them correctly formatted for mp4chaps in a chapter.txt + if [ "${mode}" == "single" ]; then + jq -r \ + 'def pad(n): tostring | if (n > length) then ((n - length) * "0") + . else . end; + .content_metadata.chapter_info.chapters | + to_entries | + .[] | + "CHAPTER\((.key))=\(((.value.start_offset_ms / (1000*60*60)) %24 | floor | pad(2))):\(((.value.start_offset_ms / (1000*60)) %60 | floor | pad(2))):\(((.value.start_offset_ms / 1000) %60 | floor | pad(2))).\((.value.start_offset_ms % 1000 | pad(3))) +CHAPTER\((.key))NAME=\(.value.title)"' "${extra_chapter_file}" > "${tmp_chapter_file}" + fi fi debug "Metadata file $metadata_file" debug_file "$metadata_file" @@ -882,8 +901,14 @@ do # Perform file tasks on output file. # ---- # ffmpeg seems to copy only chapter position, not chapter names. + # use already created chapter.txt from save_metadata() in case + # audible-cli data is used else use ffprobe to extract from m4b if [[ ${container} == "mp4" && $(type -P mp4chaps) ]]; then - ffprobe -i "${aax_file}" -print_format csv -show_chapters 2>/dev/null | awk -F "," '{printf "CHAPTER%02d=%02d:%02d:%02.3f\nCHAPTER%02dNAME=%s\n", NR, $5/60/60, $5/60%60, $5%60, NR, $8}' > "${output_directory}/${currentFileNameScheme}.chapters.txt" + if [ "${audibleCli}" == "1" ]; then + mv "${tmp_chapter_file}" "${output_directory}/${currentFileNameScheme}.chapters.txt" + else + ffprobe -i "${aax_file}" -print_format csv -show_chapters 2>/dev/null | awk -F "," '{printf "CHAPTER%d=%02d:%02d:%02.3f\nCHAPTER%dNAME=%s\n", NR, $5/60/60, $5/60%60, $5%60, NR, $8}' > "${output_directory}/${currentFileNameScheme}.chapters.txt" + fi mp4chaps -i "${output_file}" fi fi