安裝
$ pip install youtube-dl
取得可用格式
$ youtube-dl --list-formats youtube_link
取得 url
$ youtube-dl --format mp4 --get-url youtube_link
利用 GStreamer 撥放
$ gst-launch-1.0 playbin uri="$(youtube-dl --format mp4 --get-url youtube_link)"
在程式中利用 youtube-dl
GError *error = NULL;
GSubprocess *youtube_dl = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error,
"/home/nano/.local/bin/youtube-dl",
//"--format", "mp4",
"-f", "18",
//"--get-url", "https://www.youtube.com/watch?v=gMd8bLJLsiw",
"--get-url", "https://www.youtube.com/watch?v=2P_74GLABBY",
NULL);
if (error) {
g_error ("Could Not Launch Sub-Proess youtube-dl");
return -1;
}
GInputStream *istream = g_subprocess_get_stdout_pipe (youtube_dl);
gchar buffer[1024];
gssize urlSize = g_input_stream_read(istream, buffer, sizeof(buffer), NULL, &error);
if (error || urlSize == sizeof(buffer)) {
g_print("error g_input_stream_read\n");
return -1;
}
buffer[urlSize] = 0;
g_print("youtube_dl:uri %s\n", buffer);
/* Set the URI to play */
g_object_set (data.playbin, "uri", buffer, NULL);
沒有留言:
張貼留言