Qt将B站m4s视频格式转换mp4

代码实现了自动遍历B站视频利用ffmpeg将m4s格式转换为mp4格式

链接: .
#include <QCoreApplication>
#include <QTextStream>
#include <QDir>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QProcess>
#include <QThreadPool>


class CusRunnable : public QRunnable
{
          
   
public:
    explicit CusRunnable(QString comman){
          
   command = comman;}
    ~CusRunnable(){
          
   }
    void run()
    {
          
   
        QProcess process;
        process.execute(command);
        process.waitForFinished();
    }

private:
    QString command;
};

int main(int argc, char *argv[])
{
          
   
    QCoreApplication a(argc, argv);
    QTextStream out(stdout);
    QTextStream in(stdin);
    QString path;
    while (true) {
          
   
        out << QString::fromLocal8Bit("Please input m4s media path:") << Qt::flush;
        in >> path;
        QString commandCode;
        QDir dir = QDir(path);
        if(dir.exists())
        {
          
   
            while (true)
            {
          
   
             out << QString::fromLocal8Bit("1 -> Start bilibili media merge
2 -> Reset path
3 -> Exit application
Command:") << Qt::flush;
             in >> commandCode;
             int code = commandCode.toInt();
             if(code == 1)
             {
          
   
                 dir.setFilter(QDir::Dirs);
                 dir.setSorting(QDir::Name);
                 QFileInfoList list = dir.entryInfoList();
                 int i = 0;
                 QThreadPool* pool = QThreadPool::globalInstance();
                 pool->setMaxThreadCount(10);
                 pool->setExpiryTimeout(-1);
                 do
                 {
          
   
                    QFileInfo fileInfo = list.at(i);
                    if(fileInfo.fileName() == "." | fileInfo.fileName() == "..")
                    {
          
   
                       ++i;
                       continue;
                    }
                    if(fileInfo.isDir())
                    {
          
   
                        QFile file = QFile(fileInfo.filePath() + "/entry.json");
                        if(file.exists())
                        {
          
   
                            file.open(QIODevice::ReadOnly);
                            QJsonParseError error;
                            QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
                            if(doc.isNull() || error.error == error.NoError)
                            {
          
   
                                QString targetName = doc.object().value("page_data").toObject().value("part").toString();
                                dir.mkdir("./output");
                                QString comman = "ffmpeg -i "
                                        + fileInfo.filePath() + "/64/video.m4s "
                                        "-i " + fileInfo.filePath() + "/64/audio.m4s "
                                        "-c:v copy -strict experimental "" + dir.path() +"/output/" + targetName + ".mp4"";
                                CusRunnable * pWork = new CusRunnable(comman);
                                pool->start(pWork);
                            }
                            file.close();
                        }
                    }
                     ++i;
                 } while (i < list.size());
                 pool->waitForDone();
                 out << QString::fromLocal8Bit("Execute done
") << Qt::flush;
                 break;
             }
             else if(code == 2)
             {
          
   
                 break;
             }
             else if(code == 3)
             {
          
   
                 return 0;
             }
             else {
          
   
                 out << QString::fromLocal8Bit("Unknown Command
") << Qt::flush;
             }
            }
        }
        else
        {
          
   
            out << QString::fromLocal8Bit("Path does not exist
") << Qt::flush;
        }
    }

    return a.exec();
}
已添加多线程,此源码仅供学习使用。
经验分享 程序员 微信小程序 职场和发展