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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
/*
*
* $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
* Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file "COPYING" for the exact licensing terms.
*/
#include "k3bvideodvdtitletranscodingjob.h"
#include <k3bexternalbinmanager.h>
#include <k3bprocess.h>
#include <k3bcore.h>
#include <k3bglobals.h>
#include <klocale.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <tqfile.h>
#include <tqfileinfo.h>
class K3bVideoDVDTitleTranscodingJob::Private
{
public:
const K3bExternalBin* usedTranscodeBin;
K3bProcess* process;
TQString twoPassEncodingLogFile;
int currentEncodingPass;
bool canceled;
int lastProgress;
int lastSubProgress;
};
K3bVideoDVDTitleTranscodingJob::K3bVideoDVDTitleTranscodingJob( K3bJobHandler* hdl, TQObject* parent )
: K3bJob( hdl, parent ),
m_clippingTop( 0 ),
m_clippingBottom( 0 ),
m_clippingLeft( 0 ),
m_clippingRight( 0 ),
m_width( 0 ),
m_height( 0 ),
m_titleNumber( 1 ),
m_audioStreamIndex( 0 ),
m_videoCodec( VIDEO_CODEC_FFMPEG_MPEG4 ),
m_audioCodec( AUDIO_CODEC_MP3 ),
m_videoBitrate( 1800 ),
m_audioBitrate( 128 ),
m_audioVBR( false ),
m_resampleAudio( false ),
m_twoPassEncoding( false ),
m_lowPriority( true )
{
d = new Private;
d->process = 0;
}
K3bVideoDVDTitleTranscodingJob::~K3bVideoDVDTitleTranscodingJob()
{
delete d->process;
delete d;
}
void K3bVideoDVDTitleTranscodingJob::start()
{
jobStarted();
d->canceled = false;
d->lastProgress = 0;
d->usedTranscodeBin = k3bcore->externalBinManager()->binObject("transcode");
if( !d->usedTranscodeBin ) {
emit infoMessage( i18n("%1 executable could not be found.").tqarg("transcode"), ERROR );
jobFinished( false );
return;
}
if( d->usedTranscodeBin->version < K3bVersion( 1, 0, 0 ) ){
emit infoMessage( i18n("%1 version %2 is too old.")
.tqarg("transcode")
.tqarg(d->usedTranscodeBin->version), ERROR );
jobFinished( false );
return;
}
emit debuggingOutput( "Used versions", "transcode: " + d->usedTranscodeBin->version );
if( !d->usedTranscodeBin->copyright.isEmpty() )
emit infoMessage( i18n("Using %1 %2 - Copyright (C) %3")
.tqarg(d->usedTranscodeBin->name())
.tqarg(d->usedTranscodeBin->version)
.tqarg(d->usedTranscodeBin->copyright), INFO );
//
// Let's take a look at the filename
//
if( m_filename.isEmpty() ) {
m_filename = K3b::findTempFile( "avi" );
}
else {
// let's see if the directory exists and we can write to it
TQFileInfo fileInfo( m_filename );
TQFileInfo dirInfo( fileInfo.dirPath() );
if( !dirInfo.exists() && !KStandardDirs::makeDir( dirInfo.absFilePath() ) ) {
emit infoMessage( i18n("Unable to create folder '%1'").tqarg(dirInfo.filePath()), ERROR );
return;
}
else if( !dirInfo.isDir() || !dirInfo.isWritable() ) {
emit infoMessage( i18n("Invalid filename: '%1'").tqarg(m_filename), ERROR );
jobFinished( false );
return;
}
}
//
// Determine a log file for two-pass encoding
//
d->twoPassEncodingLogFile = K3b::findTempFile( "log" );
emit newTask( i18n("Transcoding title %1 from Video DVD %2").tqarg(m_titleNumber).tqarg(m_dvd.volumeIdentifier()) );
//
// Ok then, let's begin
//
startTranscode( m_twoPassEncoding ? 1 : 0 );
}
void K3bVideoDVDTitleTranscodingJob::startTranscode( int pass )
{
d->currentEncodingPass = pass;
d->lastSubProgress = 0;
TQString videoCodecString;
switch( m_videoCodec ) {
case VIDEO_CODEC_XVID:
videoCodecString = "xvid";
break;
case VIDEO_CODEC_FFMPEG_MPEG4:
videoCodecString = "ffmpeg";
break;
default:
emit infoMessage( i18n("Invalid Video codec set: %1").tqarg(m_videoCodec), ERROR );
jobFinished( false );
return;
}
TQString audioCodecString;
switch( m_audioCodec ) {
case AUDIO_CODEC_MP3:
audioCodecString = "0x55";
break;
// ogg only works (as in: transcode does something) with .y <codec>,ogg
// but then the video is garbage (at least to xine and mplayer on my system)
// case AUDIO_CODEC_OGG_VORBIS:
// audioCodecString = "0xfffe";
// break;
case AUDIO_CODEC_AC3_STEREO:
case AUDIO_CODEC_AC3_PASSTHROUGH:
audioCodecString = "0x2000";
break;
default:
emit infoMessage( i18n("Invalid Audio codec set: %1").tqarg(m_audioCodec), ERROR );
jobFinished( false );
return;
}
//
// prepare the process
//
delete d->process;
d->process = new K3bProcess();
d->process->setSuppressEmptyLines(true);
d->process->setSplitStdout(true);
connect( d->process, TQT_SIGNAL(stderrLine(const TQString&)), this, TQT_SLOT(slotTranscodeStderr(const TQString&)) );
connect( d->process, TQT_SIGNAL(stdoutLine(const TQString&)), this, TQT_SLOT(slotTranscodeStderr(const TQString&)) );
connect( d->process, TQT_SIGNAL(processExited(KProcess*)), this, TQT_SLOT(slotTranscodeExited(KProcess*)) );
// the executable
*d->process << d->usedTranscodeBin;
// low priority
if( m_lowPriority )
*d->process << "--nice" << "19";
// we only need 100 steps, but to make sure we use 150
if ( d->usedTranscodeBin->version.simplify() >= K3bVersion( 1, 1, 0 ) )
*d->process << "--progress_meter" << "2" << "--progress_rate" << TQString::number(m_dvd[m_titleNumber-1].playbackTime().totalFrames()/150);
else
*d->process << "--print_status" << TQString::number(m_dvd[m_titleNumber-1].playbackTime().totalFrames()/150);
// the input
*d->process << "-i" << m_dvd.device()->blockDeviceName();
// just to make sure
*d->process << "-x" << "dvd";
// select the title number
*d->process << "-T" << TQString("%1,-1,1").tqarg( m_titleNumber );
// select the audio stream to extract
if ( m_dvd[m_titleNumber-1].numAudioStreams() > 0 )
*d->process << "-a" << TQString::number( m_audioStreamIndex );
// clipping
*d->process << "-j" << TQString("%1,%2,%3,%4")
.tqarg(m_clippingTop)
.tqarg(m_clippingLeft)
.tqarg(m_clippingBottom)
.tqarg(m_clippingRight);
// select the encoding type (single pass or two-pass) and the log file for two-pass encoding
// the latter is unused for pass = 0
*d->process << "-R" << TQString("%1,%2").tqarg( pass ).tqarg( d->twoPassEncodingLogFile );
// depending on the pass we use different options
if( pass != 1 ) {
// select video codec
*d->process << "-y" << videoCodecString;
// select the audio codec to use
*d->process << "-N" << audioCodecString;
if( m_audioCodec == AUDIO_CODEC_AC3_PASSTHROUGH ) {
// keep 5.1 sound
*d->process << "-A";
}
else {
// audio quality settings
*d->process << "-b" << TQString("%1,%2").tqarg(m_audioBitrate).tqarg(m_audioVBR ? 1 : 0);
// resample audio stream to 44.1 khz
if( m_resampleAudio )
*d->process << "-E" << "44100";
}
// the output filename
*d->process << "-o" << m_filename;
}
else {
// gather information about the video stream, ignore audio
*d->process << "-y" << TQString("%1,null").tqarg( videoCodecString );
// we ignore the output from the first pass
*d->process << "-o" << "/dev/null";
}
// choose the ffmpeg codec
if( m_videoCodec == VIDEO_CODEC_FFMPEG_MPEG4 ) {
*d->process << "-F" << "mpeg4";
}
// video bitrate
*d->process << "-w" << TQString::number( m_videoBitrate );
// video resizing
int usedWidth = m_width;
int usedHeight = m_height;
if( m_width == 0 || m_height == 0 ) {
//
// The "real" size of the video, considering anamorph encoding
//
int realHeight = m_dvd[m_titleNumber-1].videoStream().realPictureHeight();
int readWidth = m_dvd[m_titleNumber-1].videoStream().realPictureWidth();
//
// The clipped size with the correct aspect ratio
//
int clippedHeight = realHeight - m_clippingTop - m_clippingBottom;
int clippedWidth = readWidth - m_clippingLeft - m_clippingRight;
//
// Now simply resize the clipped video to the wanted size
//
if( usedWidth > 0 ) {
usedHeight = clippedHeight * usedWidth / clippedWidth;
}
else {
if( usedHeight == 0 ) {
//
// This is the default case in which both m_width and m_height are 0.
// The result will be a size of clippedWidth x clippedHeight
//
usedHeight = clippedHeight;
}
usedWidth = clippedWidth * usedHeight / clippedHeight;
}
}
//
// Now make sure both width and height are multiple of 16 the simple way
//
usedWidth -= usedWidth%16;
usedHeight -= usedHeight%16;
// we only give information about the resizing of the video once
if( pass < 2 )
emit infoMessage( i18n("Resizing picture of title %1 to %2x%3").tqarg(m_titleNumber).tqarg(usedWidth).tqarg(usedHeight), INFO );
*d->process << "-Z" << TQString("%1x%2").tqarg(usedWidth).tqarg(usedHeight);
// additional user parameters from config
const TQStringList& params = d->usedTranscodeBin->userParameters();
for( TQStringList::const_iterator it = params.begin(); it != params.end(); ++it )
*d->process << *it;
// produce some debugging output
kdDebug() << "***** transcode parameters:\n";
const TQValueList<TQCString>& args = d->process->args();
TQString s;
for( TQValueList<TQCString>::const_iterator it = args.begin(); it != args.end(); ++it ) {
s += *it + " ";
}
kdDebug() << s << flush << endl;
emit debuggingOutput( d->usedTranscodeBin->name() + " command:", s);
// start the process
if( !d->process->start( KProcess::NotifyOnExit, KProcess::All ) ) {
// something went wrong when starting the program
// it "should" be the executable
emit infoMessage( i18n("Could not start %1.").tqarg(d->usedTranscodeBin->name()), K3bJob::ERROR );
jobFinished(false);
}
else {
if( pass == 0 )
emit newSubTask( i18n("Single-pass Encoding") );
else if( pass == 1 )
emit newSubTask( i18n("Two-pass Encoding: First Pass") );
else
emit newSubTask( i18n("Two-pass Encoding: Second Pass") );
emit subPercent( 0 );
}
}
void K3bVideoDVDTitleTranscodingJob::cancel()
{
// FIXME: do not cancel before one frame has been encoded. transcode seems to hang then
// find a way to determine all subprocess ids to kill all of them
d->canceled = true;
if( d->process && d->process->isRunning() )
d->process->kill();
}
void K3bVideoDVDTitleTranscodingJob::cleanup( bool success )
{
if( TQFile::exists( d->twoPassEncodingLogFile ) ) {
TQFile::remove( d->twoPassEncodingLogFile );
}
if( !success && TQFile::exists( m_filename ) ) {
emit infoMessage( i18n("Removing incomplete video file '%1'").tqarg(m_filename), INFO );
TQFile::remove( m_filename );
}
}
void K3bVideoDVDTitleTranscodingJob::slotTranscodeStderr( const TQString& line )
{
emit debuggingOutput( "transcode", line );
// parse progress
// encoding frames [000000-000144], 27.58 fps, EMT: 0:00:05, ( 0| 0| 0)
if( line.startsWith( "encoding frame" ) ) {
int pos1 = line.find( '-', 15 );
int pos2 = line.find( ']', pos1+1 );
if( pos1 > 0 && pos2 > 0 ) {
bool ok;
int encodedFrames = line.mid( pos1+1, pos2-pos1-1 ).toInt( &ok );
if( ok ) {
int progress = 100 * encodedFrames / m_dvd[m_titleNumber-1].playbackTime().totalFrames();
if( progress > d->lastSubProgress ) {
d->lastSubProgress = progress;
emit subPercent( progress );
}
if( m_twoPassEncoding ) {
progress /= 2;
if( d->currentEncodingPass == 2 )
progress += 50;
}
if( progress > d->lastProgress ) {
d->lastProgress = progress;
emit percent( progress );
}
}
}
}
}
void K3bVideoDVDTitleTranscodingJob::slotTranscodeExited( KProcess* p )
{
if( d->canceled ) {
emit canceled();
cleanup( false );
jobFinished( false );
}
else if( p->normalExit() ) {
switch( p->exitStatus() ) {
case 0:
if( d->currentEncodingPass == 1 ) {
emit percent( 50 );
// start second encoding pass
startTranscode( 2 );
}
else {
emit percent( 100 );
cleanup( true );
jobFinished( true );
}
break;
default:
// FIXME: error handling
emit infoMessage( i18n("%1 returned an unknown error (code %2).")
.tqarg(d->usedTranscodeBin->name()).tqarg(p->exitStatus()),
K3bJob::ERROR );
emit infoMessage( i18n("Please send me an email with the last output."), K3bJob::ERROR );
cleanup( false );
jobFinished( false );
}
}
else {
cleanup( false );
emit infoMessage( i18n("Execution of %1 failed.").tqarg("transcode"), ERROR );
emit infoMessage( i18n("Please consult the debugging output for details."), ERROR );
jobFinished( false );
}
}
void K3bVideoDVDTitleTranscodingJob::setClipping( int top, int left, int bottom, int right )
{
m_clippingTop = top;
m_clippingLeft = left;
m_clippingBottom = bottom;
m_clippingRight = right;
//
// transcode seems unable to handle different clipping values for left and right
//
m_clippingLeft = m_clippingRight = TQMIN( m_clippingRight, m_clippingLeft );
}
void K3bVideoDVDTitleTranscodingJob::setSize( int width, int height )
{
m_width = width;
m_height = height;
}
TQString K3bVideoDVDTitleTranscodingJob::audioCodecString( K3bVideoDVDTitleTranscodingJob::AudioCodec codec )
{
switch( codec ) {
case AUDIO_CODEC_AC3_STEREO:
return i18n("AC3 (Stereo)");
case AUDIO_CODEC_AC3_PASSTHROUGH:
return i18n("AC3 (Pass-through)");
case AUDIO_CODEC_MP3:
return i18n("MPEG1 Layer III");
default:
return "unknown audio codec";
}
}
TQString K3bVideoDVDTitleTranscodingJob::videoCodecString( K3bVideoDVDTitleTranscodingJob::VideoCodec codec )
{
switch( codec ) {
case VIDEO_CODEC_FFMPEG_MPEG4:
return i18n("MPEG4 (FFMPEG)");
case VIDEO_CODEC_XVID:
return i18n("XviD");
default:
return "unknown video codec";
}
}
TQString K3bVideoDVDTitleTranscodingJob::videoCodecDescription( K3bVideoDVDTitleTranscodingJob::VideoCodec codec )
{
switch( codec ) {
case VIDEO_CODEC_FFMPEG_MPEG4:
return i18n("FFmpeg is an open-source project trying to support most video and audio codecs used "
"these days. Its subproject libavcodec forms the basis for multimedia players such as "
"xine or mplayer.")
+ "<br>"
+ i18n("FFmpeg contains an implementation of the MPEG-4 video encoding standard which produces "
"high quality results.");
case VIDEO_CODEC_XVID:
return i18n("XviD is a free and open source MPEG-4 video codec. XviD was created by a group of "
"volunteer programmers after the OpenDivX source was closed in July 2001.")
+ "<br>"
+ i18n("XviD features MPEG-4 Advanced Profile settings such as b-frames, global "
"and quarter pixel motion compensation, lumi masking, trellis quantization, and "
"H.263, MPEG and custom quantization matrices.")
+ "<br>"
+ i18n("XviD is a primary competitor of DivX (XviD being DivX spelled backwards). "
"While DivX is closed source and may only run on Windows, Mac OS and Linux, "
"XviD is open source and can potentially run on any platform.")
+ "<br><em>"
+ i18n("(Description taken from the Wikipedia article)")
+ "</em>";
default:
return "unknown video codec";
}
}
TQString K3bVideoDVDTitleTranscodingJob::audioCodecDescription( K3bVideoDVDTitleTranscodingJob::AudioCodec codec )
{
static TQString s_ac3General = i18n("AC3, better known as Dolby Digital is standardized as ATSC A/52. "
"It contains up to 6 total channels of sound.");
switch( codec ) {
case AUDIO_CODEC_AC3_STEREO:
return s_ac3General
+ "<br>" + i18n("With this setting K3b will create a two-channel stereo "
"Dolby Digital audio stream.");
case AUDIO_CODEC_AC3_PASSTHROUGH:
return s_ac3General
+ "<br>" + i18n("With this setting K3b will use the Dolby Digital audio stream "
"from the source DVD without changing it.")
+ "<br>" + i18n("Use this setting to preserve 5.1 channel sound from the DVD.");
case AUDIO_CODEC_MP3:
return i18n("MPEG1 Layer III is better known as MP3 and is the most used lossy audio format.")
+ "<br>" + i18n("With this setting K3b will create a two-channel stereo MPEG1 Layer III audio stream.");
default:
return "unknown audio codec";
}
}
bool K3bVideoDVDTitleTranscodingJob::transcodeBinaryHasSupportFor( K3bVideoDVDTitleTranscodingJob::VideoCodec codec, const K3bExternalBin* bin )
{
static char* s_codecFeatures[] = { "xvid", "ffmpeg" };
if( !bin )
bin = k3bcore->externalBinManager()->binObject("transcode");
if( !bin )
return false;
return bin->hasFeature( TQString::tqfromLatin1( s_codecFeatures[(int)codec] ) );
}
bool K3bVideoDVDTitleTranscodingJob::transcodeBinaryHasSupportFor( K3bVideoDVDTitleTranscodingJob::AudioCodec codec, const K3bExternalBin* bin )
{
static char* s_codecFeatures[] = { "lame", "ac3", "ac3" };
if( !bin )
bin = k3bcore->externalBinManager()->binObject("transcode");
if( !bin )
return false;
return bin->hasFeature( TQString::tqfromLatin1( s_codecFeatures[(int)codec] ) );
}
#include "k3bvideodvdtitletranscodingjob.moc"
|