Wow, I think I got it, no errors, no segment fault:
Qt Code:
  1. #ifndef FFMPEG_RUNNER_HPP
  2. #define FFMPEG_RUNNER_HPP
  3.  
  4. #include <QDebug>
  5. #include <QDir>
  6. #include <QFileInfo>
  7. #include <QProcess>
  8.  
  9. class MyFfmpegRunnerClass : public QObject {
  10. Q_OBJECT
  11. private:
  12. QProcess *process;
  13. QString srcdir;
  14. QString outdir;
  15. QString curr_file;
  16. bool delete_me;
  17. int n_files;
  18. QStringList files;
  19. int index;
  20. public:
  21. MyFfmpegRunnerClass(QObject *parent = 0) : QObject(parent)
  22. {
  23. /* init some values */
  24. n_files = 0;
  25. index = 0;
  26.  
  27. /* we should use one QProcess instance, thread safe? */
  28. process = new QProcess(this);
  29. process->setStandardOutputFile("/dev/null");
  30. process->setStandardErrorFile("/dev/null");
  31. connect(process, SIGNAL(started()), this, SLOT(handleprocessStarted()));
  32. connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleprocessExited(int, QProcess::ExitStatus)));
  33. //connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater())); <-- We don't need this, do we?
  34. }
  35.  
  36. void Start(const QString& _srcdir, const QString& _outdir, bool be_deleted = false) {
  37. /* grab values from parent's */
  38. srcdir = _srcdir;
  39. outdir = _outdir;
  40. delete_me = be_deleted;
  41. /* grab only selected files */
  42. QDir folder(srcdir);
  43. folder.setNameFilters(QStringList() << "*.mp3" << "*.mp4" << "*.flv" << "*.wav" << "*.ogg" << "*.3gp");
  44. files = folder.entryList(QDir::NoDotAndDotDot|QDir::Files);
  45. n_files = files.size(); // hold the number of them
  46. emit aJob();
  47. handleNewJob(); // begin a job
  48. }
  49.  
  50. ~MyFfmpegRunnerClass() {
  51. /* ok, they kill us from parent's, so test if a process is running before a suicide */
  52. if (process && process->pid()) { //
  53. process->close();
  54. delete process; // This is not a problem anymore :)
  55. }
  56. qDebug() << "Class ffmpeg died -_-";
  57. }
  58.  
  59. protected:
  60. void handleNewJob() {
  61. /* do we have some files for the job? */
  62. if (index < n_files) {
  63. QFileInfo fi(srcdir, files.at(index));
  64. QString file = fi.baseName() + ".mp3";
  65. curr_file = fi.absoluteFilePath();
  66. QString out_file = QDir(outdir).filePath(file);
  67. process->start("ffmpeg", QStringList() << "-i" << curr_file << "-f" << "mp3" << "-ab" << "128k" << out_file);
  68. qDebug() << "Converting: " << curr_file;
  69. index++;
  70. } else {
  71. emit noMoreJob();
  72. }
  73. }
  74.  
  75. protected slots:
  76. void handleprocessStarted() {
  77. emit startJob();
  78. }
  79.  
  80. void handleprocessExited(int exitCode, QProcess::ExitStatus exitStatus) {
  81. qDebug() << "Exited code: " << exitCode << ". ExitStatus: " << exitStatus;
  82. if ( (exitCode == 0) && (delete_me) ) {
  83. emit endJob();
  84. handleNewJob(); // look for more files
  85. // you should delete source file here below
  86. }
  87. }
  88.  
  89. signals:
  90. void aJob();
  91. void startJob();
  92. void noMoreJob();
  93. void endJob();
  94. };
  95.  
  96.  
  97. #endif
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #ifndef TEST01_HPP
  2. #define TEST01_HPP
  3.  
  4. #include <QApplication>
  5. #include <QWidget>
  6. #include <QFileDialog>
  7. #include <QDir>
  8. #include <QDesktopServices> // Qt 4.2
  9. #include "ui_test01.h"
  10. #include "ffmpeg_runner.hpp"
  11.  
  12. namespace Ui {
  13. class Form;
  14. };
  15.  
  16. class MyForm : public QWidget {
  17. Q_OBJECT
  18. private:
  19. Ui::Form *ui;
  20. MyFfmpegRunnerClass *ffmpeg;
  21. public:
  22. MyForm(QWidget *root = 0) : QWidget(root), ui(new Ui::Form)
  23. {
  24. ui->setupUi(this);
  25.  
  26. /* below is beta, watch it!!! */
  27. ffmpeg = new MyFfmpegRunnerClass(this);
  28. connect(ffmpeg, SIGNAL(aJob()), this, SLOT(handleAJob()));
  29. connect(ffmpeg, SIGNAL(startJob()), this, SLOT(handleStart()));
  30. connect(ffmpeg, SIGNAL(endJob()), this, SLOT(handleEnd()));
  31. connect(ffmpeg, SIGNAL(noMoreJob()), this, SLOT(handleNoMoreJob()));
  32. /* ... */
  33.  
  34. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(handleRunButton()));
  35. connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(handleBrowseButton()));
  36. ui->lineEdit->setText(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
  37. }
  38. virtual ~MyForm() {
  39. delete ffmpeg;
  40. delete ui;
  41. }
  42.  
  43. protected slots:
  44. void handleBrowseButton() {
  45. QString folder = QFileDialog::getExistingDirectory(this);
  46. if (!folder.isEmpty()) ui->lineEdit->setText(folder);
  47. }
  48.  
  49. void handleRunButton() {
  50. QString source_dir = ui->lineEdit->text();
  51. QString target_dir = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
  52. bool delete_me = true;
  53. ffmpeg->Start(source_dir, target_dir, delete_me);
  54. }
  55.  
  56. void handleStart() {
  57. qDebug() << "We have a new job";
  58. }
  59.  
  60. void handleEnd() {
  61. qDebug() << "We don't have the job";
  62. }
  63.  
  64. void handleAJob() {
  65. qDebug() << "We should disable some stuff here";
  66. }
  67.  
  68. void handleNoMoreJob() {
  69. qDebug() << "We should enable some stuff here";
  70. }
  71.  
  72. };
  73.  
  74. #endif
To copy to clipboard, switch view to plain text mode 
in your opinion, everything is ok? No memory leaks?