Fix #172 by adding parsing step to save_metadata

* creates a correctly formatted chapter.txt file
  with titles from audible-cli .json
* uses this chapter.txt instead of ffprobe output
  in case audible-cli data should be used
This commit is contained in:
Stefan Weiberg 2021-09-16 07:10:49 +02:00 committed by Stefan Weiberg
parent 5ea4bd10a4
commit 56ca165063

View File

@ -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. # 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'` working_directory=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'`
metadata_file="${working_directory}/metadata.txt" 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. # 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. # 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}" \ 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" | 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 audiblecli 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 | pad(2)))=\(((.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 | pad(2)))NAME=\(.value.title)"' "${extra_chapter_file}" > "${tmp_chapter_file}"
fi
fi fi
debug "Metadata file $metadata_file" debug "Metadata file $metadata_file"
debug_file "$metadata_file" debug_file "$metadata_file"
@ -882,8 +901,14 @@ do
# Perform file tasks on output file. # Perform file tasks on output file.
# ---- # ----
# ffmpeg seems to copy only chapter position, not chapter names. # 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 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%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"
fi
mp4chaps -i "${output_file}" mp4chaps -i "${output_file}"
fi fi
fi fi