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 |
||
| 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 dinamica condivisa: "''libgstreamer-1.0''" | Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria dinamica condivisa: "''libgstreamer-1.0''" | ||
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. | =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 | Private webcam As Pointer | ||
| Riga 80: | Riga 79: | ||
'''End''' | '''End''' | ||
=Effettuare e salvare un video con audio con la ''Pipeline'' della la funzione ''gst_parse_launch()''= | |||
In quest'altro capitolo vedremo le modalità per salvare una ripresa video ''comprensiva di audio'' mediante una WebCam. Il file finale creato sarà di formato ''OGV''. Si costruirà una ''Pipeline'', per connettere i vari ''elementi'' di ''GStreamer'', con una linea di comando mediante la funzione esterna ''gst_parse_launch()''. | |||
Private webcam As Pointer | |||
Library "libgstreamer-1.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 | |||
<FONT Color=gray>' ''gst_init (int *argc, char **argv[])'' | |||
' ''Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.''</font> | |||
Private Extern gst_init(argc As Pointer, argv As Pointer) | |||
<FONT Color=gray>' ''GstElement * gst_parse_launch (const gchar *pipeline_description, GError **error)'' | |||
' ''Create a new pipeline based on command line syntax.''</font> | |||
Private Extern gst_parse_launch(description As String, GError As Pointer) As Pointer | |||
<FONT Color=gray>' ''GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)'' | |||
' ''Sets the state of the element.''</font> | |||
Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer | |||
<FONT Color=gray>' ''gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *cur)'' | |||
' ''Queries an element for the stream position in nanoseconds.''</font> | |||
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean | |||
<FONT Color=gray>' ''void gst_object_unref(gpointer object)'' | |||
' ''Decrements the reference count on object.''</font> | |||
Private Extern gst_object_unref(gobject As Pointer) | |||
'''Public''' Sub Main() | |||
Dim tempus As Long | |||
gst_init(0, 0) | |||
webcam = gst_parse_launch("v4l2src ! videorate ! video/x-raw,framerate=5/1 ! queue ! theoraenc ! queue ! " & | |||
"mux. alsasrc ! audio/x-raw,rate=44100,channels=2,depth=16 ! queue ! audioconvert ! queue ! " & | |||
"vorbisenc ! queue ! mux. oggmux name=mux ! filesink location=<font cOLOR=GRAY>''/percorso/del/file.ogv</font>", 0) | |||
<FONT Color=gray>' ''Avviamo la riproduzione audio-video:''</font> | |||
gst_element_set_state(webcam, GST_STATE_PLAYING) | |||
While True | |||
gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus)) | |||
Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000) | |||
Wait 0.01 | |||
Wend | |||
'''End''' | |||
'''Public''' Sub Application_Read() | |||
Dim s As String | |||
Input #File.In, s | |||
Select Case s | |||
Case "p" | |||
<FONT Color=gray>' ''Pone in pausa la riproduzione del file mediale''</font> | |||
gst_element_set_state(webcam, GST_STATE_PAUSED) | |||
Case "r" | |||
<FONT Color=gray>' ''Riprende la riproduzione del file mediale''</font> | |||
gst_element_set_state(webcam, GST_STATE_PLAYING) | |||
Case "s" | |||
<FONT Color=gray>' ''Arresta la riproduzione del file mediale''</font> | |||
gst_element_set_state(webcam, GST_STATE_NULL) | |||
gst_object_unref(webcam) | |||
Print "\nEsecuzione terminata." | |||
Quit | |||
End Select | |||
'''End''' | |||
Versione delle 16:29, 6 ott 2016
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 dinamica condivisa: "libgstreamer-1.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"
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 per quasi 20 secondi. Per modificare il tempo di ripresa, bisogna cambiare il valore di "num-buffers".
' Per porre un tempo pressoché infinito, porre il valore a -1 .
webcam = gst_parse_launch("v4l2src num-buffers=400 ! queue ! vpuenc codec=0 ! matroskamux ! filesink location=/tmp/file_immagine.mkv sync=false", 0)
' Avviamo la riproduzione video:
gst_element_set_state(webcam, GST_STATE_PLAYING)
While True
gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000)
Wait 0.01
Wend
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
Effettuare e salvare un video con audio con la Pipeline della la funzione gst_parse_launch()
In quest'altro capitolo vedremo le modalità per salvare una ripresa video comprensiva di audio mediante una WebCam. Il file finale creato sarà di formato OGV. Si costruirà una Pipeline, per connettere i vari elementi di GStreamer, con una linea di comando mediante la funzione esterna gst_parse_launch().
Private webcam As Pointer
Library "libgstreamer-1.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 ! videorate ! video/x-raw,framerate=5/1 ! queue ! theoraenc ! queue ! " &
"mux. alsasrc ! audio/x-raw,rate=44100,channels=2,depth=16 ! queue ! audioconvert ! queue ! " &
"vorbisenc ! queue ! mux. oggmux name=mux ! filesink location=/percorso/del/file.ogv", 0)
' Avviamo la riproduzione audio-video:
gst_element_set_state(webcam, GST_STATE_PLAYING)
While True
gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000)
Wait 0.01
Wend
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