Catturare e riprodurre immagini video mediante una WebCam con le funzioni esterne delle API di GStreamer: differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| (19 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
La risorsa '''GStreamer''' consente anche di catturare e riprodurre immagini video mediante una ''WebCam''. | La risorsa '''GStreamer''' consente anche di catturare e riprodurre immagini video mediante una ''WebCam''. | ||
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria | Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "''libgstreamer-1.0.so.0.2606.0'' ". | ||
=Effettuare | =Effettuare un video senza audio= | ||
Mostriamo di seguito due esempi di cattura e riproduzione di immagini video ''senza audio'' con una webCam in un'applicazione ''a riga di comando''. | Mostriamo di seguito due esempi di cattura e riproduzione di immagini video ''senza audio'' con una webCam in un'applicazione ''a riga di comando''. | ||
| Riga 12: | Riga 12: | ||
Library "libgstreamer-1.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 31: | Riga 31: | ||
<FONT Color=gray>' ''void g_object_set(gpointer object, const gchar *first_property_name, ...)'' | <FONT Color=gray>' ''void g_object_set(gpointer object, const gchar *first_property_name, ...)'' | ||
' ''Sets properties on an object.''</font> | ' ''Sets properties on an object.''</font> | ||
Private Extern g_object_set(gobject As Pointer, key As String, value As String) | Private Extern g_object_set(gobject As Pointer, key As String, value As String, element_1 As Pointer) | ||
<FONT Color=gray>' ''void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...)'' | <FONT Color=gray>' ''void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...)'' | ||
' ''Adds a NULL-terminated list of elements to a bin.''</font> | ' ''Adds a NULL-terminated list of elements to a bin.''</font> | ||
Private Extern gst_bin_add_many(bin As Pointer, element_1 As Pointer, element_2 As Pointer) | Private Extern gst_bin_add_many(bin As Pointer, element_1 As Pointer, element_2 As Pointer, element_3 As Pointer, element_4 As Pointer) | ||
<FONT Color=gray>' ''gboolean gst_element_link_many (GstElement *element_1, GstElement *element_2, ...)'' | <FONT Color=gray>' ''gboolean gst_element_link_many (GstElement *element_1, GstElement *element_2, ...)'' | ||
' ''Chain together a series of elements.''</font> | ' ''Chain together a series of elements.''</font> | ||
Private Extern gst_element_link_many(element_1 As Pointer, element_2 As Pointer) As Boolean | Private Extern gst_element_link_many(element_1 As Pointer, element_2 As Pointer, element_3 As Pointer, element_4 As Pointer) As Boolean | ||
<FONT Color=gray>' ''GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)'' | <FONT Color=gray>' ''GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)'' | ||
| Riga 54: | Riga 54: | ||
Public Sub Main() | |||
Dim pipeline, sink As Pointer | Dim pipeline, con, sink As Pointer | ||
Dim tempus As Long | Dim tempus As Long | ||
gst_init(0, 0) | |||
pipeline = gst_pipeline_new("webcam") | |||
webcam = gst_element_factory_make("v4l2src", "webcam") | |||
g_object_set(webcam, "device", "/dev/video0", 0) | |||
con = gst_element_factory_make("videoconvert", "convert") | |||
sink = gst_element_factory_make("xvimagesink", "image") | |||
gst_bin_add_many(pipeline, webcam, con, sink, 0) | |||
gst_element_link_many(webcam, con, sink, 0) | |||
<FONT Color=gray>' ''Avviamo la riproduzione video:''</font> | <FONT Color=gray>' ''Avviamo la riproduzione video:''</font> | ||
gst_element_set_state(pipeline, GST_STATE_PLAYING) | |||
Do | |||
gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus)) | |||
Write "\rTempo: " & Time(0, 0, 0, tempus / 1000000) | |||
Wait 0.01 | |||
Loop | |||
End | |||
Public Sub Application_Read() | |||
Dim s As String | Dim s As String | ||
| Riga 106: | Riga 108: | ||
End Select | End Select | ||
End | |||
==Impostazione della ''Pipeline'' con la funzione "gst_parse_launch()"== | |||
==Impostazione della ''Pipeline'' con la funzione | |||
In quest'altro esempio si costruirà una ''Pipeline'', per connettere i vari ''elementi'' di ''GStreamer'', con una linea di comando mediante la funzione esterna ''gst_parse_launch()''. | In quest'altro esempio si costruirà una ''Pipeline'', per connettere i vari ''elementi'' di ''GStreamer'', con una linea di comando mediante la funzione esterna ''gst_parse_launch()''. | ||
| Riga 117: | Riga 118: | ||
Library "libgstreamer-1.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 143: | Riga 144: | ||
Public Sub Main() | |||
Dim tempus As Long | Dim tempus As Long | ||
gst_init(0, 0) | |||
webcam = gst_parse_launch("v4l2src ! tee name=t_vid ! queue ! videoconvert ! xvimagesink sync=false t_vid. ! queue ! videorate ! video/x-raw-yuv,framerate=30/1", 0) | |||
<FONT Color=gray>' ''Avviamo la riproduzione video:''</font> | <FONT Color=gray>' ''Avviamo la riproduzione video:''</font> | ||
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 | Input #File.In, s | ||
| Riga 184: | Riga 185: | ||
End Select | End Select | ||
End | |||
====Altre possibili soluzioni di ripresa video con la funzione | ====Altre possibili soluzioni di ripresa video con la funzione "gst_parse_launch()"==== | ||
Dimensionando la finestra video ad esempio su 640x480: | Dimensionando la finestra video ad esempio su 640x480: | ||
webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! xvimagesink", 0) | webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! xvimagesink", 0) | ||
| Riga 193: | Riga 194: | ||
Aspetto 16/9: | Aspetto 16/9: | ||
webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! aspectratiocrop aspect-ratio=16/9 ! videoconvert ! xvimagesink", 0) | webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! aspectratiocrop aspect-ratio=16/9 ! videoconvert ! xvimagesink", 0) | ||
Versione attuale delle 12:22, 8 ott 2025
La risorsa GStreamer consente anche di catturare e riprodurre immagini video mediante una WebCam.
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "libgstreamer-1.0.so.0.2606.0 ".
Effettuare un video senza audio
Mostriamo di seguito due esempi di cattura e riproduzione di immagini video senza audio con una webCam in un'applicazione a riga di comando.
Impostazione passo passo della Pipeline con varie funzioni
In questo esempio si costruirà mediante varie funzioni una Pipeline per connettere i vari elementi di GStreamer:
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_pipeline_new (const gchar *name)
' Create a new pipeline with the given name.
Private Extern gst_pipeline_new(name As String) As Pointer
' GstElement * gst_element_factory_make(const gchar *factoryname, Const gchar * name)
' Create a new element of the type defined by the given element factory.
Private Extern gst_element_factory_make(factoryname As String, name As String) As Pointer
' void g_object_set(gpointer object, const gchar *first_property_name, ...)
' Sets properties on an object.
Private Extern g_object_set(gobject As Pointer, key As String, value As String, element_1 As Pointer)
' void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...)
' Adds a NULL-terminated list of elements to a bin.
Private Extern gst_bin_add_many(bin As Pointer, element_1 As Pointer, element_2 As Pointer, element_3 As Pointer, element_4 As Pointer)
' gboolean gst_element_link_many (GstElement *element_1, GstElement *element_2, ...)
' Chain together a series of elements.
Private Extern gst_element_link_many(element_1 As Pointer, element_2 As Pointer, element_3 As Pointer, element_4 As Pointer) As Boolean
' 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 pipeline, con, sink As Pointer
Dim tempus As Long
gst_init(0, 0)
pipeline = gst_pipeline_new("webcam")
webcam = gst_element_factory_make("v4l2src", "webcam")
g_object_set(webcam, "device", "/dev/video0", 0)
con = gst_element_factory_make("videoconvert", "convert")
sink = gst_element_factory_make("xvimagesink", "image")
gst_bin_add_many(pipeline, webcam, con, sink, 0)
gst_element_link_many(webcam, con, sink, 0)
' Avviamo la riproduzione video:
gst_element_set_state(pipeline, GST_STATE_PLAYING)
Do
gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
Write "\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
Impostazione della Pipeline con la funzione "gst_parse_launch()"
In quest'altro esempio si costruirà una Pipeline, per connettere i vari elementi di GStreamer, con una linea di comando mediante la funzione esterna gst_parse_launch().
In questo esempio rendremo la cattura delle immagini più veloce e pertanto l'esecuzione del video risulterà essere più fluida:
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 ! tee name=t_vid ! queue ! videoconvert ! xvimagesink sync=false t_vid. ! queue ! videorate ! video/x-raw-yuv,framerate=30/1", 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
Altre possibili soluzioni di ripresa video con la funzione "gst_parse_launch()"
Dimensionando la finestra video ad esempio su 640x480:
webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! xvimagesink", 0)
Aspetto 16/9:
webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! aspectratiocrop aspect-ratio=16/9 ! videoconvert ! xvimagesink", 0)