Differenze tra le versioni di "Eseguire un file Video con le funzioni esterne del API di Libmpv"

Da Gambas-it.org - Wikipedia.
(Creata pagina con "La libreria esterna '''Libmpv''' consente di eseguire diversi formati di file video. Per poter utilizzare le risorse di ''Mpv'', è necessario avere installata e richiamare i...")
 
 
(16 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
La libreria esterna '''Libmpv''' consente di eseguire diversi formati di file video.
 
La libreria esterna '''Libmpv''' consente di eseguire diversi formati di file video.
  
Per poter utilizzare le risorse di ''Mpv'', è necessario avere installata e richiamare in Gambas la libreria condivisa: "''libmpv.so.1.107.0''"
+
Per poter utilizzare le risorse di ''Mpv'', è necessario avere installata e richiamare in Gambas la libreria condivisa: "''libmpv.so.1.109.0'' ".
  
 
Mostriamo un semplice esempio:
 
Mostriamo un semplice esempio:
  Library "libmpv:1.107.0"
+
Library "libc:6"
 +
 +
Private Const LC_NUMERIC As Integer = 1
 +
 +
<FONT Color=gray>' ''char *setlocale (int __category, const char *__locale)''
 +
' ''Set and/or return the current locale.''</font>
 +
Private Extern setlocale(__category As Integer, __locale As String) As Pointer
 +
 +
 +
  Library "libmpv:1.109.0"
 
   
 
   
 
  Private Enum MPV_FORMAT_NONE = 0, MPV_FORMAT_STRING, MPV_FORMAT_OSD_STRING, MPV_FORMAT_FLAG,
 
  Private Enum MPV_FORMAT_NONE = 0, MPV_FORMAT_STRING, MPV_FORMAT_OSD_STRING, MPV_FORMAT_FLAG,
Riga 13: Riga 22:
 
  ' ''Create a new mpv instance.''</font>
 
  ' ''Create a new mpv instance.''</font>
 
  Private Extern mpv_create() As Pointer
 
  Private Extern mpv_create() As Pointer
 
<FONT Color=gray>' ''int mpv_set_option_string(mpv_handle *ctx, const char *name, const char *data)''
 
' ''Convenience function to set an option to a string value.''</font>
 
Private Extern mpv_set_option_string(ctx As Pointer, name As String, data As String) As Integer
 
 
<FONT Color=gray>' ''int mpv_set_option(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 
' ''Set an option.''</font>
 
Private Extern mpv_set_option(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer
 
 
   
 
   
 
  <FONT Color=gray>' ''int mpv_initialize(mpv_handle *ctx)''
 
  <FONT Color=gray>' ''int mpv_initialize(mpv_handle *ctx)''
Riga 29: Riga 30:
 
  ' ''Send a command to the player.''</font>
 
  ' ''Send a command to the player.''</font>
 
  Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer
 
  Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_set_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 +
' ''Set a property.''</font>
 +
Private Extern mpv_set_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 
  <FONT Color=gray>' ''int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 
  ' ''Read the value of the given property.''</font>
 
  ' ''Read the value of the given property.''</font>
  Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer)
+
  Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''void mpv_terminate_destroy(mpv_handle *ctx)''
 
  <FONT Color=gray>' ''void mpv_terminate_destroy(mpv_handle *ctx)''
Riga 39: Riga 44:
 
   
 
   
 
   
 
   
 +
'''Public''' Sub Main()
 +
 
 +
  Dim mpv As Pointer
 +
  Dim fileaudio As String
 +
  Dim d, l, v As Long
 +
 
 +
  fileaudio = "<FONT Color=gray>''/percorso/del/file/video''</font>"
 +
  Print "File audio: "; fileaudio
 +
  Print
 +
 
 +
  setlocale(LC_NUMERIC, "C")
 +
 
 +
  mpv = mpv_create()
 +
  If mpv == 0 Then Error.Raise("Errore !")
 +
 
 +
  mpv_initialize(mpv)
 +
 
 +
  mpv_command(mpv, ["loadfile", fileaudio, Null])
 +
 
 +
<FONT Color=gray>' ''Imposta il volume. 0 significa silenzio; 100 significa nessuna riduzione del volume o amplificazione rispetto al valore originario. Valori superiori a 100 amplificano il volume di uscita rispetto al valore originario.''</font>
 +
  v = 50
 +
  mpv_set_property(mpv, "volume", MPV_FORMAT_INT64, VarPtr(v))
 +
 
 +
  Repeat
 +
    mpv_get_property(mpv, "duration", MPV_FORMAT_INT64, VarPtr(d))
 +
  Until d > 0 <FONT Color=gray>' ''Si esce dal ciclo, solo quando sarà rilevata la durata del file video''</font>
 +
 
 +
  Repeat
 +
    mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(l))
 +
    Write "\e[0m\rDurata: " & Time(0, 0, 0, CInt(d) * 1000) &
 +
          "  Tempo trascorso: \e[31m" & Time(0, 0, 0, CInt(l) * 1000)
 +
    Wait 0.001
 +
  Until l >= d
 +
 
 +
  mpv_terminate_destroy(mpv)
 +
 
 +
'''End'''
 +
 +
 +
Un'altra soluzione simile per ottenere alla fine dell'esecuzione la chiusura del programma:
 
  Library "libc:6"
 
  Library "libc:6"
 
   
 
   
Riga 46: Riga 91:
 
  ' ''Set and/or return the current locale.''</font>
 
  ' ''Set and/or return the current locale.''</font>
 
  Private Extern setlocale(__category As Integer, __locale As String) As Pointer
 
  Private Extern setlocale(__category As Integer, __locale As String) As Pointer
 +
 +
 +
Library "libmpv:1.109.0"
 +
 +
Public Struct mpv_event
 +
  event_id As Integer
 +
  error_ As Integer
 +
  reply_userdata As Long
 +
  data As Pointer
 +
End Struct
 +
 +
Private Const MPV_FORMAT_INT64 As Integer = 4
 +
Private Const MPV_EVENT_END_FILE As Integer = 7
 +
 +
<FONT Color=gray>' ''mpv_handle *mpv_create(void)''
 +
' ''Create a new mpv instance.''</font>
 +
Private Extern mpv_create() As Pointer
 +
 +
<FONT Color=gray>' ''int mpv_initialize(mpv_handle *ctx)''
 +
' ''Initialize an uninitialized mpv instance.''</font>
 +
Private Extern mpv_initialize(ctx As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_command(mpv_handle *ctx, const char **args)''
 +
' ''Send a command to the player.''</font>
 +
Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_set_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 +
' ''Set a property.''</font>
 +
Private Extern mpv_set_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 +
' ''Read the value of the given property.''</font>
 +
Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''mpv_event *mpv_wait_event(mpv_handle *ctx, double timeout)''
 +
' ''Wait for the next event, or until the timeout expires.''</font>
 +
Private Extern mpv_wait_event(ctx As Pointer, timeout As Float) As Mpv_event
 +
 +
<FONT Color=gray>' ''void mpv_terminate_destroy(mpv_handle *ctx)''
 +
' ''Disconnect and destroy the mpv_handle.''</font>
 +
Private Extern mpv_terminate_destroy(ctx As Pointer)
 
   
 
   
 
   
 
   
Riga 51: Riga 137:
 
    
 
    
 
   Dim mpv As Pointer
 
   Dim mpv As Pointer
   Dim filevideo As String
+
   Dim err As Integer
   Dim d, l As Long
+
  Dim v As Long
 +
  Dim cmd As String[] = ["loadfile", Null, Null]
 +
   Dim ev As Mpv_event
 
    
 
    
   filevideo = "<FONT Color=gray>''/percorso/del/file/audio''</font>"
+
   cmd[1] = "<FONT Color=gray>''/percorso/del/file/video''</font>"
  Print "File video: "; filevideo
 
  Print
 
 
    
 
    
 
   setlocale(LC_NUMERIC, "C")
 
   setlocale(LC_NUMERIC, "C")
Riga 63: Riga 149:
 
   If mpv == 0 Then Error.Raise("Errore !")
 
   If mpv == 0 Then Error.Raise("Errore !")
 
    
 
    
   mpv_initialize(mpv)
+
   err = mpv_initialize(mpv)
 +
  If err < 0 Then
 +
    Terminus(mpv)
 +
    Error.Raise("Errore !")
 +
  Endif
 +
 
 +
  err = mpv_command(mpv, cmd)
 +
  If err < 0 Then
 +
    Terminus(mpv)
 +
    Error.Raise("Errore !")
 +
  Endif
 
    
 
    
   mpv_command(mpv, ["loadfile", filevideo, Null])
+
<FONT Color=gray>' ''Imposta il Volume:''
 +
' ''   0 = muto;''
 +
' '' 100 = nessuna variazione di volume;''
 +
' ''>100 = aumento del volume rispetto al valore originario.''</font>
 +
  v = 50
 +
  mpv_set_property(mpv, "volume", MPV_FORMAT_INT64, VarPtr(v))
 
    
 
    
 
   Repeat
 
   Repeat
     mpv_get_property(mpv, "duration", MPV_FORMAT_INT64, VarPtr(d))
+
<FONT Color=gray>' ''Acquisisce la corrente posizione del video espressa in secondi trascorsi dall'inizio dell'esecuzione:'' </font>
     Wait 0.01
+
     mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(v))
   Until d > 0
+
     Write "\r\e[31m" & Str(Time(0, 0, 0, v * 1000))
 +
    Flush
 +
    ev = mpv_wait_event(mpv, 1)
 +
  Until ev.event_id == MPV_EVENT_END_FILE  <FONT Color=gray>' ''Il ciclo termina, quando termina l'esecuzione del file video''</font>
 +
 
 +
  Terminus(mpv)
 +
 
 +
'''End'''
 +
 +
'''Private''' Procedure Terminus(po As Pointer)
 +
 +
  mpv_terminate_destroy(po)
 +
 +
'''End'''
 +
 
 +
 
 +
===Con applicazione grafica mostrando il video in una DrawingArea posta sul Form===
 +
Nel seguente codice si useranno risorse grafiche e si farà mostrare il video all'interno di una "DrawingArea" posta sul "Form" principale.
 +
Library "libc:6"
 +
 +
Private Const LC_NUMERIC As Integer = 1
 +
 +
<FONT Color=gray>' ''char *setlocale (int __category, const char *__locale)''
 +
' ''Set and/or return the current locale.''</font>
 +
Private Extern setlocale(__category As Integer, __locale As String) As Pointer
 +
 +
 +
Library "libmpv:1.109.0"
 +
 +
Public Struct mpv_event
 +
  event_id As Integer
 +
  error_ As Integer
 +
  reply_userdata As Long
 +
  data As Pointer
 +
End Struct
 +
 +
Private Enum MPV_FORMAT_INT64 = 4
 +
Private Const MPV_EVENT_END_FILE As Integer = 7
 +
 +
<FONT Color=gray>' ''mpv_handle *mpv_create(void)''
 +
' ''Create a new mpv instance.''</font>
 +
Private Extern mpv_create() As Pointer
 +
 +
<FONT Color=gray>' ''int mpv_initialize(mpv_handle *ctx)''
 +
' ''Initialize an uninitialized mpv instance.''</font>
 +
Private Extern mpv_initialize(ctx As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_command(mpv_handle *ctx, const char **args)''
 +
' ''Send a command to the player.''</font>
 +
Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_set_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 +
' ''Set a property.''</font>
 +
Private Extern mpv_set_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_set_option_string(mpv_handle *ctx, const char *name, const char *data)''
 +
' ''Set an option to a string value.''</font>
 +
Private Extern mpv_set_option_string(ctx As Pointer, Name As String, Data As String) As Integer
 +
 +
<FONT Color=gray>' ''int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)''
 +
' ''Read the value of the given property.''</font>
 +
Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''mpv_event *mpv_wait_event(mpv_handle *ctx, double timeout)''
 +
' ''Wait for the next event, or until the timeout expires.''</font>
 +
Private Extern mpv_wait_event(ctx As Pointer, timeout As Float) As Mpv_event
 +
 +
<FONT Color=gray>' ''void mpv_terminate_destroy(mpv_handle *ctx)''
 +
' ''Disconnect and destroy the mpv_handle.''</font>
 +
Private Extern mpv_terminate_destroy(ctx As Pointer)
 +
 +
 +
'''Public''' Sub Button1_Click()
 +
 
 +
  Dim mpv As Pointer
 +
  Dim err As Integer
 +
  Dim cmd As String[] = ["loadfile", Null, Null]
 +
  Dim v As Long
 +
  Dim ev As Mpv_event
 +
 
 +
  cmd[1] = "<FONT Color=gray>''/percorso/del/file/video''</font>"
 +
 
 +
  setlocale(LC_NUMERIC, "C")
 +
 
 +
  mpv = mpv_create()
 +
  If mpv == 0 Then Error.Raise("Error !")
 +
 
 +
   err = mpv_initialize(mpv)
 +
  If err < 0 Then
 +
    Terminus(mpv)
 +
    Error.Raise("Error !")
 +
  Endif
 +
 
 +
  err = mpv_command(mpv, cmd)
 +
  If err < 0 Then
 +
    Terminus(mpv)
 +
    Error.Raise("Error !")
 +
  Endif
 +
 
 +
<FONT Color=gray>' ''Imposta il Volume:''
 +
' ''  0 = muto;''
 +
' '' 100 = nessuna variazione di volume;''
 +
' ''>100 = aumento del volume rispetto al valore originario.''</font>
 +
  v = 50
 +
  mpv_set_property(mpv, "volume", MPV_FORMAT_INT64, VarPtr(v))
 +
 
 +
<FONT Color=gray>' ''Imposta la superficie grafica (in questo caso la "DrawingArea" posta sul "Form"), ove mostrare il video:'' </font>
 +
  mpv_set_option_string(mpv, "wid", CStr(DrawingArea1.Id))
 
    
 
    
 
   Repeat
 
   Repeat
     mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(l))
+
<FONT Color=gray>' ''Acquisisce la corrente posizione del video espressa in secondi trascorsi dall'inizio dell'esecuzione:'' </font>
     Write "\e[0m\rDurata: " & Date(0, 0, 0, 0, 0, 0, CInt(d) * 1000) &
+
     mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(v))
          "  Tempo trascorso: \e[31m" & Date(0, 0, 0, 0, 0, 0, CInt(l) * 1000)
+
     Write "\r\e[31m" & Str(Time(0, 0, 0, v * 1000))
    Wait 0.001
+
    Flush
   Until l >= d
+
    ev = mpv_wait_event(mpv, 1)
 +
  Until ev.event_id == MPV_EVENT_END_FILE   <FONT Color=gray>' ''Il ciclo termina, quando termina l'esecuzione del file video''</font>
 
    
 
    
   mpv_terminate_destroy(mpv)
+
   Terminus(mpv)
 
    
 
    
 
  '''End'''
 
  '''End'''
 
+
 +
'''Private''' Procedure Terminus(po As Pointer)
 +
 +
  mpv_terminate_destroy(po)
 +
 +
'''End'''
 +
  
  
 
=Riferimenti=
 
=Riferimenti=
 
* https://mpv.io/manual/
 
* https://mpv.io/manual/
 +
* https://github.com/mpv-player/mpv/tree/master/DOCS/man

Versione attuale delle 08:07, 24 apr 2023

La libreria esterna Libmpv consente di eseguire diversi formati di file video.

Per poter utilizzare le risorse di Mpv, è necessario avere installata e richiamare in Gambas la libreria condivisa: "libmpv.so.1.109.0 ".

Mostriamo un semplice esempio:

Library "libc:6"

Private Const LC_NUMERIC As Integer = 1

' char *setlocale (int __category, const char *__locale)
' Set and/or return the current locale.
Private Extern setlocale(__category As Integer, __locale As String) As Pointer


Library "libmpv:1.109.0"

Private Enum MPV_FORMAT_NONE = 0, MPV_FORMAT_STRING, MPV_FORMAT_OSD_STRING, MPV_FORMAT_FLAG,
             MPV_FORMAT_INT64, MPV_FORMAT_DOUBLE, MPV_FORMAT_NODE, MPV_FORMAT_NODE_ARRAY,
             MPV_FORMAT_NODE_MAP, MPV_FORMAT_BYTE_ARRAY

' mpv_handle *mpv_create(void)
' Create a new mpv instance.
Private Extern mpv_create() As Pointer

' int mpv_initialize(mpv_handle *ctx)
' Initialize an uninitialized mpv instance.
Private Extern mpv_initialize(ctx As Pointer) As Integer

' int mpv_command(mpv_handle *ctx, const char **args)
' Send a command to the player.
Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer

' int mpv_set_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)
' Set a property.
Private Extern mpv_set_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer

' int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)
' Read the value of the given property.
Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer

' void mpv_terminate_destroy(mpv_handle *ctx)
' Disconnect and destroy the mpv_handle.
Private Extern mpv_terminate_destroy(ctx As Pointer)


Public Sub Main()
 
 Dim mpv As Pointer
 Dim fileaudio As String
 Dim d, l, v As Long
 
 fileaudio = "/percorso/del/file/video"
 Print "File audio: "; fileaudio
 Print
 
 setlocale(LC_NUMERIC, "C")
 
 mpv = mpv_create()
 If mpv == 0 Then Error.Raise("Errore !")
 
 mpv_initialize(mpv)
 
 mpv_command(mpv, ["loadfile", fileaudio, Null])
 
' Imposta il volume. 0 significa silenzio; 100 significa nessuna riduzione del volume o amplificazione rispetto al valore originario. Valori superiori a 100 amplificano il volume di uscita rispetto al valore originario.
 v = 50
 mpv_set_property(mpv, "volume", MPV_FORMAT_INT64, VarPtr(v))
 
 Repeat
   mpv_get_property(mpv, "duration", MPV_FORMAT_INT64, VarPtr(d))
 Until d > 0 ' Si esce dal ciclo, solo quando sarà rilevata la durata del file video
 
 Repeat
   mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(l))
   Write "\e[0m\rDurata: " & Time(0, 0, 0, CInt(d) * 1000) &
         "   Tempo trascorso: \e[31m" & Time(0, 0, 0, CInt(l) * 1000)
   Wait 0.001
 Until l >= d
 
 mpv_terminate_destroy(mpv)
 
End


Un'altra soluzione simile per ottenere alla fine dell'esecuzione la chiusura del programma:

Library "libc:6"

Private Const LC_NUMERIC As Integer = 1

' char *setlocale (int __category, const char *__locale)
' Set and/or return the current locale.
Private Extern setlocale(__category As Integer, __locale As String) As Pointer


Library "libmpv:1.109.0"

Public Struct mpv_event
  event_id As Integer
  error_ As Integer
  reply_userdata As Long
  data As Pointer
End Struct

Private Const MPV_FORMAT_INT64 As Integer = 4
Private Const MPV_EVENT_END_FILE As Integer = 7

' mpv_handle *mpv_create(void)
' Create a new mpv instance.
Private Extern mpv_create() As Pointer

' int mpv_initialize(mpv_handle *ctx)
' Initialize an uninitialized mpv instance.
Private Extern mpv_initialize(ctx As Pointer) As Integer

' int mpv_command(mpv_handle *ctx, const char **args)
' Send a command to the player.
Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer

' int mpv_set_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)
' Set a property.
Private Extern mpv_set_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer

' int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)
' Read the value of the given property.
Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer

' mpv_event *mpv_wait_event(mpv_handle *ctx, double timeout)
' Wait for the next event, or until the timeout expires.
Private Extern mpv_wait_event(ctx As Pointer, timeout As Float) As Mpv_event

' void mpv_terminate_destroy(mpv_handle *ctx)
' Disconnect and destroy the mpv_handle.
Private Extern mpv_terminate_destroy(ctx As Pointer)


Public Sub Main()
 
 Dim mpv As Pointer
 Dim err As Integer
 Dim v As Long
 Dim cmd As String[] = ["loadfile", Null, Null]
 Dim ev As Mpv_event
 
 cmd[1] = "/percorso/del/file/video"
 
 setlocale(LC_NUMERIC, "C")
 
 mpv = mpv_create()
 If mpv == 0 Then Error.Raise("Errore !")
 
 err = mpv_initialize(mpv)
 If err < 0 Then
   Terminus(mpv)
   Error.Raise("Errore !")
 Endif
 
 err = mpv_command(mpv, cmd)
 If err < 0 Then
   Terminus(mpv)
   Error.Raise("Errore !")
 Endif
 
' Imposta il Volume: 
'    0 = muto;
'  100 = nessuna variazione di volume;
' >100 = aumento del volume rispetto al valore originario.
 v = 50
 mpv_set_property(mpv, "volume", MPV_FORMAT_INT64, VarPtr(v))
 
 Repeat
' Acquisisce la corrente posizione del video espressa in secondi trascorsi dall'inizio dell'esecuzione: 
   mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(v))
   Write "\r\e[31m" & Str(Time(0, 0, 0, v * 1000))
   Flush
   ev = mpv_wait_event(mpv, 1)
 Until ev.event_id == MPV_EVENT_END_FILE   ' Il ciclo termina, quando termina l'esecuzione del file video
 
 Terminus(mpv)
 
End

Private Procedure Terminus(po As Pointer)

 mpv_terminate_destroy(po)

End


Con applicazione grafica mostrando il video in una DrawingArea posta sul Form

Nel seguente codice si useranno risorse grafiche e si farà mostrare il video all'interno di una "DrawingArea" posta sul "Form" principale.

Library "libc:6"

Private Const LC_NUMERIC As Integer = 1

' char *setlocale (int __category, const char *__locale)
' Set and/or return the current locale.
Private Extern setlocale(__category As Integer, __locale As String) As Pointer


Library "libmpv:1.109.0"

Public Struct mpv_event
  event_id As Integer
  error_ As Integer
  reply_userdata As Long
  data As Pointer
End Struct

Private Enum MPV_FORMAT_INT64 = 4
Private Const MPV_EVENT_END_FILE As Integer = 7

' mpv_handle *mpv_create(void)
' Create a new mpv instance.
Private Extern mpv_create() As Pointer

' int mpv_initialize(mpv_handle *ctx)
' Initialize an uninitialized mpv instance.
Private Extern mpv_initialize(ctx As Pointer) As Integer

' int mpv_command(mpv_handle *ctx, const char **args)
' Send a command to the player.
Private Extern mpv_command(ctx As Pointer, args As String[]) As Integer

' int mpv_set_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)
' Set a property.
Private Extern mpv_set_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer

' int mpv_set_option_string(mpv_handle *ctx, const char *name, const char *data)
' Set an option to a string value.
Private Extern mpv_set_option_string(ctx As Pointer, Name As String, Data As String) As Integer

' int mpv_get_property(mpv_handle *ctx, const char *name, mpv_format format, void *data)
' Read the value of the given property.
Private Extern mpv_get_property(ctx As Pointer, name As String, format_ As Integer, data As Pointer) As Integer

' mpv_event *mpv_wait_event(mpv_handle *ctx, double timeout)
' Wait for the next event, or until the timeout expires.
Private Extern mpv_wait_event(ctx As Pointer, timeout As Float) As Mpv_event

' void mpv_terminate_destroy(mpv_handle *ctx)
' Disconnect and destroy the mpv_handle.
Private Extern mpv_terminate_destroy(ctx As Pointer)


Public Sub Button1_Click()
 
 Dim mpv As Pointer
 Dim err As Integer
 Dim cmd As String[] = ["loadfile", Null, Null]
 Dim v As Long
 Dim ev As Mpv_event
 
 cmd[1] = "/percorso/del/file/video"
 
 setlocale(LC_NUMERIC, "C")
 
 mpv = mpv_create()
 If mpv == 0 Then Error.Raise("Error !")
 
 err = mpv_initialize(mpv)
 If err < 0 Then
   Terminus(mpv)
   Error.Raise("Error !")
 Endif
 
 err = mpv_command(mpv, cmd)
 If err < 0 Then
   Terminus(mpv)
   Error.Raise("Error !")
 Endif
 
' Imposta il Volume: 
'    0 = muto;
'  100 = nessuna variazione di volume;
' >100 = aumento del volume rispetto al valore originario.
 v = 50
 mpv_set_property(mpv, "volume", MPV_FORMAT_INT64, VarPtr(v))
 
' Imposta la superficie grafica (in questo caso la "DrawingArea" posta sul "Form"), ove mostrare il video: 
 mpv_set_option_string(mpv, "wid", CStr(DrawingArea1.Id))
 
 Repeat
' Acquisisce la corrente posizione del video espressa in secondi trascorsi dall'inizio dell'esecuzione: 
   mpv_get_property(mpv, "time-pos", MPV_FORMAT_INT64, VarPtr(v))
   Write "\r\e[31m" & Str(Time(0, 0, 0, v * 1000))
   Flush
   ev = mpv_wait_event(mpv, 1)
 Until ev.event_id == MPV_EVENT_END_FILE   ' Il ciclo termina, quando termina l'esecuzione del file video
 
 Terminus(mpv)
 
End

Private Procedure Terminus(po As Pointer)

 mpv_terminate_destroy(po)

End


Riferimenti