Estrarre informazioni e TAG da un file MP3 con le funzioni esterne del API di Avbin

Da Gambas-it.org - Wikipedia.

AVbin è una libreria di decodifica multimediale audio/video multipiattaforma che intende garantire alle applicazioni la compatibilità binaria a lungo termine.

La documentazione di AVbin può essere trovata:
- nel codice sorgente: "/usr/include/avbin.h;
- nel API online di riferimento |2|


Per poter fruire delle risorse di Avbin è necessario utilizzare nelle applicazioni Gambas la libreria (nella sua attuale versione): libavbin.so.0.0.7


Riportiamo di seguito un semplice esempio, con il quale è possibile estrarre i TAG ed informazioni generali dai file mp3:

Public Struct AVbinFileInfo
  structure_size As Long     ' Size of this structure, in bytes.
  n_streams As Integer       ' Number of streams contained in the file.
  start_time As Long         ' Starting time of all streams.
  duration As Long           ' Duration of the file. Does not include the time given in start_time.
  title[512] As Byte
  author[512] As Byte
  copyright[512] As Byte
  comment[512] As Byte
  album[512] As Byte
  annus As Integer
  track As Integer
  genre[32] As Byte
End Struct

Public Struct AVbinStreamInfo
  structure_size As Long        ' Size of this structure, in bytes.
  type As Integer               ' The type of stream; either audio or video.
  sample_format As Integer      ' The sample format for audio data.
  sample_rate As Integer        ' Number of samples per second, in Hz.
  sample_bits As Integer        ' Number of bits per sample; typically 8 or 16.
  channels As Integer           ' Number of interleaved audio channels.
End Struct


Library "libavbin:0.0.7"

Private Enum AVBIN_RESULT_ERROR = -1, AVBIN_RESULT_OK
Private Enum AVBIN_STREAM_TYPE_UNKNOWN = 0, AVBIN_STREAM_TYPE_VIDEO, AVBIN_STREAM_TYPE_AUDIO
Private Enum AVBIN_SAMPLE_FORMAT_U8 = 0,    ' Unsigned byte
             AVBIN_SAMPLE_FORMAT_S16,       ' Signed 16-bit integer
             AVBIN_SAMPLE_FORMAT_S24,       ' Signed 24-bit integer
             AVBIN_SAMPLE_FORMAT_S32,       ' Signed 32-bit integer
             AVBIN_SAMPLE_FORMAT_FLOAT      ' 32-bit IEEE floating-point

' AVbinResult avbin_init()
' Initialise AVbin.
Private Extern avbin_init() As Integer

' AVbinFile *avbin_open_filename(const char *filename)
' Open a media file given its filename.
Private Extern avbin_open_filename(filename As String) As Pointer

' AVbinResult avbin_file_info(AVbinFile *file, AVbinFileInfo *info)
' Get information about the opened file.
Private Extern avbin_file_info(avfile As Pointer, info As AVbinFileInfo) As Integer

' AVbinResult avbin_stream_info(AVbinFile *file, int stream_index, AVbinStreamInfo *info)
' Get information about a stream within the file.
Private Extern avbin_stream_info(avfile As Pointer, strindex As Integer, infoFlux As AVbinStreamInfo) As Integer

' void avbin_close_file(AVbinFile *file)
' Close a media file.
Private Extern avbin_close_file(avfile As Pointer)


Public Sub Main()

 Dim err As Integer
 Dim percorsoFile As String
 Dim media As Pointer
 Dim fileinfo As New AVbinFileInfo
 Dim streaminfo As New AVbinStreamInfo   ' Opaque open stream handle


  percorsoFile = "/percorso/del/file.mp3"
 
  err = avbin_init()
  If err = AVBIN_RESULT_ERROR Then Error.Raise("Impossibile inizializzare la libreria 'libavbin' !")
   
  media = avbin_open_filename(percorsoFile)
  If IsNull(media) Then Error.Raise("Impossibile aprire il file " & File.Name(percorsoFile))

' E' assolutamente necessario passare al campo ".structure_size" la dimensione della Struttura di appartenenza:
  fileinfo.structure_size = Object.SizeOf(fileinfo)

  err = avbin_file_info(media, fileinfo)
  If err = AVBIN_RESULT_ERROR Then Error.Raise("Errore alla funzione 'avbin_file_info()' !")

  With fileinfo
    Print "\nDurata del brano: "; Date(0, 0, 0, 0, 0, 0, .duration / 1000)
    If IsNull(.title.ToString()) = False Then Print "Titolo: "; .title.ToString()
    If IsNull(.author.ToString()) = False Then Print "Autore: "; .author.ToString()
    If IsNull(.copyright.ToString()) = False Then Print "Diritti d'autore: "; .copyright.ToString()
    If IsNull(.comment.ToString()) = False Then Print "Commento: "; .comment.ToString()
    If IsNull(.album.ToString()) = False Then Print "Album: "; .album.ToString()
    If .annus > 0 Then Print "Anno di produzione: "; .annus
    If .track > 0 Then Print "Num. tracce: "; .track
    If IsNull(.genre.ToString()) = False Then Print "Genere: "; .genre.ToString()
  End With

' E' assolutamente necessario passare al campo ".structure_size" la dimensione della Struttura di appartenenza:
  streaminfo.structure_size = Object.SizeOf(streaminfo)

  err = avbin_stream_info(media, stream_index, streaminfo)
  If err = AVBIN_RESULT_ERROR Then Error.Raise("Errore alla funzione 'avbin_stream_info()' !")
   
  If streaminfo.type = AVBIN_STREAM_TYPE_VIDEO Then
    Print "ATTENZIONE: Il file caricato è un file 'Video' !\nQui intendiamo invece verificare solo file mpeg audio."
  Else
    Print "\nCaratteristiche del flusso audio:"
    Print "- Frequenza di campionamento: "; streaminfo.sample_rate; " hertz"
    Print "- Risoluzione: "; streaminfo.sample_bits; " bit"
    Print "- Canali di uscita: "; streaminfo.channels
  Endif


' Va in chiusura:
  avbin_close_file(media)

End

Riferimenti

[1] Il sito di Avbin
[2] Il sito del API di Avbin