Skip to content

Instantly share code, notes, and snippets.

@deepakjois
Created May 24, 2011 02:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepakjois/988032 to your computer and use it in GitHub Desktop.
Save deepakjois/988032 to your computer and use it in GitHub Desktop.
MP3 file information parsing using Go and libmpg123
package main
import "mp3"
import "os"
func main() {
mp3.PrintInfo(os.Args[1])
}
package mp3
// #cgo LDFLAGS: -lmpg123
/*
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include "mpg123.h"
*/
import "C"
import (
"fmt"
)
func PrintInfo(file string) {
var v1 *C.mpg123_id3v1
var v2 *C.mpg123_id3v2
C.mpg123_init()
defer C.mpg123_exit()
m := C.mpg123_new(nil, nil)
defer C.mpg123_delete(m)
f := C.CString(file)
if err := C.mpg123_open(m, f); err != C.MPG123_OK {
panic("Error reading file")
}
defer C.mpg123_close(m)
C.mpg123_scan(m)
meta := C.mpg123_meta_check(m)
if meta == C.MPG123_ID3 && C.mpg123_id3(m, &v1, &v2) == C.MPG123_OK {
var title, artist, album, genre string
switch false {
case v2 == nil:
fmt.Println("ID3V2 tag found")
title = C.GoString(v2.title.p)
artist = C.GoString(v2.artist.p)
album = C.GoString(v2.album.p)
genre = C.GoString(v2.genre.p)
case v1 == nil:
fmt.Println("ID3V1 tag found")
title = C.GoString(&v1.title[0])
artist = C.GoString(&v1.artist[0])
album = C.GoString(&v1.album[0])
genre = "Unknown" // FIXME convert int to string
}
fmt.Println(title)
fmt.Println(artist)
fmt.Println(album)
fmt.Println(genre)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment