contrib: gpg-error: group the configure flags in a variable
[vlc.git] / doc / libvlc / libvlc_DVD_ripper.c
blob7a6e778991b341218938c85f979c2a8f6bff7669
1 // gcc -o main main.c `pkg-config --cflags --libs gtk+-2.0 libvlc`
3 /* Written by Vincent Schüßler */
4 /* License WTFPL http://sam.zoy.org/wtfpl/ */
6 #include <stdlib.h>
7 #include <time.h>
8 #include <string.h>
9 #include <gtk/gtk.h>
10 #include <vlc/vlc.h>
12 #define PRESET_H264_AAC_MP4_HIGH "MP4 high (h.264 + AAC)"
13 #define PRESET_H264_AAC_MP4_LOW "MP4 low (h.264 + AAC)"
14 #define PRESET_THEORA_VORBIS_OGG_HIGH "OGG high (Theora + Vorbis)"
15 #define PRESET_THEORA_VORBIS_OGG_LOW "OGG low (Theora + Vorbis)"
16 #define PRESET_VP8_VORBIS_WEBM_HIGH "WebM high (VP8 + Vorbis)"
17 #define PRESET_VP8_VORBIS_WEBM_LOW "WebM low (VP8 + Vorbis)"
18 #define BORDER_WIDTH 6
20 GtkWidget *window;
21 GtkWidget *source_entry, *dest_entry;
22 GtkWidget *progressbar;
23 libvlc_instance_t *vlcinst;
24 libvlc_media_list_t *medialist;
25 GtkWidget *run_button, *format_chooser, *spinner;
26 char stopped;
28 gchar* get_filepath(GtkWidget* widget, GtkFileChooserAction action) {
29 GtkWidget *dialog;
30 gchar *path;
31 dialog = gtk_file_chooser_dialog_new("Choose location", GTK_WINDOW(gtk_widget_get_toplevel(widget)), action, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
32 if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
33 path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
35 else {
36 path = NULL;
38 gtk_widget_destroy(dialog);
39 return path;
42 void on_select_source_path(GtkWidget *widget, gpointer data) {
43 char *path;
44 char scheme[] = "file://";
45 char *uri;
47 if(data==NULL) {
48 path = (char*) get_filepath(widget, GTK_FILE_CHOOSER_ACTION_OPEN);
50 else {
51 path = (char*) get_filepath(widget, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
53 if(path != NULL) {
54 uri = malloc((strlen(scheme)+strlen(path)+1) * sizeof(char));
55 if(uri == NULL) return;
56 uri[0] = '\0';
57 strncat(uri, scheme, strlen(scheme));
58 strncat(uri, path, strlen(path));
59 g_free(path);
61 gtk_entry_set_text(GTK_ENTRY(source_entry), uri);
62 free(uri);
66 void on_select_dest_path(GtkWidget *widget, gpointer data) {
67 gchar *path;
68 path = get_filepath(widget, GTK_FILE_CHOOSER_ACTION_SAVE);
69 if(path != NULL) {
70 gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
71 g_free(path);
75 char* get_pos_string(float pos) {
76 int len;
77 const char format[] = "%.3f %%";
78 char *pos_string;
79 pos *= 100;
80 len = snprintf(NULL, 0, format, pos);
81 pos_string = malloc(len);
82 if(pos_string==NULL) return NULL;
83 sprintf(pos_string, format, pos);
84 return pos_string;
87 gboolean update_progressbar(char *handle) {
88 float pos = libvlc_vlm_get_media_instance_position(vlcinst, handle, 0);
89 char *pos_string;
90 if(pos < 1.0 && 0.0 <= pos) {
91 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar), pos);
92 pos_string = get_pos_string(pos);
93 if(pos_string != NULL) {
94 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar), pos_string);
95 free(pos_string);
97 return TRUE;
99 if(stopped = 1) {
100 free(handle);
101 return FALSE;
105 const char* get_transcode_string(char *preset) {
106 static const char mp4_high[] = "#transcode{vcodec=h264,venc=x264{cfr=16},scale=1,acodec=mp4a,ab=160,channels=2,samplerate=44100}";
107 static const char mp4_low[] = "#transcode{vcodec=h264,venc=x264{cfr=40},scale=1,acodec=mp4a,ab=96,channels=2,samplerate=44100}";
108 static const char ogg_high[] = "#transcode{vcodec=theo,venc=theora{quality=9},scale=1,acodec=vorb,ab=160,channels=2,samplerate=44100}";
109 static const char ogg_low[] = "#transcode{vcodec=theo,venc=theora{quality=4},scale=1,acodec=vorb,ab=96,channels=2,samplerate=44100}";
110 static const char webm_high[] = "#transcode{vcodec=VP80,vb=2000,scale=1,acodec=vorb,ab=160,channels=2,samplerate=44100}";
111 static const char webm_low[] = "#transcode{vcodec=VP80,vb=1000,scale=1,acodec=vorb,ab=96,channels=2,samplerate=44100}";
112 static const char nothing[] = "";
113 if(0 == strcmp(preset, PRESET_H264_AAC_MP4_HIGH)) {
114 return mp4_high;
116 else if(0 == strcmp(preset, PRESET_H264_AAC_MP4_LOW)) {
117 return mp4_low;
119 else if(0 == strcmp(preset, PRESET_THEORA_VORBIS_OGG_HIGH)) {
120 return ogg_high;
122 else if(0 == strcmp(preset, PRESET_THEORA_VORBIS_OGG_LOW)) {
123 return ogg_low;
125 else if(0 == strcmp(preset, PRESET_VP8_VORBIS_WEBM_HIGH)) {
126 return webm_high;
128 else if(0 == strcmp(preset, PRESET_VP8_VORBIS_WEBM_LOW)) {
129 return webm_low;
131 else {
132 return nothing;
136 void on_run(GtkWidget *widget, gpointer data) {
137 char *handle;
138 const char *transcode;
139 char *source = (char*) gtk_entry_get_text(GTK_ENTRY(source_entry));
140 char *dest = (char*) gtk_entry_get_text(GTK_ENTRY(dest_entry));
141 char *preset= (char*) gtk_combo_box_get_active_text(GTK_COMBO_BOX(format_chooser));
142 int i;
143 char file_begin[] = ":file{dst=";
144 char file_end[] = "}";
145 char *sout;
146 if(preset == NULL) return;
147 gtk_widget_set_sensitive(widget, FALSE);
148 handle = malloc((strlen(source)+4+1) * sizeof(char));
149 if(handle == NULL) return;
150 strncpy(handle, source, strlen(source));
151 for(i=0;i<=3;++i) {
152 handle[strlen(source)+i] = (char) (((unsigned int) rand()) & 63) + '0';
154 handle[strlen(source)+4] = '\0';
155 transcode = get_transcode_string(preset);
156 free(preset);
157 sout = malloc((strlen(transcode)+strlen(file_begin)+strlen(dest)+strlen(file_end)+1) * sizeof(char));
158 if(sout == NULL) {
159 free(handle);
160 return;
162 strncpy(sout, transcode, strlen(transcode)+1);
163 strncat(sout, file_begin, strlen(file_begin));
164 strncat(sout, dest, strlen(dest));
165 strncat(sout, file_end, strlen(file_end));
166 libvlc_vlm_add_broadcast(vlcinst, handle, source, sout, 0, NULL, 1, 0);
167 free(sout);
168 libvlc_vlm_play_media(vlcinst, handle);
169 gtk_widget_show(spinner);
170 gtk_spinner_start(GTK_SPINNER(spinner));
171 stopped = 0;
172 g_timeout_add(50, (GSourceFunc) update_progressbar, handle);
175 void stop(void) {
176 stopped = 1;
177 gtk_spinner_stop(GTK_SPINNER(spinner));
178 gtk_widget_hide(spinner);
179 gtk_widget_set_sensitive(run_button, TRUE);
180 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar), 0.0);
181 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar), "");
184 gboolean on_end(void) {
185 GtkWidget *dialog;
186 stop();
187 dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Rip done");
188 gtk_dialog_run(GTK_DIALOG(dialog));
189 gtk_widget_destroy(dialog);
190 return FALSE;
193 void on_end_vlc(const libvlc_event_t *event, void *data) {
194 g_idle_add((GSourceFunc) on_end, NULL);
197 gboolean on_error(void) {
198 GtkWidget *dialog;
199 stop();
200 dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error");gtk_dialog_run(GTK_DIALOG(dialog));
201 gtk_widget_destroy(dialog);
202 return FALSE;
205 void on_error_vlc(const libvlc_event_t *event, void *data) {
206 g_idle_add((GSourceFunc) on_error, NULL);
209 int main (int argc, char *argv[]) {
210 GtkWidget *media_list, *scrolled;
211 GtkWidget *choose_frame, *output_frame, *run_frame;
212 GtkWidget *vbox, *choose_table, *output_table, *run_table;
213 GtkWidget *source_label, *source_button, *source_folder_button;
214 GtkWidget *dest_label, *dest_button;
216 libvlc_event_manager_t *evtman;
218 gtk_init(&argc, &argv);
220 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
221 gtk_container_set_border_width(GTK_CONTAINER(window), BORDER_WIDTH);
222 gtk_window_set_title(GTK_WINDOW(window), "libVLC DVD ripper");
224 //setup vbox
225 vbox = gtk_vbox_new(FALSE, BORDER_WIDTH);
226 gtk_container_add(GTK_CONTAINER(window), vbox);
228 // setup media list with scrolled window
229 media_list = gtk_tree_view_new();
230 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(media_list), FALSE);
231 scrolled = gtk_scrolled_window_new(NULL, NULL);
232 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
233 gtk_container_add(GTK_CONTAINER(scrolled), media_list);
235 // setup "choose"-frame
236 choose_frame = gtk_frame_new("Choose DVD");
237 gtk_box_pack_start(GTK_BOX(vbox), choose_frame, TRUE, TRUE, 0);
238 choose_table = gtk_table_new(1, 4, FALSE);
239 gtk_table_set_row_spacings(GTK_TABLE(choose_table), BORDER_WIDTH/2);
240 gtk_table_set_col_spacings(GTK_TABLE(choose_table), BORDER_WIDTH/2);
241 gtk_container_set_border_width(GTK_CONTAINER(choose_table), BORDER_WIDTH);
242 source_label = gtk_label_new("Input file or folder:");
243 source_entry = gtk_entry_new();
244 gtk_entry_set_text(GTK_ENTRY(source_entry), "dvd://");
245 source_button = gtk_button_new_with_label("Open file");
246 source_folder_button = gtk_button_new_with_label("Open folder");
247 g_signal_connect(G_OBJECT(source_button), "clicked", G_CALLBACK(on_select_source_path), NULL);
248 g_signal_connect(G_OBJECT(source_folder_button), "clicked", G_CALLBACK(on_select_source_path), (gpointer) 1);
249 gtk_table_attach(GTK_TABLE(choose_table), source_label, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
250 gtk_table_attach(GTK_TABLE(choose_table), source_entry, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
251 gtk_table_attach(GTK_TABLE(choose_table), source_button, 2, 3, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
252 gtk_table_attach(GTK_TABLE(choose_table), source_folder_button, 3, 4, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
253 gtk_container_add(GTK_CONTAINER(choose_frame), choose_table);
255 // setup "output"-frame
256 output_frame = gtk_frame_new("Output settings");
257 gtk_box_pack_start(GTK_BOX(vbox), output_frame, TRUE, TRUE, 0);
258 output_table = gtk_table_new(2, 3, FALSE);
259 gtk_table_set_row_spacings(GTK_TABLE(output_table), BORDER_WIDTH/2);
260 gtk_table_set_col_spacings(GTK_TABLE(output_table), BORDER_WIDTH/2);
261 gtk_container_set_border_width(GTK_CONTAINER(output_table), BORDER_WIDTH);
262 gtk_container_add(GTK_CONTAINER(output_frame), output_table);
263 dest_label = gtk_label_new("Output file:");
264 dest_entry = gtk_entry_new();
265 format_chooser = gtk_combo_box_new_text();
266 gtk_combo_box_append_text(GTK_COMBO_BOX(format_chooser), PRESET_H264_AAC_MP4_HIGH);
267 gtk_combo_box_append_text(GTK_COMBO_BOX(format_chooser), PRESET_H264_AAC_MP4_LOW);
268 gtk_combo_box_append_text(GTK_COMBO_BOX(format_chooser), PRESET_THEORA_VORBIS_OGG_HIGH);
269 gtk_combo_box_append_text(GTK_COMBO_BOX(format_chooser), PRESET_THEORA_VORBIS_OGG_LOW);
270 gtk_combo_box_append_text(GTK_COMBO_BOX(format_chooser), PRESET_VP8_VORBIS_WEBM_HIGH);
271 gtk_combo_box_append_text(GTK_COMBO_BOX(format_chooser), PRESET_VP8_VORBIS_WEBM_LOW);
272 gtk_combo_box_set_active(GTK_COMBO_BOX(format_chooser), 0);
273 dest_button = gtk_button_new_from_stock(GTK_STOCK_OPEN);
274 g_signal_connect(G_OBJECT(dest_button), "clicked", G_CALLBACK(on_select_dest_path), NULL);
275 gtk_table_attach(GTK_TABLE(output_table), dest_label, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
276 gtk_table_attach(GTK_TABLE(output_table), dest_entry, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
277 gtk_table_attach(GTK_TABLE(output_table), dest_button, 2, 3, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
278 gtk_table_attach(GTK_TABLE(output_table), format_chooser, 0, 4, 1, 2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
280 // setup "run"-frame
281 run_frame = gtk_frame_new("Run");
282 gtk_box_pack_start(GTK_BOX(vbox), run_frame, TRUE, TRUE, 0);
283 run_table = gtk_table_new(3, 2, FALSE);
284 gtk_table_set_row_spacings(GTK_TABLE(run_table), BORDER_WIDTH/2);
285 gtk_table_set_col_spacings(GTK_TABLE(run_table), BORDER_WIDTH/2);
286 gtk_container_set_border_width(GTK_CONTAINER(run_table), BORDER_WIDTH);
287 gtk_container_add(GTK_CONTAINER(run_frame), run_table);
288 progressbar = gtk_progress_bar_new();
289 spinner = gtk_spinner_new();
290 run_button = gtk_button_new_from_stock(GTK_STOCK_OK);
291 g_signal_connect(G_OBJECT(run_button), "clicked", G_CALLBACK(on_run), NULL);
292 gtk_table_attach(GTK_TABLE(run_table), progressbar, 0, 3, 0, 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
293 gtk_table_attach(GTK_TABLE(run_table), gtk_label_new(""), 0, 1, 1, 2, GTK_EXPAND, GTK_SHRINK, 0, 0);
294 gtk_table_attach(GTK_TABLE(run_table), spinner, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
295 gtk_table_attach(GTK_TABLE(run_table), run_button, 2, 3, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
297 // setup vlc
298 vlcinst = libvlc_new(0, NULL);
299 evtman = libvlc_vlm_get_event_manager(vlcinst);
300 libvlc_event_attach(evtman, libvlc_VlmMediaInstanceStatusEnd, on_end_vlc, NULL);
301 libvlc_event_attach(evtman, libvlc_VlmMediaInstanceStatusError, on_error_vlc, NULL);
303 g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
304 gtk_widget_show_all(window);
305 gtk_widget_hide(spinner);
306 gtk_main ();
307 libvlc_release(vlcinst);
308 return 0;