Local media files to obtain information by way of calling an external program (ffmpeg.exe) of

  Before blogger Bowen " FFmpeg decapsulation (demultiplexing) and acquires information of the local media file ", there is described the use of the API FFmpeg media file information acquisition method. But the FFmpeg API is relatively complex, if not engineers specializing in the development of audio and video, may not be a good use of the inside of the API. Herein to obtain information of the media file as an example, describes another approach using FFmpeg --------- by invoking an external program (ffmpeg.exe) manner using FFmpeg. Using this method does not require programmers to understand the FFmpeg api, just need to know some of FFmpeg command to the media file for processing. This method is described below:

 

First, the principle:

  FIG next file folder, and the presence ffmpeg.exe video1.mp4.

 

Command prompt to the directory shown in the figure, the command:

ffmpeg -hide_banner -i video1.mp4

It can be obtained as shown below in the media file information video1.mp4

 

As long as the programmer Run "ffmpeg -hide_banner -i video1.mp4" (such as may be performed by a command of the start function in Qt QProcess) information is stored, at the command prompt to be output by way of calling external program in the program the variables of the program, you can get the information media files in the program

 

Second, the code

Development environment for windows7 64 Wei, Qt5.9.0 + vs2015

Part of the code as follows:

 

QtGuiApplication2.h

#pragma once

#include <QtWidgets/QMainWindow>
#include <QProcess>
#include "ui_QtGuiApplication2.h"

class QtGuiApplication2 : public QMainWindow
{
	Q_OBJECT

public:
	QtGuiApplication2(QWidget *parent = Q_NULLPTR);
public slots:
	void ReadMediaFileInformation();
	void ProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
private:
	Ui::QtGuiApplication2Class ui;
	QProcess *m_proc;
	std::string m_FFmpegBuf;   //保存FFmpeg的输出信息
	QString m_MediaFileName;   //要获取信息的媒体文件的名称
};

 

QtGuiApplication2.cpp

#include "QtGuiApplication2.h"

using namespace std;

QtGuiApplication2::QtGuiApplication2(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	m_proc = new QProcess(this);
	m_proc->setProcessChannelMode(QProcess::MergedChannels);
	connect(m_proc, SIGNAL(readyReadStandardOutput()), this, SLOT(ReadMediaFileInformation()));
	connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ProcessFinished(int, QProcess::ExitStatus)));
	m_MediaFileName = "video1.mp4";
	QString cmd = "ffmpeg -hide_banner -i " + m_MediaFileName;
        if (m_proc->state() != m_proc->NotRunning)
	{
		m_proc->waitForFinished(20000);
	}
	m_proc->start(cmd); //启动外部进程(执行命令ffmpeg -hide_banner -i video1.mp4)
}

//当外部进程产生的新的数据时该函数会被调用
void QtGuiApplication2::ReadMediaFileInformation()
{
	QString buf(QString::fromLocal8Bit(m_proc->read(10000)));
	string s = buf.toStdString();
	if (0 == s.length())
	{
		return;
	}
	m_FFmpegBuf += s;
}

//当外部进程结束时,该函数会被调用
void QtGuiApplication2::ProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
	qDebug("m_FFmpegBuf:%s", m_FFmpegBuf.c_str());
	int pos1,pos2;
	string bitrate;
	string duration;
	pos1 = m_FFmpegBuf.find("bitrate: ");
	pos2 = m_FFmpegBuf.find(" kb/s");
	if (pos1>=0 && pos2>=0)
	{
		bitrate = m_FFmpegBuf.substr(pos1+strlen("bitrate: "), (pos2+strlen(" kb/s")) - (pos1+strlen("bitrate: ")));  //该媒体文件的平均码率
	}

	pos1 = m_FFmpegBuf.find("Duration: ");
	pos2 = m_FFmpegBuf.find(", start:");
	if (pos1 >= 0 && pos2 >= 0)
	{
		duration = m_FFmpegBuf.substr(pos1 + strlen("Duration: "), pos2 - (pos1 + strlen("Duration: "))); //该媒体文件总时长
	}

	ui.LabelMediaFileName->setText(m_MediaFileName);
	ui.Labelbitrate->setText(QString::fromStdString(bitrate));
	ui.LabelDuration->setText(QString::fromStdString(duration));
}

 

The effect of executing the program are as follows:

I can see you can get the media file information video1.mp4 (rate, duration)

 

Third, the comparison of FFmpeg used in two ways:

1, call ffmpeg.exe by way of external processes:

Advantages: simple operation, only need to know the FFmpeg command to the media file for processing. It is mainly used in the case of audio and video processing execution time critical, such as some video editor encapsulation format conversion on the market, combined video and audio, audio and video separation operation is achieved in this way.

缺点:执行效率低,时间开销大。有些音视频操作是只能用FFmpeg的API完成而无法通过调用ffmpeg.exe代替的,比如实现一个播放器。

 

2、通过调用FFmpeg的API的方式:

优点:灵活,速度快,效率高。能通过外部进程调用ffmpeg.exe的方式实现的操作就必然可以通过调用FFmpeg的API的方式实现。

缺点:使用比较复杂,对程序员的要求较高。

 

四、资源文件下载

上面演示的所有源码,完整的工程可以在https://download.csdn.net/download/u014552102/11859971下载,欢迎各位下载

发布了54 篇原创文章 · 获赞 55 · 访问量 12万+

Guess you like

Origin blog.csdn.net/u014552102/article/details/102535491