programmazione:glib:catturare_output_comando
Catturare output comando con glib
Autore: Fabio Di Matteo
Ultima revisione: 23/03/2017 - 17:15
In questo esempio andiamo a catturare l'output del comando youtube-dl –get-title https://www.youtube.com/watch?v=y4uHZJiH_Mo
e lo stampiamo sulla variabile gchar* output;
La funzione ritorna il titolo del video youtube a partire dall'url.
#include <glib.h> ... gchar* getVideoTitle(gchar* url) { GError *error = NULL; gchar *cmd[]={ youtubedl,"--get-title",url, NULL }; gint *exitStatus; gchar *output = NULL; //output del comando int exit_status = 0; g_spawn_sync(NULL, cmd, NULL, 0, NULL, NULL, &output, NULL, &exit_status, &error); if (error!=NULL) { g_print(" Errore: %s\n",error->message); g_error_free (error); } return output; }
Versione con controllo errori dallo stderr:
gchar* getVideoTitle(gchar* url) { GError *error = NULL; gchar *cmd[]={ youtubedl,"--get-title",url, NULL }; gint *exitStatus; gchar *output = NULL; // will contain command output gchar *errorYoutubedl = 0; // will contain command output int exit_status = 0; g_spawn_sync(NULL, cmd, NULL, 0, NULL, NULL, &output, &errorYoutubedl, &exit_status, &error); if (error!=NULL) { g_print(" Errore: %s\n",error->message); g_error_free (error); } if (strlen(errorYoutubedl)!=0) { g_print("Errori: %s\n", errorYoutubedl); errorYoutubedl=g_strdup(""); } return output; }
programmazione/glib/catturare_output_comando.txt · Ultima modifica: 08/05/2025 10:02 da 127.0.0.1