Open coolaj86 opened 2 years ago
m4a-to-ffmetadata.sh
:
#!/bin/sh
set -e
set -u
# Output metadata file
output_file="ffmetadata.txt"
# Initialize the metadata file
echo ";FFMETADATA1" > "$output_file"
# Function to escape special characters
fn_escape() { (
echo "$1" | sed 's/"/\\"/g'
); }
fn_get_duration() { (
my_file="${1}"
if command -v mdls > /dev/null; then
fn_get_duration_mdls "${my_file}"
else
# TODO ffprobe
echo >&2 "non-mac not supported yet"
return 1
fi
); }
fn_get_duration_mdls() { (
my_file="${1}"
my_secs="$(
mdls -name kMDItemDurationSeconds -raw "${my_file}"
)"
my_ms="$(
echo "${my_secs} * 1000" |
bc -l |
cut -d'.' -f1
)"
echo "${my_ms}"
); }
main() { (
my_start=0
my_end=0
echo "Reading metadata from..."
for my_file in ./m4a/*.m4a; do
echo " ${my_file}"
# show ALL kMD fields
#mdls "${my_file}"
my_title="$(
mdls -name kMDItemTitle -raw "${my_file}"
)"
echo >&2 " Title: ${my_title}"
my_title="$(
fn_escape "${my_title}"
)"
my_artist="$(
mdls -name kMDItemAuthors -raw "${my_file}" |
grep '[a-z]' |
cut -d"\"" -f2
)"
echo >&2 " Artist: ${my_artist}"
my_artist="$(
fn_escape "${my_artist}"
)"
my_duration="$(
fn_get_duration "${my_file}"
)"
my_end="$((my_start + my_duration))"
# Add a chapter entry for the my_file
{
echo ''
echo "[CHAPTER]"
echo "TIMEBASE=1/1000"
echo "START=${my_start}"
echo "END=${my_end}"
echo "title=\"${my_title}\""
echo "artist=\"${my_artist}\""
} >> "${output_file}"
my_start="$((my_end + 1))"
done
echo ''
echo "Metadata file '${output_file}' created with chapters."
echo ''
); }
main
Add metadata from original M4As to M4B:
#!/bin/sh
set -e
set -u
my_file="${1}"
my_name="$(
basename "${my_file}" .m4b
)"
ffmpeg -i "ffmetadata.txt" -i "${my_file}" -map_metadata 1 -codec copy "${my_name}.chapters.m4b"
Still haven't figured out how to add the cover art without yet another tool...
I think it's something like this:
inputs.txt
Not sure if that correctly carries over the chapter info.
References: