Search code examples
ffmpegmp4itunesm4a

ffmpeg m4a/m4b/mp4 output file's "Time" value is incorrect when read into iTunes


I'm using ffmpeg to convert audiobooks to m4a/m4b/mp4. All seems to work until trying to play them in iTunes. It plays in VLC, QuickTime, and MacOS's Quicklook without issue.

"So why are you posting here? This isn't an iTunes forum."

I'm hoping this is iTunes being picky about file formats and that I can add some magic argument to my ffmpeg command and have it spit out something that iTunes can read.

Below is the bash function I'm using to do the conversion. I've tried m4a/mp4/m4b as values for TEMP_FILE_EXTENSION and tried opening the intermediate file as well. It's always the same corrupted "Time" value when you put it in iTunes.

dedrm_audible () {
  # Check for AtomicParsley, ffmpeg, and 3 args
  if (! type AtomicParsley >/dev/null 2>/dev/null) || (! type ffmpeg >/dev/null 2>/dev/null) || [ ! $# -eq 3 ]; then
    echo "Usage:"
    echo "    dedrm_audible <path to .aax> <activation bytes> <path to output file>"
    echo "    Note: AtomicParsley and ffmpeg must be in PATH variable"
    return
  fi
  local ORIGINAL_PWD="$(pwd)"
  local TEMP_DIR="/tmp/audible"
  local AUDIOBOOK_FILE="$1"
  local ACTIVATION_BYTES="$2"
  local OUTPUT_FILE="$3"
  local FULL_AUDIOBOOK_PATH="$(realpath "${AUDIOBOOK_FILE}")"
  local OUTPUT_PATH="$(realpath "${OUTPUT_FILE}")"
  local TEMP_FILE_EXTENSION="m4a"

  mkdir -p "${TEMP_DIR}"
  cd "${TEMP_DIR}"

  # Extract the book cover
  ffmpeg -activation_bytes "${ACTIVATION_BYTES}" -i "${FULL_AUDIOBOOK_PATH}" -vcodec copy artwork.png
  # Convert the audio
  ffmpeg -activation_bytes "${ACTIVATION_BYTES}" -i "${FULL_AUDIOBOOK_PATH}" -vn -c:a copy -v debug output.${TEMP_FILE_EXTENSION}
  # Add the cover to the new file
  AtomicParsley output.${TEMP_FILE_EXTENSION} --artwork artwork.png --overWrite

  # Put it where you want it and clean up
  cp output.${TEMP_FILE_EXTENSION} "${OUTPUT_PATH}"
  rm artwork.png
  rm output.${TEMP_FILE_EXTENSION}
  cd "${ORIGINAL_PWD}"
}

It goes off without a hitch. The new file is there waiting for me, with all the metadata including the cover when I do a "Get Info" or "Quick look" on it (I'm on MacOS). But when opening it and trying to play it in iTunes, the "time" field is way off and it immediately skips to the next song/audiobook in the queue.

Attempts to convert it within iTunes fail immediately - too quickly to see what's happening. The errors in the console simply say "Assert failure:" (with nothing after the colon).

edit: Tommy answered the question. Here's a working bash function:

dedrm_audible () {
  # Check for AtomicParsley, ffmpeg, and 3 args
  if (! type AtomicParsley >/dev/null 2>/dev/null) || (! type ffmpeg >/dev/null 2>/dev/null) || [ ! $# -eq 3 ]; then
    echo "Usage:"
    echo "    dedrm_audible <path to .aax> <activation bytes> <path to output file>"
    echo "    Note: AtomicParsley and ffmpeg must be in PATH variable"
    return
  fi
  local ORIGINAL_PWD="$(pwd)"
  local TEMP_DIR="/tmp/audible"
  local AUDIOBOOK_FILE="$1"
  local ACTIVATION_BYTES="$2"
  local OUTPUT_FILE="$3"
  # Alternative to realpath (since I read somewhere that it's not there by default on some systems): OUTPUT_PATH="$( cd "$( dirname "$OUTPUT_FILE" )" && pwd )"
  local FULL_AUDIOBOOK_PATH="$(realpath "${AUDIOBOOK_FILE}")"
  local AUDIOBOOK_NAME="${$(basename "${FULL_AUDIOBOOK_PATH}")%.aax}.m4a"
  local OUTPUT_PATH="$(realpath "${OUTPUT_FILE}")"
  local TEMP_FILE_EXTENSION="m4a"

  mkdir -p "${TEMP_DIR}"
  cd "${TEMP_DIR}"

  cp "${FULL_AUDIOBOOK_PATH}" "${AUDIOBOOK_NAME}"

  # Extract the book cover
  ffmpeg -activation_bytes "${ACTIVATION_BYTES}" -i "${AUDIOBOOK_NAME}" -vcodec copy artwork.png
  # Convert the audio
  ffmpeg -activation_bytes "${ACTIVATION_BYTES}" -i "${AUDIOBOOK_NAME}" -vn -c:a copy -v debug output.${TEMP_FILE_EXTENSION}
  # Add the cover to the new file
  AtomicParsley output.${TEMP_FILE_EXTENSION} --artwork artwork.png --overWrite

  # Put it where you want it and clean up
  mv output.${TEMP_FILE_EXTENSION} "${OUTPUT_PATH}"
  rm artwork.png
  rm "${AUDIOBOOK_NAME}"
  cd "${ORIGINAL_PWD}"
}

Solution

  • They stopped working for me too, but if you just change the file extension to .m4a it will import fine, then you just have to change type from music to audiobook and tell it to remember location and it works fine on mac or iPhone.

    The other approach is I went back and reran ffmpeg on the .aax with output to .m4b and iTunes handled it fine. I didn’t use.m4b before but I frankly don’t recall why.