curl/fetch.c: Rewrite to use the new enums
[libquvi.git] / examples / media.c
blob20d6d0cf0789a091d8a8746dbfd82257e7e594a8
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <quvi.h>
26 #include "examples.h"
28 static void usage()
30 g_printerr("Usage: media [-f<fmt_id,...>] [-v] [-b] [-a] <URL>\n"
31 "Options:\n"
32 " -f<arg> Select arg format ID from the available streams,\n"
33 " this may also be a comma-separated list of IDs\n"
34 " -v Enable verbose output (libcurl)\n"
35 " -b Choose the best quality stream\n"
36 " -a Enable autoproxy feature\n");
37 exit(0);
40 static void dump_stream()
42 gchar *fmt_id, *url;
44 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_FORMAT_ID, &fmt_id);
45 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_URL, &url);
47 g_print("[%s] fmt_id='%s', url='%s'\n", __func__, fmt_id, url);
50 static void dump_streams()
52 while (quvi_media_stream_next(qm) == QUVI_TRUE)
53 dump_stream();
56 typedef quvi_callback_status qcs;
58 gint main(gint argc, gchar **argv)
60 gboolean best_flag = FALSE;
61 gchar *fmt_id = NULL;
62 gchar *url = NULL;
63 gint i = 1;
65 g_assert(qm == NULL);
66 g_assert(q == NULL);
68 setlocale(LC_ALL, "");
70 if (argc < 2)
71 usage();
73 q = quvi_new();
74 exit_if_error();
76 for (; i<argc; ++i)
78 if (g_strcmp0("-v", argv[i]) == 0)
79 enable_verbose();
80 else if (g_strcmp0("-h", argv[i]) == 0)
81 usage();
82 else if (g_strcmp0("-a", argv[i]) == 0)
83 enable_autoproxy();
84 else if (g_strcmp0("-b", argv[i]) == 0)
85 best_flag = TRUE;
86 else if (g_str_has_prefix(argv[i], "-f") == TRUE)
88 if (strlen(argv[i]) >2)
89 fmt_id = &argv[i][2];
90 else
91 g_printerr("[%s] `-f' ignored, specify format ID\n", __func__);
93 else
94 url = argv[i];
97 if (url == NULL)
98 usage();
100 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) status);
102 qm = quvi_media_new(q, url);
103 exit_if_error();
105 gchar *s;
107 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
108 g_print("[%s] title='%s'\n", __func__, s);
110 if (best_flag == TRUE)
112 quvi_media_stream_choose_best(qm);
113 dump_stream();
115 else if (fmt_id != NULL)
117 quvi_media_stream_select(qm, fmt_id);
118 exit_if_error();
119 dump_stream();
121 else
122 dump_streams();
124 cleanup();
126 g_assert(qm == NULL);
127 g_assert(q == NULL);
129 return (0);
132 /* vim: set ts=2 sw=2 tw=72 expandtab: */