Question

I'm currently writing a command line tool to convert a input music library with various formats (flac / ogg / mp3 / ...) to an output music library of a given format (flac / ogg / mp3). I've based it on avconv (or ffmpeg if avconv is not available) since it is the most complete command line converter i've found. My script is available at this URL (GitHub):

https://github.com/biapy/howto.biapy.com/blob/master/various/mussync-tools

I'm trying to pass the metadata from the input library files to the output/converted library files.

I've come up with this code:

 local MAP_METADATA=' 0:g'
  # Specific needs for some input formats/
  case "${INPUT_FILE_MIMETYPE}" in
    'application/ogg' )
      # Get input metadata from first audio stream and direct it to global.
      MAP_METADATA=' 0:s:0'
      ;;

    * )
      # Do nothing.
      # MAP_METADATA=' 0:g'
      ;;
  esac

  # Specific needs for some output formats/
  local OUTPUT_OPTIONS=""
  case "${OUTPUT_FORMAT}" in
    'flac' )
      # No encoding options needed.
      ENCODING_OPTIONS=""
      ;;

    'ogg' )
      # Set vorbis as default codec for ogg.
      OUTPUT_OPTIONS="-codec:a libvorbis -f ${OUTPUT_FORMAT}"
      # Map input metadata to all audio streams in ogg container.
      MAP_METADATA=":s:a ${MAP_METADATA}"
      ;;

    * )
      # Do nothing.
      # MAP_METADATA="${MAP_METADATA}"
      OUTPUT_OPTIONS="-f ${OUTPUT_FORMAT}"
      ;;
  esac

  # Dangerous solution for mp3 cbr format:
  # Write output on pipe and then directed to file.
  # For cbr format for mp3 files. Harmless for other formats.
  # See: http://ffmpeg.zeranoe.com/forum/viewtopic.php?f=7&t=377
  #
  # What about log output ? how to prevent it from being included in
  # the resulting output file ?

  if ! command ${AVCONV} -i "${INPUT_FILE}" \
             -vn -sn \
            -map_metadata${MAP_METADATA} \
            -loglevel "${LOG_LEVEL}" \
            ${AVCONV_OPTIONS} \
            ${OUTPUT_OPTIONS} \
            ${ENCODING_OPTIONS} \
            "${OUTPUT_TEMP_FILE}"; then
    test "${QUIET}" != 'True' && echo "Failed."
    test -e "${OUTPUT_TEMP_FILE}" && command rm "${OUTPUT_TEMP_FILE}"

    return 1
  else
    test "${QUIET}" != 'True' && echo "Done."

    # Test if fix for MP3 VBR is needed.
    # See: http://ffmpeg.zeranoe.com/forum/viewtopic.php?f=7&t=377
    if [ "${OUTPUT_FORMAT}" = 'mp3' -a "${ENCODING_MODE}" != 'CBR' ]; then
      # Output file is MP3 and VBR. Apply header fix.
      if [ "${VERBOSE}" = 'True' ]; then
        command vbrfix "${OUTPUT_TEMP_FILE}" "${OUTPUT_FILE}"
      else
        command vbrfix "${OUTPUT_TEMP_FILE}" "${OUTPUT_FILE}"
      fi
    else
      # Nothing to do but rename the file.
      command mv "${OUTPUT_TEMP_FILE}" "${OUTPUT_FILE}"
    fi

    # Delete temporary file if it is still present.
    test -e "${OUTPUT_TEMP_FILE}" && command rm "${OUTPUT_TEMP_FILE}"

    # Fetch cover art from input file.
    transfert_images "${INPUT_FILE}" "${OUTPUT_FILE}"
  fi

My problem is that when converting a flac to ogg with the avconv version available on Ubuntu 13.10 Saucy Salamander, the metadata are not kept, despite this option (copy global metadata from input flac file to all audio streams of output ogg file):

--map_metadata:s:a 0:g

Does one of you know the correct --map_metadata option for copying metadata from a flac input file to a ogg output file when converting ?

ps: additional question: how to prevent avconv generated CBR mp3 files to have a VBR header ?

pps: i'm aware of tools such as beets, but i've yet to see a specialized command line tool that do this task.

Was it helpful?

Solution

Found the fix here:

https://bugs.kde.org/show_bug.cgi?id=306895

The good --map_metadata options are:

  • for ogg to any : --map_metadata 0:s:0
  • for any except ogg to ogg : --map_metadata:s:a 0:s:0

Note that the --map_metadata option is the same when outputing Ogg and inputing ogg.

As for CBR mp3 files, the fix presented here: http://ffmpeg.zeranoe.com/forum/viewtopic.php?f=7&t=37 (output to pipe) is valid. avconv output log informations to &2. &1 is free for data. EDIT: this fix work only for CBR mp3, it breaks VBR mp3 that needs direct output to file.

I've integrated the changes to mussync-tools 1.2.0:

https://github.com/biapy/howto.biapy.com/blob/master/various/mussync-tools

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top