Estrarre con le funzioni esterne del API di VLC l'audio di un file video e salvarlo in un file audio

Da Gambas-it.org - Wikipedia.

Con le risorse della libreria VLC è possibile estrarre la traccia audio di un file video e di salvarla in un file audio.

E' necessario avere installata nel sistema e richiamare con Gambas la libreria condivisa: "libvlc.so.5.6.1 ".

Mostriamo un esempio pratico in ambiente grafico, nel quale da un file video .mp4 viene estratta la traccia audio, che viene quindi salvata in un file audio di formato OggVorbis:

Private bo As Boolean


Library "libvlc:5.6.1"

' libvlc_instance_t * libvlc_new (int argc, const char *const *argv)
' Create And initialize a libvlc instance.
Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer

' libvlc_media_t *libvlc_media_new_location(libvlc_instance_t *p_instance, const char * psz_mrl)
' Create a media with a certain given media resource location, for instance a valid URL.
Private Extern libvlc_media_new_location(p_instance As Pointer, path As String) As Pointer

' libvlc_media_player_t * libvlc_media_player_new(libvlc_instance_t *p_libvlc_instance)
' Create an empty Media Player object.
Private Extern libvlc_media_player_new(p_libvlc_instance As Pointer) As Pointer

' void libvlc_media_player_set_media(libvlc_media_player_t *p_mi, libvlc_media_t *p_md)
' Set the media that will be used by the media_player.
Private Extern libvlc_media_player_set_media(p_md As Pointer, mp As Pointer)

' int libvlc_media_player_play (libvlc_media_player_t * p_mi)
' Play the video file.
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer

' libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)
' Get the current movie time (in ms).
Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Integer

' void libvlc_media_player_release (libvlc_media_player_t * p_mi)
' Release a media_player after use Decrement the reference count of a media player object.
Private Extern libvlc_media_player_release(p_mi As Pointer)

' void libvlc_media_release (libvlc_media_t *p_md)
' Decrement the reference count of a media descriptor object.
Private Extern libvlc_media_release(p_md As Pointer)

' libvlc_release (libvlc_instance_t * p_instance)
' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero.
Private Extern libvlc_release(p_instance As Pointer)


Public Sub Form_Open()
 
  TextBox1.Alignment = Align.Right
  
End


Public Sub Button1_Click()
 
 Dim inst, mp, m As Pointer
 Dim ss As String[]
 Dim argc as Integer
    
' Vengono impostate le opzioni ed il percorso ove sarà salvato il file audio .ogg:
 ss = ["--sout=#transcode{acodec=vorb,ab=64,channels=2}:standard{access=file,mux=ogg,dst=/tmp/audio.ogg}"]
     
' Imposta il valore del 1° argomento della funzione "libvlc_new()":
 argc = 1
  
' Inizializza la libreria VLC:
 inst = libvlc_new(argc, ss)
  
' Crea il media player:
 mp = libvlc_media_player_new(inst)
  
' Crea un nuovo oggetto multimedia.
' Nel secondo argomento della funzione va specificato il percorso del file video .mp4, da cui ottenere il file audio .ogg:
 m = libvlc_media_new_location(inst, "file:///percorso/del/file/video.mp4")
  
 libvlc_media_player_set_media(mp, m)
 
 libvlc_media_release(m)
  
' Avvia la realizzazione del file .ogg da parte del media player:
 libvlc_media_player_play(mp)
  
 While bo = False
   TextBox1.Text = Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp)))
' Il Wait consente di agire su altri oggetti posti sul Form:
   Wait 0.01
 Wend
   
' Chiude la libreria VLC ed il programma:
 libvlc_media_player_release(mp)
 libvlc_release(inst)
 Me.Close
  
End


Public Sub Button2_Click()

' Causa l'arresto dell'estrazione dell'audio, la chiusura della libreria VLC e del programma:
 bo = True

End


Ottenere altri formati di file audio

Oltre al formato OggVorbis è possibile ottenere qualche altro formato di file audio.

Formato audio MP3

Per ottenere un file audio di formato mp3, è necessario sostituire la stringa delle opzioni con la seguente:

ss = ["--sout=#transcode{acodec=mp3,ab=128,samplerate=44100, channels=2}:standard{access=file,mux=raw,dst=/tmp/audio.mp3}"]

Formato audio WAV

Per ottenere un file audio di formato wav, è necessario sostituire la stringa delle opzioni con la seguente:

ss = ["--sout=#transcode{acodec=s16l,ab=128,samplerate=44100, channels=2}:standard{access=file,mux=wav,dst=/tmp/audio.wav}"]


Riferimenti