-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn_process.c
164 lines (127 loc) · 4.83 KB
/
spawn_process.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <gtk/gtk.h>
#include "struct.h"
gint button_clicked( GtkButton*, Data* );
static void cb_child_watch( GPid, gint, Data* );
static gboolean cb_out_watch( GIOChannel*, GIOCondition, Data* );
static gboolean cb_err_watch( GIOChannel*, GIOCondition, Data* );
/* Spawn ffmpeg. */
gint button_clicked(GtkButton *button, Data *data)
{
gchar *outputfilename = g_strconcat( data->inputfilename, ".mp3", NULL );
gchar *meta_title_conc = g_strconcat( "title=", data->title, NULL );
gchar *meta_artist_conc = g_strconcat( "artist=", data->artist, NULL );
gchar *meta_album_conc = g_strconcat( "album=", data->album, NULL );
gchar *meta_genre_conc = g_strconcat( "genre=", data->genre, NULL );
gchar *meta_year_conc = g_strconcat( "year=", data->year, NULL );
gchar *quality_conc = g_strconcat( data->quality, "k", NULL );
GtkWidget *dialog;
/* Get the filename for the output. */
data->outputfilename = (gchar*)gtk_entry_get_text(GTK_ENTRY(data->output_entry));
if( g_utf8_strlen( data->outputfilename, 1) == 0 )
data->outputfilename = g_strconcat( data->inputfilename, ".mp3", NULL );
gtk_widget_set_visible(data->spinner, TRUE);
gtk_spinner_start( GTK_SPINNER(data->spinner) );
GPid pid;
gchar *argv[] = { "ffmpeg", "-i", (gchar*)data->inputfilename, "-metadata",
meta_title_conc, "-metadata",
meta_artist_conc, "-metadata", meta_album_conc, "-metadata",
meta_genre_conc, "-metadata", meta_year_conc, "-ab", quality_conc,
data->outputfilename, NULL };
gint out,
err;
GIOChannel *out_ch,
*err_ch;
gboolean ret;
if( g_utf8_strlen( data->inputfilename, 1 ) == 0 )
{
gtk_spinner_stop( GTK_SPINNER(data->spinner) );
dialog = gtk_message_dialog_new (GTK_WINDOW(data->parent), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Pick a file to transcode" );
gtk_widget_set_visible(data->spinner, FALSE);
gtk_window_set_title (GTK_WINDOW (dialog), "Error");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return 1;
}
GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH;
/* Spawn child process. */
ret = g_spawn_async_with_pipes( NULL, argv, NULL, flags, NULL,
NULL, &pid, NULL, &out, &err, NULL );
if( ! ret )
{
g_error( "SPAWNING FFMPEG FAILED" );
return 1;
}
/* Add watch function to catch termination of the process. This function
* will clean any remnants of process. */
g_child_watch_add( pid, (GChildWatchFunc)cb_child_watch, data );
/* Create channels that will be used to read data from pipes. */
out_ch = g_io_channel_unix_new( out );
err_ch = g_io_channel_unix_new( err );
/* Add watches to channels. */
g_io_add_watch( out_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_out_watch, data );
g_io_add_watch( err_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_err_watch, data );
/* Free stuff. */
g_free( outputfilename );
g_free( meta_title_conc );
g_free( meta_artist_conc );
g_free( quality_conc );
g_free( meta_album_conc );
g_free( meta_genre_conc );
g_free( meta_year_conc );
return 0;
}
/* */
static void cb_child_watch( GPid pid, gint status, Data *data )
{
GtkWidget *dialog;
/* Remove timeout callback. */
g_source_remove( data->timeout_id );
/* Close pid. */
g_spawn_close_pid( pid );
gtk_spinner_stop( GTK_SPINNER(data->spinner) );
gtk_widget_set_visible(data->spinner, FALSE);
if( status == 0 )
{
dialog = gtk_message_dialog_new (GTK_WINDOW(data->parent), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Transcoding successful !" );
gtk_window_set_title (GTK_WINDOW (dialog), "Finished");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
else
{
dialog = gtk_message_dialog_new (GTK_WINDOW(data->parent), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Oops something went wrong !" );
gtk_window_set_title (GTK_WINDOW (dialog), "Error");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
}
/* */
static gboolean cb_out_watch( GIOChannel *channel, GIOCondition cond, Data *data )
{
gchar *string;
gsize size;
if( cond == G_IO_HUP )
{
g_io_channel_unref( channel );
return( FALSE );
}
g_io_channel_read_line( channel, &string, &size, NULL, NULL );
gtk_text_buffer_insert_at_cursor( data->out, string, -1 );
g_free( string );
return( TRUE );
}
/* */
static gboolean cb_err_watch( GIOChannel *channel, GIOCondition cond, Data *data )
{
gchar *string;
gsize size;
if( cond == G_IO_HUP )
{
g_io_channel_unref( channel );
return( FALSE );
}
g_io_channel_read_line( channel, &string, &size, NULL, NULL );
gtk_text_buffer_insert_at_cursor( data->err, string, -1 );
g_free( string );
return( TRUE );
}