summaryrefslogtreecommitdiff
path: root/src/videopreview/async.diff
blob: 4f126842cbcf07fd5f4634fa59bb9ae7b5215ff5 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
Index: videopreview/main.cpp
===================================================================
--- videopreview/main.cpp	(revisi�n: 2543)
+++ videopreview/main.cpp	(copia de trabajo)
@@ -47,11 +47,17 @@
 	*/
 	//vp.setAspectRatio( 2.35 );
 
+#if VIDEOPREVIEW_ASYNC
+	if (vp.showConfigDialog()) {
+		vp.createThumbnails();
+		return a.exec();
+	}
+#else
 	if ( (vp.showConfigDialog()) && (vp.createThumbnails()) ) {
 		vp.show();
 		vp.adjustWindowSize();
 		return a.exec();
 	}
-
+#endif
 	return 0;
 }
Index: videopreview/videopreview.cpp
===================================================================
--- videopreview/videopreview.cpp	(revisi�n: 2543)
+++ videopreview/videopreview.cpp	(copia de trabajo)
@@ -105,7 +105,15 @@
 	my_layout->addWidget(button_box);
 	setLayout(my_layout);
 
+#if VIDEOPREVIEW_ASYNC
+	process = new QProcess(this);
+	connect( process, SIGNAL(finished(int, QProcess::ExitStatus)), 
+             this, SLOT(processFinished(int, QProcess::ExitStatus)) );
 
+	connect( this, SIGNAL(finishedOk()), this, SLOT(workFinishedOK()) );
+	connect( this, SIGNAL(finishedWithError()), this, SLOT(workFinishedWithError()) );
+#endif
+
 	QList<QByteArray> r_formats = QImageReader::supportedImageFormats();
 	QString read_formats;
 	for (int n=0; n < r_formats.count(); n++) {
@@ -156,6 +164,132 @@
 		return "00000005.jpg";
 }
 
+#if VIDEOPREVIEW_ASYNC
+void VideoPreview::createThumbnails() {
+    clearThumbnails();
+    error_message.clear();
+
+	// Initalization
+	VideoInfo i = getInfo(mplayer_bin, prop.input_video);
+	int length = i.length;
+
+	if (length == 0) {
+		if (error_message.isEmpty()) error_message = tr("The length of the video is 0");
+		emit finishedWithError();
+		return;
+	}
+
+	// Create a temporary directory
+	QDir d(QDir::tempPath());
+	if (!d.exists(output_dir)) {
+		if (!d.mkpath(output_dir)) {
+			qDebug("VideoPreview::extractImages: error: can't create '%s'", full_output_dir.toUtf8().constData());
+			error_message = tr("The temporary directory (%1) can't be created").arg(full_output_dir);
+			emit finishedWithError();
+			return;
+		}
+	}
+
+	displayVideoInfo(i);
+
+	// Let's begin
+	run.thumbnail_width = 0;
+
+	run.num_pictures = prop.n_cols * prop.n_rows;
+	length -= prop.initial_step;
+	run.s_step = length / run.num_pictures;
+
+	run.current_time = prop.initial_step;
+
+	canceled = false;
+	progress->setLabelText(tr("Creating thumbnails..."));
+	progress->setRange(0, run.num_pictures-1);
+
+	run.current_picture = 0;
+	progress->setValue( run.current_picture);
+
+	if (!runMplayer(run.current_time)) {
+		emit finishedWithError();
+	}
+}
+
+void VideoPreview::processFinished(int exitCode, QProcess::ExitStatus exitStatus) {
+	qDebug("VideoPreview::processFinished");
+
+	if (exitStatus != QProcess::NormalExit) {
+		emit finishedWithError();
+		return;
+	}
+
+	// Continue processing
+	QString frame_picture = full_output_dir + "/" + framePicture();
+	if (!QFile::exists(frame_picture)) {
+		error_message = tr("The file %1 doesn't exist").arg(frame_picture);
+		emit finishedWithError();
+		return;
+	}
+
+#if RENAME_PICTURES
+	QDir d(QDir::tempPath());
+	QString extension = (extractFormat()==PNG) ? "png" : "jpg";
+	QString output_file = output_dir + QString("/picture_%1.%2").arg(run.current_time, 8, 10, QLatin1Char('0')).arg(extension);
+	d.rename(output_dir + "/" + framePicture(), output_file);
+#else
+	QString output_file = output_dir + "/" + framePicture();
+#endif
+
+	if (!addPicture(QDir::tempPath() +"/"+ output_file, run.current_picture, run.current_time)) {
+		emit finishedWithError();
+		return;
+	}
+
+	run.current_time += run.s_step;
+
+	if (canceled) {
+		emit finishedOk();
+		return;
+	}
+
+	// Next picture
+	run.current_picture++;
+
+	if (run.current_picture >= run.num_pictures) {
+		emit finishedOk();
+		return;
+	}
+
+	progress->setValue( run.current_picture);
+
+	if (!runMplayer(run.current_time)) {
+		emit finishedWithError();
+	}	
+}
+
+void VideoPreview::workFinishedOK() {
+	qDebug("VideoPreview::workFinishedOK");
+
+	show();
+	adjustWindowSize();
+
+	cleanDir(full_output_dir);
+}
+
+void VideoPreview::workFinishedWithError() {
+	qDebug("VideoPreview::workFinishedWithError");
+
+	if (!error_message.isEmpty()) {
+		QMessageBox::critical(this, tr("Error"), 
+                              tr("The following error has occurred while creating the thumbnails:")+"\n"+ error_message );
+	}
+
+	cleanDir(full_output_dir);
+
+	close();
+}
+
+
+#else // VIDEOPREVIEW_ASYNC
+
 bool VideoPreview::createThumbnails() {
 	clearThumbnails();
 	error_message.clear();
@@ -244,6 +378,7 @@
 
 	return true;
 }
+#endif // VIDEOPREVIEW_ASYNC
 
 bool VideoPreview::runMplayer(int seek) {
 	QStringList args;
@@ -283,6 +418,18 @@
 	for (int n = 0; n < args.count(); n++) command = command + args[n] + " ";
 	qDebug("VideoPreview::runMplayer: command: %s", command.toUtf8().constData());
 
+#if VIDEOPREVIEW_ASYNC
+	#ifdef CD_TO_TEMP_DIR
+	process->setWorkingDirectory(full_output_dir);
+	qDebug("VideoPreview::runMplayer: changing working directory of the process to '%s'", full_output_dir.toUtf8().constData());
+	#endif
+	process->start(mplayer_bin, args);
+	if (!process->waitForStarted()) {
+		qDebug("VideoPreview::runMplayer: error running process");
+		error_message = tr("The mplayer process didn't run");
+		return false;
+	}
+#else // VIDEOPREVIEW_ASYNC
 	QProcess p;
 	#ifdef CD_TO_TEMP_DIR
 	p.setWorkingDirectory(full_output_dir);
@@ -294,6 +441,7 @@
 		error_message = tr("The mplayer process didn't run");
 		return false;
 	}
+#endif // VIDEOPREVIEW_ASYNC
 
 	return true;
 }
Index: videopreview/videopreview.h
===================================================================
--- videopreview/videopreview.h	(revisi�n: 2543)
+++ videopreview/videopreview.h	(copia de trabajo)
@@ -19,10 +19,16 @@
 #ifndef _VIDEOPREVIEW_H_
 #define _VIDEOPREVIEW_H_
 
+#define VIDEOPREVIEW_ASYNC 0
+
 #include <QWidget>
 #include <QString>
 #include <QList>
 
+#if VIDEOPREVIEW_ASYNC
+#include <QProcess>
+#endif
+
 class QProgressDialog;
 class QGridLayout;
 class QLabel;
@@ -90,7 +96,11 @@
 	void setExtractFormat( ExtractFormat format ) { prop.extract_format = format; };
 	ExtractFormat extractFormat() { return prop.extract_format; };
 
+#if VIDEOPREVIEW_ASYNC
+	void createThumbnails();
+#else
 	bool createThumbnails();
+#endif
 
 	bool showConfigDialog();
 
@@ -106,8 +116,23 @@
 	void cancelPressed();
 	void saveImage();
 
+#if VIDEOPREVIEW_ASYNC
+	void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
+	void workFinishedOK();
+	void workFinishedWithError();
+
 protected:
+	QProcess * process;
+
+signals:
+	void finishedOk();
+	void finishedWithError();
+#endif
+
+protected:
+#if !VIDEOPREVIEW_ASYNC
 	bool extractImages();
+#endif
 	bool runMplayer(int seek);
 	bool addPicture(const QString & filename, int num, int time); 
 	void displayVideoInfo(const VideoInfo & i);
@@ -144,9 +169,19 @@
 		ExtractFormat extract_format;
 	} prop;
 
+#if VIDEOPREVIEW_ASYNC
 	struct {
 		int thumbnail_width;
+		int num_pictures;
+		int s_step;
+		int current_time;
+		int current_picture;
 	} run;
+#else
+	struct {
+		int thumbnail_width;
+	} run;
+#endif
 
 	QString last_directory;
 	QString error_message;
Index: basegui.cpp
===================================================================
--- basegui.cpp	(revisi�n: 2543)
+++ basegui.cpp	(copia de trabajo)
@@ -4022,10 +4022,16 @@
 
 	video_preview->setMplayerPath(pref->mplayer_bin);
 
+#if VIDEOPREVIEW_ASYNC
+	if (video_preview->showConfigDialog()) {
+		video_preview->createThumbnails();
+	}
+#else
 	if ( (video_preview->showConfigDialog()) && (video_preview->createThumbnails()) ) {
 		video_preview->show();
 		video_preview->adjustWindowSize();
 	}
+#endif
 }
 
 QNetworkProxy BaseGui::userProxy() {