Salvare in un file video mediante le funzioni esterne del API di GStreamer la ripresa video effettuata con una WebCam: differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| (10 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
La risorsa '''GStreamer''' consente anche di catturare, riprodurre, nonché salvare in un file video la ripresa video effettuata con una ''WebCam''. | La risorsa '''GStreamer''' consente anche di catturare, riprodurre, nonché salvare in un file video la ripresa video effettuata con una ''WebCam''. | ||
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "''libgstreamer-1.0.so.0. | Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "''libgstreamer-1.0.so.0.2606.0'' ". | ||
| Riga 9: | Riga 9: | ||
Library "libgstreamer-1.0:0. | Library "libgstreamer-1.0:0.2606.0" | ||
Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING | Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING | ||
| Riga 92: | Riga 92: | ||
Library "libgstreamer-1.0:0. | Library "libgstreamer-1.0:0.2606.0" | ||
Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING | Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING | ||
Versione attuale delle 12:25, 8 ott 2025
La risorsa GStreamer consente anche di catturare, riprodurre, nonché salvare in un file video la ripresa video effettuata con una WebCam.
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "libgstreamer-1.0.so.0.2606.0 ".
Effettuare e salvare un video senza audio
Mostriamo di seguito un esempo pratico di ripresa video - senza audio - in un'applicazione a riga di comando. Oltre a non contenere audio, non saranno mostrate in contemporanea le immagini della ripresa video. La ripresa video sarà salvata in file immagine di formato MKV.
Private webcam As Pointer
Library "libgstreamer-1.0:0.2606.0"
Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING
Private Const GST_FORMAT_TIME As Integer = 3
' gst_init (int *argc, char **argv[])
' Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.
Private Extern gst_init(argc As Pointer, argv As Pointer)
' GstElement * gst_parse_launch (const gchar *pipeline_description, GError **error)
' Create a new pipeline based on command line syntax.
Private Extern gst_parse_launch(description As String, GError As Pointer) As Pointer
' GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)
' Sets the state of the element.
Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer
' gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *cur)
' Queries an element for the stream position in nanoseconds.
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean
' void gst_object_unref(gpointer object)
' Decrements the reference count on object.
Private Extern gst_object_unref(gobject As Pointer)
Public Sub Main()
Dim tempus As Long
gst_init(0, 0)
' Effettua una ripresa video "senza" audio:
webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! matroskamux ! filesink location=/tmp/file_video.mkv sync=false", 0)
' Avviamo la riproduzione video:
gst_element_set_state(webcam, GST_STATE_PLAYING)
Do
gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
Write #File.Out, "\rTempo: " & Time(0, 0, 0, tempus / 1000000)
Wait 0.01
Loop
End
Public Sub Application_Read()
Dim s As String
Input #File.In, s
Select Case s
Case "p"
' Pone in pausa la riproduzione del file mediale
gst_element_set_state(webcam, GST_STATE_PAUSED)
Case "r"
' Riprende la riproduzione del file mediale
gst_element_set_state(webcam, GST_STATE_PLAYING)
Case "s"
' Arresta la riproduzione del file mediale
gst_element_set_state(webcam, GST_STATE_NULL)
gst_object_unref(webcam)
Print "\nEsecuzione terminata."
Quit
End Select
End
Salvare il video - privo di audio - in un file di formato OGG-Theora
Se si intende salvare il video - privo di audio - in un file di formato OGG-Theora, si adotterà la seguente riga nel precedente codice:
webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! theoraenc ! oggmux ! filesink location=/tmp/file_video.ogg sync=false", 0)
Effettuare e salvare un video con audio mediante la Pipeline della funzione gst_parse_launch()
In quest'altro capitolo vedremo le modalità per salvare una ripresa video comprensiva di audio mediante una WebCam. Si costruirà una Pipeline, per connettere i vari elementi di GStreamer, con una linea di comando mediante la funzione esterna gst_parse_launch().
File audio-video di formato MKV
In questo esempio il file finale creato sarà di formato MKV (Matroska):
Private webcam As Pointer
Library "libgstreamer-1.0:0.2606.0"
Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING
Private Const GST_FORMAT_TIME As Integer = 3
' gst_init (int *argc, char **argv[])
' Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.
Private Extern gst_init(argc As Pointer, argv As Pointer)
' GstElement * gst_parse_launch (const gchar *pipeline_description, GError **error)
' Create a new pipeline based on command line syntax.
Private Extern gst_parse_launch(description As String, GError As Pointer) As Pointer
' GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)
' Sets the state of the element.
Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer
' gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *cur)
' Queries an element for the stream position in nanoseconds.
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean
' void gst_object_unref(gpointer object)
' Decrements the reference count on object.
Private Extern gst_object_unref(gobject As Pointer)
Public Sub Main()
Dim tempus As Long
gst_init(0, 0)
webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! mkv. autoaudiosrc ! queue ! audioconvert ! mkv. matroskamux name=mkv ! filesink location=/tmp/test.mkv sync=false", 0)
' Avviamo la riproduzione audio-video:
gst_element_set_state(webcam, GST_STATE_PLAYING)
Do
gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
Write "\rTempo: " & Time(0, 0, 0, tempus / 1000000)
Flush
Wait 0.01
Loop
Print "\nEsecuzione terminata."
Quit
End
Public Sub Application_Read()
Dim s As String
Input #File.In, s
Select Case s
Case "p"
' Pone in pausa la riproduzione del file mediale
gst_element_set_state(webcam, GST_STATE_PAUSED)
Case "r"
' Riprende la riproduzione del file mediale
gst_element_set_state(webcam, GST_STATE_PLAYING)
Case "s"
' Arresta la riproduzione del file mediale
gst_element_set_state(webcam, GST_STATE_NULL)
gst_object_unref(webcam)
End Select
End
Mostrare nel video anche il tempo trascorso
Per mostrare nella finestra del video anche il tempo trascorso dall'inizio della ripresa video, è necessario utilizzare il plugin timeoverlay fornito da GStreamer. Pertanto la stringa del primo argomento della funzione esterna "gst_parse_launch()" diventa come segue:
webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! timeoverlay ! videoconvert ! mkv. autoaudiosrc ! queue ! audioconvert ! mkv. matroskamux name=mkv ! filesink location=/tmp/test.mkv sync=false", 0)