Qt ffmpeg音视频转换工具

news2024/10/5 13:53:28

Qt ffmpeg音视频转换工具,QProcess方式调用ffmpeg,对音视频文件进行格式转换,支持常见的音视频格式,主要在于QProcess的输出处理以及转换的文件名和后缀的处理,可以进一步加上音视频剪切合并和音视频文件属性查询修改的功能。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTextCodec>
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QFont font;
    font.setPixelSize(16);
    setFont(font);

    setWindowTitle(QStringLiteral("ffmpeg工具"));

    ui->listWidget->setMaximumWidth(200);
    connect(ui->listWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(convert()));

    ui->checkBox->setChecked(true);

    mProcess = new QProcess;
    connect(mProcess, SIGNAL(readyReadStandardError()), this, SLOT(readError()));
    connect(mProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
    connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int,QProcess::ExitStatus)));

    mTimer = new QTimer(this);
    connect(mTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
    mTimer->start(1000);

    initListWidget();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readError()
{
    QString str = mProcess->readAllStandardError().data();

    if (str == "\r")
    {
        ui->textBrowser->append(mTmpStr);
        mTmpStr.clear();
    }
    else
    {
        mTmpStr += str;
        if (str.contains("\r\n"))
        {
            ui->textBrowser->append(mTmpStr);
            mTmpStr.clear();
        }
    }
}

void MainWindow::readOutput()
{
    QByteArray qba = mProcess->readAllStandardOutput();
    QTextCodec* tc = QTextCodec::codecForName("System");
    QString str = tc->toUnicode(qba);

    if (str == "\r")
    {
        ui->textBrowser->append(mTmpStr);
        mTmpStr.clear();
    }
    else
    {
        mTmpStr += str;
        if (str.contains("\r\n"))
        {
            ui->textBrowser->append(mTmpStr);
            mTmpStr.clear();
        }
    }
}

void MainWindow::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
    ui->textBrowser->append(QStringLiteral("finished : %1 %2").arg(exitCode).arg(exitStatus));
    mProcess->close();

    if (exitCode == 0 && exitStatus == 0)
    {
        informationMessageBox(QStringLiteral("提示"), QStringLiteral("%1\n转换\n%2\n完成").arg(mSourceFile).arg(mTargetFile));
    }
    else
    {
        informationMessageBox(QStringLiteral("提示"), QStringLiteral("%1\n转换\n%2\n失败").arg(mSourceFile).arg(mTargetFile));
    }
}

void MainWindow::updateTimer()
{
    if (!mTmpStr.isEmpty())
    {
        ui->textBrowser->append(mTmpStr);
        mTmpStr.clear();
    }
}

void MainWindow::initListWidget()
{
    QStringList nameLst;
    nameLst.append(QStringLiteral("FLAC转MP3")); // ffmpeg -i input.flac -ab 320k -map_metadata 0 -id3v2_version 3 output.mp3
    nameLst.append(QStringLiteral("M4A转MP3")); // ffmpeg -i 1.m4a -acodec libmp3lame -aq 0 123.mp3
    nameLst.append(QStringLiteral("WAV转MP3")); // ffmpeg -i input.wav -f mp3 -acodec libmp3lame -aq 0 output.mp3
    nameLst.append(QStringLiteral("APE转MP3")); // ffmpeg -i 1.ape -acodec libmp3lame -aq 0 123.mp3
    nameLst.append("");
    nameLst.append(QStringLiteral("MP4转M4A")); // ffmpeg -i test.mp4 -acodec copy -vn 123.m4a
    nameLst.append(QStringLiteral("MP4转AAC")); // ffmpeg -i test.mp4 -acodec copy -vn 123.aac
    nameLst.append(QStringLiteral("MP4转MP3")); // ffmpeg -i test.mp4 -acodec libmp3lame -aq 0 123.mp3
    nameLst.append("");
    nameLst.append(QStringLiteral("MP3转OGG")); // ffmpeg -i bb.mp3 -acodec libvorbis -ab 128k bb.ogg
    nameLst.append(QStringLiteral("MP3转WAV")); // ffmpeg -i input.mp3 -f wav output.wav

    QMap<QString, QString> cmdMap;
    cmdMap.insert(QStringLiteral("FLAC转MP3"), QStringLiteral("ffmpeg -i \"%1\" -ab 320k -map_metadata 0 -id3v2_version 3 -aq 0 \"%2\""));
    cmdMap.insert(QStringLiteral("M4A转MP3"), QStringLiteral("ffmpeg -i \"%1\" -acodec libmp3lame -aq 0 \"%2\""));
    cmdMap.insert(QStringLiteral("WAV转MP3"), QStringLiteral("ffmpeg -i \"%1\" -f mp3 -acodec libmp3lame -aq 0 \"%2\""));
    cmdMap.insert(QStringLiteral("APE转MP3"), QStringLiteral("ffmpeg -i \"%1\" -acodec libmp3lame -aq 0 \"%2\""));
    cmdMap.insert(QStringLiteral("MP4转M4A"), QStringLiteral("ffmpeg -i \"%1\" -acodec copy -vn \"%2\""));
    cmdMap.insert(QStringLiteral("MP4转AAC"), QStringLiteral("ffmpeg -i \"%1\" -acodec copy -vn \"%2\""));
    cmdMap.insert(QStringLiteral("MP4转MP3"), QStringLiteral("ffmpeg -i \"%1\" -acodec libmp3lame -aq 0 \"%2\""));
    cmdMap.insert(QStringLiteral("MP3转OGG"), QStringLiteral("ffmpeg -i \"%1\" -acodec libvorbis -ab 128k \"%2\""));
    cmdMap.insert(QStringLiteral("MP3转WAV"), QStringLiteral("ffmpeg -i \"%1\" -f wav \"%2\""));

    foreach (QString name, nameLst)
    {
        QListWidgetItem *item = new QListWidgetItem;
        if (!name.isEmpty())
        {
            item->setText(name);
            item->setData(Qt::UserRole, cmdMap.value(name));
        }
        else
        {
            item->setText("");
            item->setData(Qt::UserRole, "");
        }
        ui->listWidget->addItem(item);
    }
}

QString MainWindow::getFileSuffix(QString file)
{
    QString ret;

    if (file == "FLAC")
    {
        ret = QStringLiteral("flac");
    }
    else if (file == "MP3")
    {
        ret = QStringLiteral("mp3");
    }
    else if (file == "M4A")
    {
        ret = QStringLiteral("m4a");
    }
    else if (file == "WAV")
    {
        ret = QStringLiteral("wav");
    }
    else if (file == "APE")
    {
        ret = QStringLiteral("ape");
    }
    else if (file == "AAC")
    {
        ret = QStringLiteral("aac");
    }
    else if (file == "MP4")
    {
        ret = QStringLiteral("mp4");
    }
    else if (file == "OGG")
    {
        ret = QStringLiteral("ogg");
    }

    return ret;
}

void MainWindow::convert()
{
    QListWidgetItem *item = ui->listWidget->currentItem();
    QString tmp = item->data(Qt::UserRole).toString();

    if (mProcess->isOpen())
    {
        mProcess->close();
    }

    mSourceFile.clear();
    mTargetFile.clear();
    mSource.clear();
    mTarget.clear();
    mSourceSuffix.clear();
    mTargetSuffix.clear();

    if (!tmp.isEmpty())
    {
        mTitle = item->text();
        mCmd = tmp;
        setWindowTitle(QStringLiteral("ffmpeg工具 - %1").arg(mTitle));
    }
    else
    {
        mTitle.clear();
        mCmd.clear();
        setWindowTitle(QStringLiteral("ffmpeg工具"));
    }
}

bool MainWindow::informationMessageBox(const QString &title, const QString &text, bool isOnlyOk)
{
    QMessageBox msgBox(this);
    msgBox.setFont(this->font());
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setWindowTitle(title);
    msgBox.setText(text);
    if (isOnlyOk)
    {
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setButtonText(QMessageBox::Ok, QStringLiteral("确定"));
    }
    else
    {
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setButtonText(QMessageBox::Ok, QStringLiteral("确定"));
        msgBox.setButtonText(QMessageBox::Cancel, QStringLiteral("取消"));
    }

    return (msgBox.exec() == QMessageBox::Ok);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (informationMessageBox(QStringLiteral("提示"), QStringLiteral("确定关闭"), false))
    {
        event->accept();
    }
    else
    {
        event->ignore();
    }
}

void MainWindow::on_pushButton_import_clicked()
{
    if (mTitle.isEmpty() || mCmd.isEmpty())
    {
        return;
    }

    mSource = mTitle.split(QStringLiteral("转"))[0];
    mTarget = mTitle.split(QStringLiteral("转"))[1];
    mSourceSuffix = getFileSuffix(mSource);
    mTargetSuffix = getFileSuffix(mTarget);

    if (mSourceSuffix.isEmpty() || mTargetSuffix.isEmpty())
    {
        informationMessageBox(QStringLiteral("提示"), QStringLiteral("不支持的文件格式"));
        return;
    }

    QString file = QFileDialog::getOpenFileName(this, QStringLiteral("打开%1文件").arg(mSource), QStringLiteral("."), QStringLiteral("%1文件(*.%2)").arg(mSource).arg(mSourceSuffix));
    if (!file.isEmpty())
    {
        mSourceFile = file;
        if (ui->checkBox->isChecked())
        {
            QString tmp = mSourceFile;
            mTargetFile = tmp.replace(QStringLiteral(".%1").arg(mSourceSuffix), QStringLiteral(".%1").arg(mTargetSuffix));
        }
    }
}

void MainWindow::on_pushButton_save_clicked()
{
    if (mSourceFile.isEmpty())
    {
        return;
    }

    QString file = QFileDialog::getSaveFileName(this, QStringLiteral("保存%1文件").arg(mTarget), mTargetFile, QStringLiteral("%1文件(*.%2)").arg(mTarget).arg(mTargetSuffix));
    if (!file.isEmpty())
    {
        mTargetFile = file;
    }
}

void MainWindow::on_pushButton_convert_clicked()
{
    if (mSourceFile.isEmpty() || mTargetFile.isEmpty())
    {
        return;
    }

    QString cmd = mCmd.arg(mSourceFile).arg(mTargetFile);
    ui->textBrowser->append("\n" + cmd + "\n");

    if (mProcess->isOpen())
    {
        mProcess->close();
    }

    mTmpStr.clear();
    mProcess->start(cmd);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1018726.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

GraphQL基础知识与Spring for GraphQL使用教程

文章目录 1、数据类型1.1、标量类型1.2. 高级数据类型 基本操作2、Spring for GraphQL实例2.1、项目目录2.2、数据库表2.3、GraphQL的schema.graphql2.4、Java代码 3、运行效果3.1、添加用户3.2、添加日志3.3、查询所有日志3.4、查询指定用户日志3.5、数据订阅 4、总结 GraphQL…

多输入多输出 | Matlab实现GWO-BP灰狼算法优化BP神经网络多输入多输出预测

多输入多输出 | Matlab实现GWO-BP灰狼算法优化BP神经网络多输入多输出预测 目录 多输入多输出 | Matlab实现GWO-BP灰狼算法优化BP神经网络多输入多输出预测预测效果基本介绍程序设计往期精彩参考资料 预测效果 基本介绍 多输入多输出 | Matlab实现GWO-BP灰狼算法优化BP神经网络…

python有限差分法求解一维热传导方程

​1、方程及其离散 1.1一维热传导方程 1.2离散化 设定步长&#xff0c;依据上述方程得到递推关系&#xff1a; 2、python求解实现 import numpy as np import matplotlib.pyplot as plth 0.1#空间步长 N 30#空间步数 dt 0.0001#时间步长 M 10000#时间的步数 A dt/(h**2)…

Oracle数据库体系结构(一)_概述

目录 1 Oracle系统框架 1.1 Orace SGA组成 1.2 Oracle后台进程 1.3 PGA服务进程 2 数据库与实例 2.1 数据库 2.2 实例 3 总结 Oracle公司&#xff08;甲骨文&#xff09;是全球最大的信息管理软件及服务供应商&#xff0c;成立于1977年&#xff0c;总部位于美国加…

关于rdkit 错误2w08_ligand: warning - O.co2 with non C.2 or S.o2 neighbor.

1 问题&#xff1a; 读取 PDBBindv2019的数据集&#xff0c;尝试把所有配体的mol2文件转换成对应smiles表达式。大约超过1千个出现问题。 主要问题就是‘warning - O.co2 with non C.2 or S.o2 neighbor’。 2 原因&#xff1a; Phosphate group - warning O.co2 with non C…

Rocky Linux 安装图解(替代centos)服务器+桌面

centos自从20年底转变为不稳定版本后&#xff0c;有很多替代方案 经过近3年的发展&#xff0c;rocky linux算是一个比较好的选择&#xff0c;一是依照red hat企业版来做&#xff0c;二是rocky的发起者也是centos的创始人 如果想安装debian&#xff0c;可以参考&#xff1a;deb…

微服务保护-初识Sentinel

个人名片&#xff1a; 博主&#xff1a;酒徒ᝰ. 个人简介&#xff1a;沉醉在酒中&#xff0c;借着一股酒劲&#xff0c;去拼搏一个未来。 本篇励志&#xff1a;三人行&#xff0c;必有我师焉。 本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》&#xff0c;SpringCloud…

【详细教程hexo博客搭建】2、Vercel部署并绑定自定义域名+安装Butterfly主题

2.Vercel部署与自定义域名 2.1 Vercel部署 Vercel简介&#xff1a;vercel是一个代码托管平台&#xff0c;它能够托管你的静态html界面&#xff0c;甚至能够托管你的node.js与Python服务端脚本&#xff0c;是不想买服务器的懒人的福音&#xff01; 使用Vercel部署Hexo项目步骤…

在openSUSE-Leap-15.5-DVD-x86_64中使用钉钉dingtalk_7.0.40.30829_amd64

在openSUSE-Leap-15.5-DVD-x86_64中使用钉钉dingtalk_7.0.40.30829_amd64 一、到官网下载钉钉Linux客户端 https://page.dingtalk.com/wow/z/dingtalk/simple/ddhomedownload#/ localhost:~ # ls -lh /home/suozhang/download/com.alibabainc.dingtalk_7.0.40.30829_amd64.d…

一篇文章学会正则表达式的语法

点击下方关注我&#xff0c;然后右上角点击...“设为星标”&#xff0c;就能第一时间收到更新推送啦~~~ 正则表达式&#xff08;Regular Expression&#xff09;在代码中常常简写为regex。正则表达式通常被用来检索、替换那些符合某个规则的文本&#xff0c;它是一种强大而灵活…

1. 快速体验 VSCode 和 CMake 创建 C/C++项目

1. 快速体验 VSCode 和 CMake 创建 C/C项目 本章的全部代码和markdown文件地址: CMake_Tutorial&#xff0c;欢迎互相交流. 此次介绍的内容都是针对于 Linux 操作系统上的开发过程. 1.1 安装开发工具 VSCode: 自行下载安装, 然后安装插件 Cmake:在 Ubuntu 系统上, 可以采用 ap…

[k8s] pod的创建过程

pod的创建过程 定义 Pod 的规范&#xff1a; apiVersion: v1 kind: Pod metadata:name: my-pod spec:containers:- name: my-containerimage: nginx:latest创建 Pod 对象&#xff1a; 使用 kubectl 命令行工具或其他客户端工具创建 Pod 对象&#xff1a; kubectl create -f…

【JAVA-Day15】Java 的 do-while 循环语句

Java 的 do-while 循环语句 Java 的 do-while 循环语句摘要引言一、什么是 do-while 循环语句二、do-while 循环语句的语法三、do-while 循环的优势和使用场景优势使用场景 与其他方式相比优势劣势与while循环比较与for循环比较 建议四、总结参考资料 博主 默语带您 Go to New …

90 # 实现 express 请求处理

上一节构建 layer 和 route 的关系&#xff0c;这一节来实现 express 请求处理 application.js const http require("http"); const Router require("./router");function Application() {this._router new Router(); }Application.prototype.get fu…

【C语言】【数据存储】用%u打印char类型?用char存128?

1.题目一&#xff1a; #include <stdio.h> int main() {char a -128;printf("%u\n",a);return 0; }%u 是打印无符号整型 解题逻辑&#xff1a; 1. 原反补互换&#xff0c;截断 -128 原码&#xff1a;10000000…10000000 补码&#xff1a;11111111…10000000…

【初阶数据结构】树(tree)的基本概念——C语言

目录 一、树&#xff08;tree&#xff09; 1.1树的概念及结构 1.2树的相关概念 1.3树的表示 1.4树在实际中的运用&#xff08;表示文件系统的目录树结构&#xff09; 二、二叉树的概念及结构 2.1二叉树的概念 2.2现实中真正的二叉树 2.3特殊的二叉树 2.4二叉树的性质…

【iOS】ViewController的生命周期

文章目录 前言一、UIViewController生命周期有关函数二、执行顺序注意点loadview&#xff1a; 前言 在iOS开发中UIViewController扮演者非常重要的角色&#xff0c;它是视图view和数据model的桥梁&#xff0c;通过UIViewController的管理有条不紊的将数据展示在视图上。作为UI…

XML 和 JSON 学习笔记(基础)

XML Why XML 的出现背景&#xff1a;在实际开发中&#xff0c;不同语言&#xff08;如Java、JavaScript等&#xff09;的应用程序之间数据传递的格式不同&#xff0c;导致它们进行数据交换时很困难&#xff0c;XML就应运而生了&#xff01;&#xff08;XML 是一种通用的数据交…

视频分析【video analytics】的项目的关键因素 -- 如何选择合适的摄像头,存储设备,以及AI推理硬件?

文字大纲 参考指标摄像机存储设备AI 推理硬件参考文献与学习路径参考指标 摄像机 通常的做法是将视频视为一系列图像(帧),并使用仅在图像上训练的深度神经网络模型来执行视频上的相似分析任务。在这篇论文中,我们表明,这种在图像上运行良好的深度学习模型在视频上也会运行…