Wow, I think I got it, no errors, no segment fault:
#ifndef FFMPEG_RUNNER_HPP
#define FFMPEG_RUNNER_HPP
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QProcess>
class MyFfmpegRunnerClass
: public QObject { Q_OBJECT
private:
bool delete_me;
int n_files;
int index;
public:
{
/* init some values */
n_files = 0;
index = 0;
/* we should use one QProcess instance, thread safe? */
process->setStandardOutputFile("/dev/null");
process->setStandardErrorFile("/dev/null");
connect(process, SIGNAL(started()), this, SLOT(handleprocessStarted()));
connect(process,
SIGNAL(finished
(int,
QProcess::ExitStatus)),
this,
SLOT(handleprocessExited
(int,
QProcess::ExitStatus)));
//connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater())); <-- We don't need this, do we?
}
void Start(const QString& _srcdir, const QString& _outdir, bool be_deleted = false) {
/* grab values from parent's */
srcdir = _srcdir;
outdir = _outdir;
delete_me = be_deleted;
/* grab only selected files */
folder.
setNameFilters(QStringList() <<
"*.mp3" <<
"*.mp4" <<
"*.flv" <<
"*.wav" <<
"*.ogg" <<
"*.3gp");
files
= folder.
entryList(QDir::NoDotAndDotDot|QDir
::Files);
n_files = files.size(); // hold the number of them
emit aJob();
handleNewJob(); // begin a job
}
~MyFfmpegRunnerClass() {
/* ok, they kill us from parent's, so test if a process is running before a suicide */
if (process && process->pid()) { //
process->close();
delete process; // This is not a problem anymore :)
}
qDebug() << "Class ffmpeg died -_-";
}
protected:
void handleNewJob() {
/* do we have some files for the job? */
if (index < n_files) {
QString file = fi.
baseName() + ".mp3";
curr_file = fi.absoluteFilePath();
process
->start
("ffmpeg",
QStringList() <<
"-i" << curr_file <<
"-f" <<
"mp3" <<
"-ab" <<
"128k" << out_file
);
qDebug() << "Converting: " << curr_file;
index++;
} else {
emit noMoreJob();
}
}
protected slots:
void handleprocessStarted() {
emit startJob();
}
void handleprocessExited
(int exitCode,
QProcess::ExitStatus exitStatus
) { qDebug() << "Exited code: " << exitCode << ". ExitStatus: " << exitStatus;
if ( (exitCode == 0) && (delete_me) ) {
emit endJob();
handleNewJob(); // look for more files
// you should delete source file here below
}
}
signals:
void aJob();
void startJob();
void noMoreJob();
void endJob();
};
#endif
#ifndef FFMPEG_RUNNER_HPP
#define FFMPEG_RUNNER_HPP
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QProcess>
class MyFfmpegRunnerClass : public QObject {
Q_OBJECT
private:
QProcess *process;
QString srcdir;
QString outdir;
QString curr_file;
bool delete_me;
int n_files;
QStringList files;
int index;
public:
MyFfmpegRunnerClass(QObject *parent = 0) : QObject(parent)
{
/* init some values */
n_files = 0;
index = 0;
/* we should use one QProcess instance, thread safe? */
process = new QProcess(this);
process->setStandardOutputFile("/dev/null");
process->setStandardErrorFile("/dev/null");
connect(process, SIGNAL(started()), this, SLOT(handleprocessStarted()));
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleprocessExited(int, QProcess::ExitStatus)));
//connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater())); <-- We don't need this, do we?
}
void Start(const QString& _srcdir, const QString& _outdir, bool be_deleted = false) {
/* grab values from parent's */
srcdir = _srcdir;
outdir = _outdir;
delete_me = be_deleted;
/* grab only selected files */
QDir folder(srcdir);
folder.setNameFilters(QStringList() << "*.mp3" << "*.mp4" << "*.flv" << "*.wav" << "*.ogg" << "*.3gp");
files = folder.entryList(QDir::NoDotAndDotDot|QDir::Files);
n_files = files.size(); // hold the number of them
emit aJob();
handleNewJob(); // begin a job
}
~MyFfmpegRunnerClass() {
/* ok, they kill us from parent's, so test if a process is running before a suicide */
if (process && process->pid()) { //
process->close();
delete process; // This is not a problem anymore :)
}
qDebug() << "Class ffmpeg died -_-";
}
protected:
void handleNewJob() {
/* do we have some files for the job? */
if (index < n_files) {
QFileInfo fi(srcdir, files.at(index));
QString file = fi.baseName() + ".mp3";
curr_file = fi.absoluteFilePath();
QString out_file = QDir(outdir).filePath(file);
process->start("ffmpeg", QStringList() << "-i" << curr_file << "-f" << "mp3" << "-ab" << "128k" << out_file);
qDebug() << "Converting: " << curr_file;
index++;
} else {
emit noMoreJob();
}
}
protected slots:
void handleprocessStarted() {
emit startJob();
}
void handleprocessExited(int exitCode, QProcess::ExitStatus exitStatus) {
qDebug() << "Exited code: " << exitCode << ". ExitStatus: " << exitStatus;
if ( (exitCode == 0) && (delete_me) ) {
emit endJob();
handleNewJob(); // look for more files
// you should delete source file here below
}
}
signals:
void aJob();
void startJob();
void noMoreJob();
void endJob();
};
#endif
To copy to clipboard, switch view to plain text mode
#ifndef TEST01_HPP
#define TEST01_HPP
#include <QApplication>
#include <QWidget>
#include <QFileDialog>
#include <QDir>
#include <QDesktopServices> // Qt 4.2
#include "ui_test01.h"
#include "ffmpeg_runner.hpp"
namespace Ui {
class Form;
};
Q_OBJECT
private:
Ui::Form *ui;
MyFfmpegRunnerClass *ffmpeg;
public:
{
ui->setupUi(this);
/* below is beta, watch it!!! */
ffmpeg = new MyFfmpegRunnerClass(this);
connect(ffmpeg, SIGNAL(aJob()), this, SLOT(handleAJob()));
connect(ffmpeg, SIGNAL(startJob()), this, SLOT(handleStart()));
connect(ffmpeg, SIGNAL(endJob()), this, SLOT(handleEnd()));
connect(ffmpeg, SIGNAL(noMoreJob()), this, SLOT(handleNoMoreJob()));
/* ... */
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(handleRunButton()));
connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(handleBrowseButton()));
}
virtual ~MyForm() {
delete ffmpeg;
delete ui;
}
protected slots:
void handleBrowseButton() {
if (!folder.isEmpty()) ui->lineEdit->setText(folder);
}
void handleRunButton() {
QString source_dir
= ui
->lineEdit
->text
();
bool delete_me = true;
ffmpeg->Start(source_dir, target_dir, delete_me);
}
void handleStart() {
qDebug() << "We have a new job";
}
void handleEnd() {
qDebug() << "We don't have the job";
}
void handleAJob() {
qDebug() << "We should disable some stuff here";
}
void handleNoMoreJob() {
qDebug() << "We should enable some stuff here";
}
};
#endif
#ifndef TEST01_HPP
#define TEST01_HPP
#include <QApplication>
#include <QWidget>
#include <QFileDialog>
#include <QDir>
#include <QDesktopServices> // Qt 4.2
#include "ui_test01.h"
#include "ffmpeg_runner.hpp"
namespace Ui {
class Form;
};
class MyForm : public QWidget {
Q_OBJECT
private:
Ui::Form *ui;
MyFfmpegRunnerClass *ffmpeg;
public:
MyForm(QWidget *root = 0) : QWidget(root), ui(new Ui::Form)
{
ui->setupUi(this);
/* below is beta, watch it!!! */
ffmpeg = new MyFfmpegRunnerClass(this);
connect(ffmpeg, SIGNAL(aJob()), this, SLOT(handleAJob()));
connect(ffmpeg, SIGNAL(startJob()), this, SLOT(handleStart()));
connect(ffmpeg, SIGNAL(endJob()), this, SLOT(handleEnd()));
connect(ffmpeg, SIGNAL(noMoreJob()), this, SLOT(handleNoMoreJob()));
/* ... */
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(handleRunButton()));
connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(handleBrowseButton()));
ui->lineEdit->setText(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
}
virtual ~MyForm() {
delete ffmpeg;
delete ui;
}
protected slots:
void handleBrowseButton() {
QString folder = QFileDialog::getExistingDirectory(this);
if (!folder.isEmpty()) ui->lineEdit->setText(folder);
}
void handleRunButton() {
QString source_dir = ui->lineEdit->text();
QString target_dir = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
bool delete_me = true;
ffmpeg->Start(source_dir, target_dir, delete_me);
}
void handleStart() {
qDebug() << "We have a new job";
}
void handleEnd() {
qDebug() << "We don't have the job";
}
void handleAJob() {
qDebug() << "We should disable some stuff here";
}
void handleNoMoreJob() {
qDebug() << "We should enable some stuff here";
}
};
#endif
To copy to clipboard, switch view to plain text mode
in your opinion, everything is ok? No memory leaks?
Bookmarks