File indexing completed on 2024-05-05 04:49:24

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Téo Mrnjavac <teo@kde.org>                                        *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef TRANSCODING_JOB_H
0018 #define TRANSCODING_JOB_H
0019 
0020 #include "amarok_transcoding_export.h"
0021 #include "core/transcoding/TranscodingConfiguration.h"
0022 
0023 #include <KJob>
0024 #include <QUrl>
0025 
0026 #include <KProcess>
0027 
0028 namespace Transcoding
0029 {
0030 
0031 /**
0032  * A KJob that transcodes an audio stream from a file into another file with a different
0033  * codec and container format.
0034  * @author Téo Mrnjavac <teo@kde.org>
0035  */
0036 class AMAROK_TRANSCODING_EXPORT Job : public KJob
0037 {
0038     Q_OBJECT
0039 public:
0040     /**
0041      * Constructor. Creates a Transcoding::Job and fills in the source, destination and
0042      * encoder parameters. The job does not start automatically.
0043      * @param src the path of the source file.
0044      * @param dest the path of the destination file, to be created.
0045      * @param configuration the string of parameters to be fed to the encoder. This implementation
0046      *        uses the FFmpeg executable, @see http://ffmpeg.org/ffmpeg-doc.html#SEC6
0047      * @param parent the parent QObject.
0048      */
0049     explicit Job( const QUrl &src,
0050                   const QUrl &dest,
0051                   const Transcoding::Configuration &configuration,
0052                   QObject *parent = nullptr );
0053 
0054     /**
0055      * Convenience constructor. Creates a Transcoding::Job with the destination file to be
0056      * placed in the same directory as the source.
0057      */
0058     explicit Job( QUrl &src,
0059                   const Transcoding::Configuration &configuration,
0060                   QObject *parent = nullptr );
0061 
0062     /**
0063      * Sets the path of the source file.
0064      * @param src the path of the source file.
0065      */
0066     void setSource( const QUrl &src );
0067 
0068     /**
0069      * Sets the path of the destination file, to be created.
0070      * @param dest the path of the destination file.
0071      */
0072     void setDestination( const QUrl &dest );
0073 
0074     /**
0075      * Starts the transcoding job.
0076      */
0077     void start() override;
0078 
0079     /**
0080      * Get the source url.
0081      */
0082     QUrl srcUrl() const { return m_src; }
0083 
0084     /**
0085      * Get the destination url.
0086      */
0087     QUrl destUrl() const { return m_dest; }
0088 
0089 private Q_SLOTS:
0090     void processOutput();
0091     void transcoderDone( int exitCode, QProcess::ExitStatus exitStatus );
0092     void transcoderDoneDefault();
0093     void init();
0094 
0095 private:
0096     inline qint64 computeDuration( const QString &output );
0097     inline qint64 computeProgress( const QString &output );
0098     QUrl m_src;
0099     QUrl m_dest;
0100     Transcoding::Configuration m_configuration;
0101     KProcess *m_transcoder;
0102     qint64 m_duration; //in csec
0103 };
0104 
0105 } //namespace Transcoding
0106 
0107 #endif //TRANSCODING_JOB_H