Search code examples
id3id3v2factor-lang

Write / change raw ID3 tags?


I have an MP3 file:

B{
    255 251 144 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 73 110 102 111 0 0 0 15
    0 0 30 161 0 50 3 66 0 3 5 8 10 13 15 18 20 24 26 28
    31 33 36 38 41 43 47 49 52 54 56 59 61 64 66 69 72
    75 77 79 82 84 87 89 92 95 98 100 103 105 107 110
    112 115 118 ~3277535 more~
}

And I have some metadata I want to put in it:

H{
    { "title" "Superstar (feat. Krewella)" }
    { "artist" "Pegboard Nerds & NGHTMRE" }
    { "num" 1 }
}

Factor doesn't have a vocabulary to write ID3 tags (yet), but I have the raw file data and some data I'd like to write.

Wikipedia isn't any help, and the mpg123 source doesn't really clear anything up either.

How do I put the data from the hashtable into the ID3v2 header?


Solution

  • We don't have support for writing id3 tags yet. But python has and we have support for wrapping Python modules. So if you are willing to be pragmatic first sudo pip install mutagen then:

    USING: kernel python python.syntax sequences ;
    IN: examples.python.mutagen
    
    PY-QUALIFIED-FROM: mutagen.easyid3 => EasyID3 ( name -- obj ) ;
    PY-METHODS: mutagen:easyid3:EasyID3 =>
        __setitem__ ( self key value -- )
        save ( self -- ) ;
    
    : <EasyID3> ( str -- easyid3 )
        >py mutagen.easyid3:EasyID3 ;
    
    : setitem ( obj key val -- )
        [ >py ] bi@ __setitem__ ;
    
    : update-tags ( easyid3 assoc -- )
        dupd [ first2 setitem ] with each save ;
    

    Then you can write id3 tags like this:

    [
        "/path/to/mp3-file.mp3" <EasyID3>  
        { 
            { "title" "Superstar (feat. Krewella)" }
            { "artist" "Pegboard Nerds & NGHTMRE" }
        } update-tags
    ] with-destructors
    

    Of course doing it in pure Factor isn't impossible. But you would have to study up on the mp3 and id3v1-2 specifications.