0
点赞
收藏
分享

微信扫一扫

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

深夜瞎琢磨 2022-01-20 阅读 45

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

#include <QCoreApplication>
#include <QTextStream>
#include <QDir>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QProcess>

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\n2 -> Reset path\n3 -> Exit application\nCommand:") << 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;
                 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;
                            // 在这个json文件里面保存了视频的标题,将他取出来
                            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();
                                out << "Current media title:" << targetName << "\n" << Qt::flush;
                                QProcess process;
                                dir.mkdir("./output");
                                // 这里执行ffmpeg命令来合成音视频生成.mp4文件
                                process.execute("ffmpeg -i "
                                                + fileInfo.filePath() + "/64/video.m4s "
                                                "-i " + fileInfo.filePath() + "/64/audio.m4s "
                                                "-c:v copy -strict experimental \"" + dir.path() +"/output/" + targetName + ".mp4\"");
                                process.waitForFinished(-1);
                            }
                            file.close();
                        }
                    }
                     ++i;
                 } while (i < list.size());
                 out << QString::fromLocal8Bit("Execute done\n") << Qt::flush;
                 break;
             }
             else if(code == 2)
             {
                 break;
             }
             else if(code == 3)
             {
                 return 0;
             }
             else {
                 out << QString::fromLocal8Bit("Unknown Command\n") << Qt::flush;
             }
            }
        }
        else
        {
            out << QString::fromLocal8Bit("Path does not exist\n") << Qt::flush;
        }
    }

    return a.exec();
}

举报

相关推荐

0 条评论