QProcess 调用 ffmpeg来处理音频

news/2024/11/23 2:40:41/

项目场景:

在文章 qt 实现音视频的分贝检测系统中,实现的是边播放变解析音频数据来统计音频的分贝大小,并不满足实际项目的需求,有的视频声音正常,有的视频声音就偏低,即使放到最大音量声音也是比较小,本文的目的是直接通过对本地视频进行检测,拿出关键指标,来进行对音频处理

关键依赖:ffmpeg

因为依赖于ffmpeg的能力,所以第一步要安装ffmpeg环境,自行百度

步骤1,检测max_volume 值是否小于0dB

 ffmpeg -i 1.mp3  -filter_complex volumedetect -c:v copy -f null /dev/null

在这里插入图片描述
其中检测的关键指标就是 max_volume, 本次检测和处理的目标就是把 max_volume的值给提高到0dB;

步骤2,如何区分是音频文件还是视频文件

可以有多种方法,1可以通过后缀名来区分; 2 因为代码是用qt写的,可以用qt来实现,代码如下

QFileInfo f(1.mp3”);
if(f.completeSuffix() == "mp3" || f.completeSuffix() == "aac" || 
f.completeSuffix() == "amr" || f.completeSuffix() == "wav" || f.completeSuffix() == "wma" )
QMediaPlayer p;
p.audioAvailable()

步骤3,处理音频,使max_volume的值接近0dB

音频文件用以下指令

ffmpeg -i 1.mp3-af  "volume=5.8dB"  out.mp3

如果是视频文件,用以下指令:(保持视频信息不变,当然还设计到音频的编码格式)

ffmpeg -i 1.mp3-af  "volume=5.8dB" -c:v copy -c:a aac out.mp3

输出如下
在这里插入图片描述
在这里插入图片描述

程序设计思路

通过QProcess 调用 ffmpeg指令,检测max_volume小于0的文件,拿到文件列表,再通过ffmpeg指令来提高音频。关键代码如下:

dbdetectthread.h

#ifndef DBDETECTTHREAD_H
#define DBDETECTTHREAD_H#include <QObject>
#include <QThread>
#include <QProcess>
#include <QStringList>class DbDetectThread : public QThread
{Q_OBJECT
public:explicit DbDetectThread(QObject *parent = nullptr);void setList(const QStringList &list);virtual void run();
signals:void sigPath(const QString &p, float db);void sigMsg(const QString &p);void sigEnd();public slots:void readStandardOutput();void readStandardError();void processFinished(int exitCode, QProcess::ExitStatus exitStatus);void threadFinished(); //线程退出private:QProcess *pCmdProcess;QStringList pathlist;QString curFile;
};#endif // DBDETECTTHREAD_H

dbdetectthread.cpp

#include "dbdetectthread.h"
#include <QDebug>
DbDetectThread::DbDetectThread(QObject *parent): QThread{parent}
{qRegisterMetaType<QProcess::ExitStatus>("QProcess::ExitStatus");
}void DbDetectThread::setList(const QStringList &list)
{pathlist = list;
}//ffmpeg -i 2.wav -filter_complex volumedetect -c:v copy -f null /dev/null//需要计算分贝相差值
//ffmpeg  -i 2.wav -filter:a "volume=80dB" output2.wavvoid DbDetectThread::run()
{pCmdProcess = new QProcess();    //不要加thisconnect(pCmdProcess, &QProcess::readyReadStandardOutput, this, &DbDetectThread::readStandardOutput);connect(pCmdProcess, &QProcess::readyReadStandardError, this, &DbDetectThread::readStandardError);connect(pCmdProcess, QOverload<int , QProcess::ExitStatus >::of(&QProcess::finished), this, &DbDetectThread::processFinished);connect(this, &QThread::finished, this, &DbDetectThread::threadFinished);QStringList arguments;//arguments << "-i" << "C:/Users/wmm/Desktop/DbDetect/voice/input.mp4" << "-c:v" << "h264" << "-c:a" << "aac" << "C:/Users/wmm/Desktop/DbDetect/voice/output.mp4";//arguments << "-i" << "input.mp4" << "-c:v" << "h264" << "-c:a" << "aac" << "output.mp4";//arguments << "-i" << p << "-filter_complex"<< "volumedetect" <<  "-c:v" << "copy" << "-f" << "null" << "/dev/null";//QString cmd = QString("ffmpeg -i %1 -filter_complex volumedetect -c:v copy -f null /dev/null").arg(p);//QString cmd = QString("ffmpeg -i C:/Users/wmm/Desktop/DbDetect/voice/input.mp4 -filter_complex volumedetect -c:v copy -f null /dev/null");//QString cmd = "ping www.baidu.com -w 500";//QString result;foreach(auto p, pathlist){curFile = p;qDebug() << "start detect " << curFile;QString cmd = QString("ffmpeg -i %1 -filter_complex volumedetect -c:v copy -f null /dev/null").arg(p);pCmdProcess->start(cmd/*,arguments*/);pCmdProcess->waitForFinished();sleep(1);}}void DbDetectThread::readStandardOutput() {qDebug() << "Standard output: " << pCmdProcess->readAllStandardOutput();
}void DbDetectThread::readStandardError() {QString errorMessage = QString::fromUtf8(pCmdProcess->readAllStandardError());QStringList errlist = errorMessage.split("\r\n");//qDebug() << errlist.size();//qDebug() << errlist;//    float num = 3.1415926;
//    QString str = QString::number(num, 'f', 2);foreach (auto p, errlist) {if(p.contains("max_volume")){qDebug() << p;int pos = p.indexOf("max_volume");int pos2 = p.indexOf("dB",pos);QString val = p.mid(pos+11,pos2-11-pos);qDebug() <<val;float fval = val.toFloat();qDebug() << fval;if(fval <0){qDebug() << "小于0";emit sigPath(curFile,fval);}else{qDebug() << "不小于0";}}}// 对 errorMessage 进行解析和处理...//qDebug() << "Standard error: " << pCmdProcess->readAllStandardError();
}void DbDetectThread::processFinished(int exitCode, QProcess::ExitStatus exitStatus) {qDebug() << "Process finished with exit code" << exitCode << "and exit status" << exitStatus << ((QProcess*)QObject::sender())->arguments();emit sigMsg(QString("%1 检测完成,exitCode = %2, exitStatus = %3 ").arg(curFile).arg(exitCode).arg(exitStatus));
}void DbDetectThread::threadFinished()
{qDebug() << __func__;emit sigEnd();pCmdProcess->deleteLater();
}

dbprocessthread.h

#ifndef DBPROCESSTHREAD_H
#define DBPROCESSTHREAD_H#include <QObject>
#include <QThread>
#include <QProcess>
#include <QMap>
#include <QMediaPlayer>
class DbProcessThread : public QThread
{Q_OBJECT
public:explicit DbProcessThread(QObject *parent = nullptr);void setOutputDir(const QString &dir);void setMap(const QMap<QString,float> &map);virtual void run();
signals:void sigPath(const QString &p, float db);void sigMsg(const QString &p);void sigEnd();public slots:void readStandardOutput();void readStandardError();void processFinished(int exitCode, QProcess::ExitStatus exitStatus);void threadFinished(); //线程退出private:QProcess *pCmdProcess;QMediaPlayer  mediaPlayer;QMap<QString,float> m_needProcessMap;QString m_outputDir;QString m_curSrcFile;QString m_curDesFile;
};#endif // DBPROCESSTHREAD_H

dbprocessthread.cpp

#include "dbprocessthread.h"
#include <QDebug>
#include <QFileInfo>
DbProcessThread::DbProcessThread(QObject *parent): QThread{parent}
{}void DbProcessThread::setOutputDir(const QString &dir)
{m_outputDir = dir;
}void DbProcessThread::setMap(const QMap<QString, float> &map)
{m_needProcessMap = map;
}void DbProcessThread::run()
{pCmdProcess = new QProcess();    //不要加thisconnect(pCmdProcess, &QProcess::readyReadStandardOutput, this, &DbProcessThread::readStandardOutput);connect(pCmdProcess, &QProcess::readyReadStandardError, this, &DbProcessThread::readStandardError);connect(pCmdProcess, QOverload<int , QProcess::ExitStatus >::of(&QProcess::finished), this, &DbProcessThread::processFinished);connect(this, &QThread::finished, this, &DbProcessThread::threadFinished);QMapIterator<QString, float> i(m_needProcessMap);while (i.hasNext()) {i.next();QFileInfo f(i.key());qDebug() << f.fileName() << m_outputDir+"/" + f.fileName();m_curSrcFile = i.key();m_curDesFile = m_outputDir+"/" + f.fileName();QStringList arguments;//arguments << "-i" << i.key() << "-c:v" << "h264" << "-c:a" << "aac" << m_outputDir+"/" + f.fileName();arguments << "-i" << i.key() << "-af" << QString("volume=%1dB").arg(qAbs(i.value())) << "-c:v copy" << " -c:a aac "  << m_outputDir+"/" + f.fileName();qDebug() << arguments;emit sigMsg(QString("正在处理...%1").arg(m_curSrcFile));if(f.completeSuffix() == "mp3" || f.completeSuffix() == "aac" || f.completeSuffix() == "amr" || f.completeSuffix() == "wav"\|| f.completeSuffix() == "wma" ){pCmdProcess->start(QString("ffmpeg -i %1 -af \"volume=%2dB\"  %3").arg(i.key()).arg(qAbs(i.value())).arg(m_outputDir+"/" + f.fileName())/*,arguments*/);}else {pCmdProcess->start(QString("ffmpeg -i %1 -af \"volume=%2dB\" -c:v copy  %3").arg(i.key()).arg(qAbs(i.value())).arg(m_outputDir+"/" + f.fileName())/*,arguments*/);}//pCmdProcess->start(QString("ffmpeg -i %1 -af \"volume=%2dB\" -c:v copy -c:a aac %3").arg(i.key()).arg(qAbs(i.value())).arg(m_outputDir+"/" + f.fileName())/*,arguments*/);pCmdProcess->waitForFinished();msleep(500);}}void DbProcessThread::readStandardOutput() {qDebug() << "Standard output: " << pCmdProcess->readAllStandardOutput();
}void DbProcessThread::readStandardError() {qDebug() << "Standard error: " << pCmdProcess->readAllStandardError();
}void DbProcessThread::processFinished(int exitCode, QProcess::ExitStatus exitStatus) {qDebug() << "Process finished with exit code" << exitCode << "and exit status" << exitStatus << ((QProcess*)QObject::sender())->arguments();emit sigMsg(QString("%1 处理完成,文件保存为%2,exitCode = %3, exitStatus = %4 ").arg(m_curSrcFile).arg(m_curDesFile).arg(exitCode).arg(exitStatus));
}void DbProcessThread::threadFinished()
{qDebug() << __func__;emit sigEnd();pCmdProcess->deleteLater();
}

效果如图:
在这里插入图片描述
代码上传到此 https://download.csdn.net/download/u011942101/88299291


http://www.ppmy.cn/news/1087126.html

相关文章

WordPress Page Builder KingComposer 2.9.6 Open Redirection

WordPress Page Builder KingComposer 2.9.6 Open Redirection WordPress 插件 KingComposer 版本2.9.6 以及以前版本受到开放重定向漏洞的影响。该漏洞在packetstorm网站披露于2023年7月24日&#xff0c;除了该漏洞&#xff0c;该版本的插件还存在XSS攻击的漏洞风险 图1.来自…

Golang 中return和defer执行先后顺序

先给出最终结论&#xff1a; 执行return语句 -> 执行defer函数 -> 函数返回 这里可能会有一个疑问&#xff0c; 执行return语句和函数返回难道不是一回事? Golang语言中函数的return不是原子操作&#xff0c;而是分为了两步&#xff1a; 返回值赋值真正函数返回 Gol…

Linux系统gdb调试常用命令

GDB&#xff08;GNU调试器&#xff09;是一款常用的调试工具&#xff0c;用于调试C、C等编程语言的程序。以下是一些常用的GDB命令&#xff1a; 1. 启动程序&#xff1a; - gdb <executable>&#xff1a;启动GDB调试器&#xff0c;并加载可执行文件。 2. 设置断点&a…

一个简单的Python DNS客户端

import socketdef dns_lookup(domain):# 定义DNS服务器地址和端口server socket.gethostbyname(dns.google)port 53# 构建DNS查询消息question b\\0 * 2 domain.encode(utf-8) b\0message b\x01\x00\x01\x00\x01\x00\x00\x00\x00\x01 question b\x00\x00\x00\x00# 连接…

人体呼吸存在传感器成品,毫米波雷达探测感知技术,引领智能家居新潮流

随着科技的不断进步和人们生活质量的提高&#xff0c;智能化家居逐渐成为一种时尚和生活方式。 人体存在传感器作为智能家居中的重要组成部分&#xff0c;能够实时监测环境中人体是否存在&#xff0c;为智能家居系统提供更加精准的控制和联动。 在这个充满创新的时代&#xf…

无锡布里渊——厘米级分布式光纤-锅炉安全监测解决方案

无锡布里渊——厘米级分布式光纤-锅炉安全监测解决方案 厘米级分布式光纤-锅炉安全监测解决方案 1、方案背景与产品简介&#xff1a; 1.1&#xff1a;背景简介&#xff1a; 锅炉作为一种把煤、石油或天燃气等化石燃料所储藏的化学能转换成水或水蒸气的热能的重要设备&#xff…

基于SSM的新能源汽车在线租赁系统

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用Vue技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

9-AJAX-2综合案例

AJAX-综合案例 目录 案例-图书管理图片上传案例-网站换肤案例-个人信息设置 学习目标 今天主要就是练&#xff0c;巩固 axios 的使用 完成案例-图书管理系统&#xff08;增删改查&#xff09;经典业务掌握图片上传的思路完成案例-网站换肤并实现图片地址缓存完成案例-个人信…