Skip to content

Embed book description into m4b #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion m4bify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
# - mp4chaps For adding chapter metadata to the final M4B file.
# - mp4art For adding in a cover image to the final M4A file before converting it to M4B.

readonly VERSION="v0.3.5"
readonly VERSION="v0.3.6"

# Color schema for pretty print
readonly NC='\033[0m' # No Color
Expand Down Expand Up @@ -311,6 +311,44 @@ function add_cover_image {
fi
}

function add_description {
local m4b_file=$1 source_dir=$2 temp_dir=$3
local description_file

echo -e "${COLORS[ACTION]}Checking for book description file...${NC}"

# Use the first matched file in the source folder as book description
description_file=$(find "${source_dir}" -type f \
\( -iname "*.txt" -o -iname "*.info" -o -iname "*.md" \) | head -n 1)

# Embed the book description into the m4b audiobook
if [[ -f "${description_file}" ]]; then
rel_path=$( get_relative_path "${source_dir}" "${description_file}" )
echo -e "${COLORS[INFO]}Found description in '${rel_path}'${NC}"

# Read and clean up the description:
# - Trim leading/trailing whitespace
# - Preserve internal newlines
description=$(sed 's/^[[:space:]]*//;s/[[:space:]]*$//' "${description_file}")

original_m4a="${temp_dir}/final.nodescr.m4a"
mv "${m4b_file}" "${original_m4a}"

echo -e "${COLORS[ACTION]}Embedding description into audiobook metadata...${NC}"
if ${FFMPEG} -i "${original_m4a}" \
-metadata description="${description}" \
-codec copy "${m4b_file}" -y > /dev/null 2>&1; then
echo -e "${COLORS[SUCCESS]}Book description embedded successfully.${NC}"
else
echo -e "${COLORS[ERROR]}Failed to embed book description.${NC}"
exit 1
fi
else
echo -e "${COLORS[INFO]}No book description found.${NC}"
echo -e "${COLORS[WARN]}Skipped book description addition.${NC}"
fi
}

function add_metadata {
local m4b_file=$1 source_dir=$2 temp_dir=$3
local dir_name author title date
Expand Down Expand Up @@ -610,6 +648,10 @@ echo -e "---"
add_cover_image "${FINAL_M4A_FILE}" "${INPUT_DIR}" "${TEMP_DIR}"
echo -e "---"

# Add book description (if available)
add_description "${FINAL_M4A_FILE}" "${INPUT_DIR}" "${TEMP_DIR}"
echo -e "---"

# Add audiobook ID3 tags
add_metadata "${FINAL_M4A_FILE}" "${INPUT_DIR}" "${TEMP_DIR}"

Expand Down
2 changes: 1 addition & 1 deletion m4bulk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
# - Logs for each audiobook conversion will be saved in the same directory as the source audiobooks.
# - Customize worker threads and m4bify options to optimize for system resources and project requirements.

readonly VERSION="v0.3.5"
readonly VERSION="v0.3.6"

# Color schema for pretty print
readonly NC='\033[0m' # No Color
Expand Down