Freitag, April 24, 2026

Rezept: OGG nach MP3 konvertieren mit autoradio-verträglichen Metadaten

Problem: Mein Autoradio verdaut nur MP3. Ich habe früher meine auf CD gekaufte Musik als OGG gesammelt, seit einiger Zeit sammle ich auch FLAC. Aber mein altes Ford Sync-System kann beides nicht.

(Nicht-Lösung): Eigentlich könnte alles ganz einfach sein, es hilft wie immer ffmpeg:

ffmpeg -i eingabedatei.xyz -aq 0 ausgabedatei.mp3

Das funktioniert auch für FLAC-Dateien perfekt. Auch OGG-Dateien werden konvertiert – jedoch kommen die Metadaten, die mein EasyTag da reingemacht hat, nie richtig an – selbst alte Player auf dem PC wie Clementine wissen nichts über die MP3s.

Echte Lösung: In die Vollen gehen und die Metadaten aus den OGG-Dateien rausholen und separat in die MP3-Dateien eintüten. Das geht mit folgendem Script, ich nenne es toMP3:

#!/bin/bash

# My ogg files contain metadata that does not work automatically with ffmpeg, so I need to sort it manually
function convertOGG () {
    local file=”$1”
    local output=”$2”

    # extract metadata
    local title=$(ffprobe -v quiet -show_entries stream_tags=TITLE -of default=nw=1:nk=1 ”$file”)
    local artist=$(ffprobe -v quiet -show_entries stream_tags=ARTIST -of default=nw=1:nk=1 ”$file”)
    local album=$(ffprobe -v quiet -show_entries stream_tags=ALBUM -of default=nw=1:nk=1 ”$file”)
    local track=$(ffprobe -v quiet -show_entries stream_tags=TRACK -of default=nw=1:nk=1 ”$file”)

    ffmpeg -i ”$file” 
    -id3v2_version 3 -write_id3v1 1 
    -map_metadata -1 
    -metadata title=”$title” -metadata artist=”$artist” -metadata album=”$album” -metadata track=”$track” 
    -aq 0 ”$output”
}

# FLAC metadata is simple, this works automatically
function convertFLAC () {
    local file=”$1”
    local output=”$2”
    ffmpeg -i ”$file” -map_metadata 0 -id3v2_version 3 -write_id3v1 1 -aq 0 ”$output”
}

# WAV could have metadata, too
# TODO: This is not really tested since I don’t have WAV files that often
function convertWAV () {
    local file=”$1”
    local output=”$2”
    ffmpeg -i ”$file” -map_metadata 0 -id3v2_version 3 -write_id3v1 1 -aq 0 ”$output”
}

f=”$1”
# Remove file extensions and add mp3
out=”$f” ; out=”${out%flac}” ; out=”${out%ogg}” ; out=”${out%wav}” ; out=”${out}mp3”

# Decide based on file contents / mime type what to do now
mimetype=$(file -b –mime-type ”$f”)
case ”$mimetype” in
    ”audio/ogg”)
        convertOGG ”$f” ”$out”
        ;;
    ”audio/flac”)
        convertFLAC ”$f” ”$out”
        ;;
    ”audio/wav”)
        convertWAV ”$f” ”$out”
        ;;
    *)
        echo ”Don’t know what to do with mime type $mimetype in $f.”
        exit 1
esac

Ob das auch für WAV-Dateien funzt? Das ist noch unklar. Da ffmpeg beim Encoding von Audio nicht parallelisiert / die Last auf alle Prozessoren verteilt, kann man das noch mittels folgendem find/xargs-basierten Hack abvespern, der alle OGG-Dateien im aktuellen Verzeichnis und allen Unterverzeichnissen in MP3 konvertiert.

find * -iname ”*.ogg”  -print0 | xargs -0 -P $(nproc) -I {} toMP3 ”{}”

Das setzt natürlich voraus, dass man das oben aufgeführte lange Script in der als ausführbar deklarierten Datei toMP3 in seinem Suchpfad ($PATH) abgelegt hat.

Jetzt kann ich mit meinem Autoradio hoffentlich wieder Alben hören, und nicht nur insgesamt alle Tracks auf dem USB-Stick in alphabetischer Reihenfolge der Dateinamen!