File indexing completed on 2024-05-12 04:51:52

0001 /*
0002     SPDX-FileCopyrightText: 2009-2010 Michal Malek <michalm@jabster.pl>
0003     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bvideodvdrippingpreview.h"
0009 
0010 #include "k3bcore.h"
0011 #include "k3bexternalbinmanager.h"
0012 #include "k3bdevice.h"
0013 #include "k3bprocess.h"
0014 #include <QDebug>
0015 #include <QDir>
0016 #include <QTemporaryDir>
0017 
0018 
0019 
0020 K3b::VideoDVDRippingPreview::VideoDVDRippingPreview( QObject* parent )
0021     : QObject( parent ),
0022       m_process( 0 )
0023 {
0024 }
0025 
0026 
0027 K3b::VideoDVDRippingPreview::~VideoDVDRippingPreview()
0028 {
0029     cancel();
0030     if (m_process) {
0031         m_process->deleteLater();
0032         m_process = Q_NULLPTR;
0033     }
0034 }
0035 
0036 
0037 void K3b::VideoDVDRippingPreview::generatePreview( const K3b::VideoDVD::VideoDVD& dvd, int title, int chapter )
0038 {
0039     // cleanup first
0040     cancel();
0041     if (m_process) {
0042         m_process->deleteLater();
0043         m_process = 0;
0044     }
0045     m_tempDir.reset();
0046     m_canceled = false;
0047 
0048     const K3b::ExternalBin* bin = k3bcore->externalBinManager()->binObject("transcode");
0049     if( !bin ) {
0050         emit previewDone( false );
0051         return;
0052     }
0053 
0054     // auto-select a chapter
0055     // choose the center chapter, but not the first or last if possible
0056     if( chapter == 0 )
0057         chapter = qMin( qMax( dvd[title-1].numChapters()/2, 2U ), qMax( dvd[title-1].numChapters() - 1, 1U ) );
0058 
0059     // select a frame number
0060     unsigned int frame = 30;
0061     if( dvd[title-1][chapter-1].playbackTime().totalFrames() < frame )
0062         frame = dvd[title-1][chapter-1].playbackTime().totalFrames() / 2;
0063 
0064     m_dvd = dvd;
0065     m_title = title;
0066     m_chapter = chapter;
0067 
0068     m_tempDir.reset( new QTemporaryDir );
0069 
0070     m_process = new Process();
0071     *m_process << bin->path();
0072     if ( bin->version() >= Version( 1, 1, 0 ) )
0073         *m_process << "--log_no_color";
0074     *m_process << "-i" << dvd.device()->blockDeviceName();
0075     *m_process << "-T" << QString("%1,%2").arg(title).arg(chapter);
0076     if ( bin->version() < Version( 1, 1, 0 ) ) {
0077         *m_process << "-x" << "dvd,null";
0078         *m_process << "--dvd_access_delay" << "0";
0079     }
0080     else {
0081         *m_process << "-x" << "dvd='delay=0',null";
0082     }
0083     *m_process << "-y" << "ppm,null";
0084     *m_process << "-c" << QString("%1-%2").arg( frame ).arg( frame+1 );
0085     *m_process << "-Z" << "x200";
0086     *m_process << "-o" << m_tempDir->path();
0087 
0088     connect( m_process, SIGNAL(finished(int,QProcess::ExitStatus)),
0089              this, SLOT(slotTranscodeFinished(int,QProcess::ExitStatus)) );
0090     if ( !m_process->start(KProcess::ForwardedChannels) ) {
0091         // something went wrong when starting the program
0092         // it "should" be the executable
0093         qDebug() << "(K3b::VideoDVDRippingPreview) Could not start transcode.";
0094         m_process->deleteLater();
0095         m_process = 0;
0096         m_tempDir.reset();
0097         emit previewDone( false );
0098     }
0099 }
0100 
0101 
0102 void K3b::VideoDVDRippingPreview::cancel()
0103 {
0104     if( m_process && m_process->isRunning() ) {
0105         m_canceled = true;
0106         m_process->kill();
0107         m_process->waitForFinished();
0108     }
0109 }
0110 
0111 
0112 void K3b::VideoDVDRippingPreview::slotTranscodeFinished( int, QProcess::ExitStatus exitStatus)
0113 {
0114     if( exitStatus != QProcess::NormalExit )
0115         return;
0116 
0117     // read the image
0118     QString filename = QDir( m_tempDir->path() ).filePath( "000000.ppm" );// + tempQDir->entryList( QDir::Files ).first();
0119     qDebug() << "(K3b::VideoDVDRippingPreview) reading from file " << filename;
0120     m_preview = QImage( filename );
0121     bool success = !m_preview.isNull() && !m_canceled;
0122 
0123     // remove temp files
0124     m_tempDir.reset();
0125 
0126     // clean up
0127     m_process->deleteLater();
0128     m_process = 0;
0129 
0130     qDebug() << "Preview done:" << success;
0131 
0132     // retry the first chapter in case another failed
0133     if( !success && m_chapter > 1 )
0134         generatePreview( m_dvd, m_title, 1 );
0135     else
0136         emit previewDone( success );
0137 }
0138 
0139 #include "moc_k3bvideodvdrippingpreview.cpp"