Eseguire i file video con le funzioni esterne del API di VLC
La libreria VLC mette a disposizione funzioni e risorse per eseguire file audio/video.
I file Video eseguibili sono i seguenti:
- AVI;
- MP4;
- MPG;
- MOV;
- VOB;
- WMV.
E' possibile anche eseguire un video direttamente da internet richiamando la funzione esterna libvlc_media_new_location( ).
Per creare in Gambas un'applicazione con la presente risorsa, si dovrà installare e richiamare la libreria condivisa: "libvlc.so.5.6.1 ".
Indice
Esempio con applicazione grafica
Mostriamo di seguito un semplice esempio, nel quale si potrà avviare, arrestare, porre in pausa e riprendere l'esecuzione del file video.
Il video sarà mostrato in una DrawingArea che porremo sul Form dell'applicazione.
Qualora non venga individuata una DrawingArea, o altro oggetto o finestra capace di supportare un video, la libreria mostrerà automaticamente il video in un'apposita e distinta finestra esterna all'applicazione.
Private inst As Pointer Private mp As Pointer Private m As Pointer Library "libvlc:5.6.1" Private Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing, libvlc_Paused, libvlc_Stopped, libvlc_Ended, libvlc_Error ' 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_path (libvlc_instance_t *p_instance, const char *path) ' Create a media for a certain file path. Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer ' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md) ' Create a Media Player object from a Media. Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer ' void libvlc_media_player_set_xwindow (libvlc_media_player_t *p_mi, uint32_t drawable) ' Set an X Window System drawable where the media player should render its video output. Private Extern libvlc_media_player_set_xwindow(p_mi As Pointer, drawable As Integer) ' 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 ' void libvlc_media_player_stop (libvlc_media_player_t * p_mi) ' Stop the video file Private Extern libvlc_media_player_stop(p_mi As Pointer) ' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *) ' Get the current movie length (in ms). Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex 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 ' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi) ' Get current movie state. Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer ' void libvlc_media_player_pause (libvlc_media_player_t *p_mi) ' Toggle pause. Private Extern libvlc_media_player_pause(p_mi As Pointer) ' int libvlc_audio_set_volume(libvlc_media_player_t *p_mi, int i_volume) ' Set current software audio volume. Private Extern libvlc_audio_set_volume(p_mi As Pointer, i_volume As Integer) 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() With Slider1 .MaxValue = 100 .MinValue = 0 .Value = 50 End With End Public Sub Button1_Click() ' Inizializza la libreria VLC: inst = libvlc_new(0, Null) ' Crea un nuovo oggetto multimedia. ' Nel secondo argomento della funzione va specificato il percorso del file video: m = libvlc_media_new_path(inst, "/percorso/del/file/video") ' Se si vuole eseguire, invece, un video direttamente da internet sarà necessario usare la funzione esterna "libvlc_media_new_location()" ' Esempio pratico di una web-tv: ' m = libvlc_media_new_location(inst, "http://web.onair-radio.eu:1935/Alpha-Host/Alpha-Host/playlist.m3u8") ' Crea un media player: mp = libvlc_media_player_new_from_media(m) ' Passa l'identificativo della finestra, nella quale dovrà essere mostrato il video: libvlc_media_player_set_xwindow(mp, DrawingArea1.Id) ' Imposta il valore iniziale del volume in base al valore iniziale dello "Slider": libvlc_audio_set_volume(mp, Slider1.Value) ' Avvia l'esecuzione del file video da parte del media player: libvlc_media_player_play(mp) Repeat Wait 0.01 Until libvlc_media_player_get_length(mp, 0) > 0.0 Repeat TextLabel1.Text = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) TextLabel2.Text = "<FONT Color=red>" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) ProgressBar1.Value = libvlc_media_player_get_time(mp) / libvlc_media_player_get_length(mp, 0) Wait 0.01 Until libvlc_media_player_get_state(mp) > libvlc_Paused Chiude() End Public Sub Button2_Click() ' Arresta l'esecuzione del file video: libvlc_media_player_stop(mp) DrawingArea1.Clear End Private Procedure Chiude() ' Chiude la libreria VLC: libvlc_media_player_release(mp) libvlc_media_release(m) libvlc_release(inst) End Public Sub ToggleButton1_Click() ' Pone in pausa o riprende (se già in pausa) l'esecuzione del video: libvlc_media_player_pause(mp) End Public Sub Slider1_Change() ' Modifica il volume audio: If mp > 0 Then libvlc_audio_set_volume(mp, Slider1.Value) End
Spostare l'esecuzione del video nell'ambito della sua durata
Si può avanzare o retrocedere all'interno dell'esecuzione di un file video mediante la funzione esterna "libvlc_media_player_set_time()".
Innanzitutto si dichiarerà formalmente la funzione esterna "libvlc_media_player_set_time()" di libvlc:
' void libvlc_media_player_set_time (libvlc_media_player_t *p_mi, libvlc_time_t i_time) ' Set the movie time (in ms). Private Extern libvlc_media_player_set_time(player As Pointer, i_time As Long)
Volendo usare uno Slider, imposteremo i valori iniziali dello Slider, dedicato allo spostamento dell'esecuzione del video, nella routine:
Public Sub Form_Open() ...... With Slider2 .MaxValue = 1000 .MinValue = 0 .Value = 0 End With End
Inoltre imposteremo una specifica routine per lo Slider dedicato allo spostamento dell'esecuzione del video:
Public Sub Slider2_Change() ' Sposta l'esecuzione del video nell'ambito della sua durata: libvlc_media_player_set_time(mp, (Slider2.Value * libvlc_media_player_get_length(mp, 0)) / 1000) End
Far variare la posizione del cursore dello Slider in base all'esecuzione
Se si preferisce far variare la posizione del cursore dello Slider in base al punto, all'evolversi dell'esecuzione del file video, e poter arbitrariamente far avanzare e retrocedere il punto dell'esecuzione muovendo il cursore dello Slider, allora potrà adottarsi un codice come il seguente:
Private inst As Pointer Private mp As Pointer Private m As Pointer Private bo As Boolean Library "libvlc:5.6.1" Private Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing, libvlc_Paused, libvlc_Stopped, libvlc_Ended, libvlc_Error ' 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_path (libvlc_instance_t *p_instance, const char *path) ' Create a media for a certain file path. Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer ' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md) ' Create a Media Player object from a Media. Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer ' void libvlc_media_player_set_xwindow (libvlc_media_player_t *p_mi, uint32_t drawable) ' Set an X Window System drawable where the media player should render its video output. Private Extern libvlc_media_player_set_xwindow(p_mi As Pointer, drawable As Integer) ' 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 ' void libvlc_media_player_stop (libvlc_media_player_t * p_mi) ' Stop the video file. Private Extern libvlc_media_player_stop(p_mi As Pointer) ' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *) ' Get the current movie length (in ms). Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex 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 ' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi) ' Get current movie state. Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer ' void libvlc_media_player_pause (libvlc_media_player_t *p_mi) ' Toggle pause. Private Extern libvlc_media_player_pause(p_mi As Pointer) ' void libvlc_media_player_set_time (libvlc_media_player_t *p_mi, libvlc_time_t i_time) ' Set the movie time (in ms). Private Extern libvlc_media_player_set_time(player As Pointer, i_time As Long) ' 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) ' void 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() With Slider1 .MaxValue = 100 .MinValue = 0 .Value = 0 End With End Public Sub Button1_Click() Slider1.Value = 0 inst = libvlc_new(0, Null) m = libvlc_media_new_path(inst, "/percorso/del/file/video") mp = libvlc_media_player_new_from_media(m) libvlc_media_player_set_xwindow(mp, DrawingArea1.Id) libvlc_media_player_play(mp) Repeat Wait 0.01 Until libvlc_media_player_get_length(mp, 0) > 0.0 Repeat Me.Title = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) & " - " & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) ProgressBar1.Value = libvlc_media_player_get_time(mp) / libvlc_media_player_get_length(mp, 0) If Not bo Then Slider1.Value = ProgressBar1.Value * 100 Wait 0.01 Until libvlc_media_player_get_state(mp) > libvlc_Paused Chiude() End Public Sub Button2_Click() libvlc_media_player_stop(mp) DrawingArea1.Clear End Private Procedure Chiude() libvlc_media_player_release(mp) libvlc_media_release(m) libvlc_release(inst) End Public Sub ToggleButton1_Click() libvlc_media_player_pause(mp) End Public Sub Slider1_MouseDown() bo = True End Public Sub Slider1_MouseUp() bo = False libvlc_media_player_set_time(mp, (Slider1.Value * libvlc_media_player_get_length(mp, 0)) / 100) End
Esempio con applicazione a riga di comando
Con un'applicazione a riga di comando la libreria VLC provvederà autonomamente a generare una finestra, nella quale eseguire il video:
Library "libvlc:5.6.1" Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing, libvlc_Paused, libvlc_Stopped, libvlc_Ended, libvlc_Error ' 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_path (libvlc_instance_t *p_instance, const char *path) ' Create a media for a certain file path. Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer ' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md) ' Create a Media Player object from a Media. Private Extern libvlc_media_player_new_from_media(p_md As Pointer) 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 ' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi) ' Get current movie state. Private Extern libvlc_media_player_get_state(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 Main() Dim inst, m, mp As Pointer ' Inizializza la libreria VLC: inst = libvlc_new(0, Null) ' Crea un nuovo oggetto multimedia. ' Nel secondo argomento della funzione va specificato il percorso del file video: m = libvlc_media_new_path(inst, "/percorso/del/file/video") ' Se si vuole eseguire, invece un video direttamente da internet sarà necessario usare la funzione esterna "libvlc_media_new_location()" ' Esempio pratico di una web-tv: ' m = libvlc_media_new_location(inst, "http://web.onair-radio.eu:1935/Alpha-Host/Alpha-Host/playlist.m3u8") ' Crea un media player: mp = libvlc_media_player_new_from_media(m) ' Avvia l'esecuzione del file video da parte del media player: libvlc_media_player_play(mp) While libvlc_media_player_get_state(mp) < libvlc_Stopped Write "\r" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) Wait 0.01 Wend ' Va in chiusura: libvlc_media_player_release(mp) libvlc_media_release(m) libvlc_release(inst) End
Se si vuole consentire l'arresto arbitrario dell'esecuzione del file video, allora si dovrà rendere "Globale" la variabile Puntatore, chiamata "mp", e la parte finale del codice dovrà essere così modificata e integrata:
...... While (libvlc_media_player_get_state(mp) <> libvlc_Stopped) And (libvlc_media_player_get_state(mp) <> libvlc_Ended) Write "\r \e[31m" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) Wait 0.01 Wend ' Chiude il media player, la libreria VLC e il programma: libvlc_media_player_release(mp) libvlc_release(inst) ' Avendo usato l'Evento "Application_Read()", è necessario usare l'istruzione "Quit" per chiudere il programma: Quit End
Gestione dei filtri video
La libreria VLC possiede risorse utili a gestire i filtri video/immagine, dei tipi previsti nell'enumerazione libvlc_video_adjust_option_t:
enum libvlc_video_adjust_option_t { libvlc_adjust_Enable = 0, libvlc_adjust_Contrast, libvlc_adjust_Brightness, libvlc_adjust_Hue, libvlc_adjust_Saturation, libvlc_adjust_Gamma }
In particolare il primo valore (libvlc_adjust_Enable) dovrà essere passato come 2° parametro alla funzione esterna libvlc_video_set_adjust_int() per attivare o disattivare la possibilità di modificare i valori di filtro.
Dunque, in ordine alle funzioni esterne bisognerà adoperare la predetta funzione libvlc_video_set_adjust_int() e la funzione libvlc_video_set_adjust_float().
Si è già visto che la prima funzione (libvlc_video_set_adjust_int()) consente di modificare i filtri. In tal caso nel 2° argomento va impostato - come già visto - il valore della costante libvlc_adjust_Enable, e nel 3° argomento va attribuito il valore 1. Per impedire la modifica di un filtro il valore del 3° parametro va impostato a zero.
La seconda funzione di VLC libvlc_video_set_adjust_float() individua quale filtro specifico deve essere applicato al video e quale valore va attribuito alla modifica. Il valore del 2° parametro va impostato con il valore del filtro da modificare tra quelli compresi nella enumerazione libvlc_video_adjust_option_t. Il valore del 3° parametro può assumere un valore compreso nell'ambito 0.0 - 2.0 .
Mostriamo un esempio pratico con applicazione grafica, nella quale si applicherà al video il filtro della luminosità (Brightness), il cui valore potrà essere modificato (da 0.0 a 2.0) con uno Slider.
Private inst As Pointer Private mp As Pointer Library "libvlc:5.6.1" Private Enum libvlc_NothingSpecial = 0, libvlc_Opening, libvlc_Buffering, libvlc_Playing, libvlc_Paused, libvlc_Stopped, libvlc_Ended, libvlc_Error Private Enum libvlc_adjust_Enable = 0, libvlc_adjust_Contrast, libvlc_adjust_Brightness, libvlc_adjust_Hue, libvlc_adjust_Saturation, libvlc_adjust_Gamma ' 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_path (libvlc_instance_t *p_instance, const char *path) ' Create a media for a certain file path. Private Extern libvlc_media_new_path(p_instance As Pointer, path As String) As Pointer ' libvlc_media_player_t * libvlc_media_player_new_from_media (libvlc_media_t *p_md) ' Create a Media Player object from a Media. Private Extern libvlc_media_player_new_from_media(p_md As Pointer) As Pointer ' void libvlc_media_player_set_xwindow (libvlc_media_player_t *p_mi, uint32_t drawable) ' Set an X Window System drawable where the media player should render its video output. Private Extern libvlc_media_player_set_xwindow(p_mi As Pointer, drawable As Integer) ' 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 ' void libvlc_media_player_stop (libvlc_media_player_t * p_mi) ' Stop the video file Private Extern libvlc_media_player_stop(p_mi As Pointer) ' libvlc_time_t libvlc_media_player_get_length(libvlc_media_player_t *, libvlc_exception_t *) ' Get the current movie length (in ms). Private Extern libvlc_media_player_get_length(p_mi As Pointer, l_ex 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 ' libvlc_state_t libvlc_media_player_get_state(libvlc_media_player_t *p_mi) ' Get current movie state. Private Extern libvlc_media_player_get_state(p_mi As Pointer) As Integer ' void libvlc_video_set_adjust_int (libvlc_media_player_t *p_mi, unsigned option, int value) ' Set adjust option as integer. Private Extern libvlc_video_set_adjust_int(p_mi As Pointer, option As Integer, value As Integer) ' void libvlc_video_set_adjust_float (libvlc_media_player_t *p_mi, unsigned option, float value) ' Set adjust option as float. Private Extern libvlc_video_set_adjust_float(p_mi As Pointer, option As Integer, value As Single) ' 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() With Slider1 .MaxValue = 200 .MinValue = 0 .Value = 100 .Enabled = False End With End Public Sub Button1_Click() Dim m As Pointer Dim id As Integer Slider1.Enabled = True ' Inizializza la libreria VLC: inst = libvlc_new(0, Null) ' Crea un nuovo oggetto multimedia. ' Nel secondo argomento della funzione va specificato il percorso del file video. ' Se si vuole eseguire,invece, un video direttamente da internet sarà necessario usare la funzione esterna "libvlc_media_new_location()": m = libvlc_media_new_path(inst, "/percorso/del/file/video") ' Crea un media player: mp = libvlc_media_player_new_from_media(m) ' Per far mostrare il video nella "DrawingArea", ricaviamo il suo identificativo: id = DrawingArea1.Id ' Passiamo l'identificativo della finestra, nella quale dovrà essere mostrato il video: libvlc_media_player_set_xwindow(mp, id) ' Avvia l'esecuzione del file video da parte del media player: libvlc_media_player_play(mp) Repeat TextLabel1.Text = "Durata: " & Str(Time(0, 0, 0, libvlc_media_player_get_length(mp, 0))) TextLabel2.Text = "<FONT Color=red>" & Str(Time(0, 0, 0, libvlc_media_player_get_time(mp))) Wait 0.01 Until libvlc_media_player_get_state(mp) > libvlc_Paused Chiude() End Public Sub Slider1_Change() If mp = 0 Then Return ' Attiva la modifica dei filtri: libvlc_video_set_adjust_int(mp, libvlc_adjust_Enable, 1) ' Imposta la modifica del filtro "Luminosità" secondo il variare del valore dello "Slider": libvlc_video_set_adjust_float(mp, libvlc_adjust_Brightness, CSingle(Slider1.Value / 100)) End Public Sub Button2_Click() ' Arresta l'esecuzione del file video: libvlc_media_player_stop(mp) End Private Procedure Chiude() ' Chiude la libreria VLC: libvlc_media_player_release(mp) libvlc_media_release(m) libvlc_release(inst) End Public Sub ToggleButton1_Click() ' Pone in pausa o riprende (se già in pausa) l'esecuzione del video: libvlc_media_player_pause(mp) End