Qt audio and video development 11-ffmpeg common commands

I. Introduction

Most of the format conversion tools, such as formatting factories, use ffmpeg to process. The executable files such as ffmpeg.exe, ffplay.exe, ffprobe.exe, etc. generated after ffmpeg compilation actually encapsulate many awesome functions. ffprobe is a tool for viewing media file header information, ffplay is a tool for playing media files, especially ffmpeg.exe, a powerful media file conversion tool that can convert any media file, and can also use its own AudioFilter and VideoFliter for processing and editing, For example, some of the following functions.

  1. List the supported formats: ffmpeg -formats
  2. Cut a piece of media file: ffmpeg -i input.mp4 -ss 00:00:50.0 -codec copy -t 20 output.mp4
  3. Extract audio files in a video file: ffmpeg -i input.mp4 -vn -acodec copy output.m4a
  4. The video is muted, that is, only the video is kept: ffmpeg -i input.mp4 -an -vcodec copy output.mp4
  5. Extract the video stream from the MP4 file and export it as raw H264 data: ffmpeg -i output.mp4 -an -vcodec copy -bsf:v h264_mp4toannexb output.h264
  6. Use AAC audio data and H264 video to generate MP4 files: ffmpeg -i test.aac -i test.h264 -acodec copy -bsf:a aac_adtstoasc -vcodec copy -f mp4 output.mp4
  7. Audio format conversion: ffmpeg -i input.wav -acodec libfdk_aac output.aac
  8. Convert an MP4 file to a GIF animation: ffmpeg -i input.mp4 -vf scale=100:-1 -t 5 -r 10 image.gif

Since these tools are so awesome, can you consider making a function to directly call these executable files for processing? Of course, QProcess is on the scene, he can directly call the executable program or directly execute the command, and then it can intercept the output and print information, and read it out in the form of a pipeline. This is very intuitive. It can print when the executable file is called for execution. All information is output.

2. Features

  1. Multi-threaded real-time playback of video stream + local video + USB camera, etc.
  2. Support windows+linux+mac, support ffmpeg3 and ffmpeg4, support 32-bit and 64-bit.
  3. Multi-threaded display images, not stuck in the main interface.
  4. Reconnect the webcam automatically.
  5. You can set the border size, offset and border color.
  6. You can set whether to draw OSD labels, that is, label text or pictures and label positions.
  7. Two OSD positions and styles can be set.
  8. Can set whether to save to file and file name.
  9. You can directly drag the file to the ffmpegwidget control to play.
  10. Support common video streams such as h265 video stream + rtmp.
  11. Can pause and resume playing.
  12. Support storage of single video files and timing storage of video files.
  13. Customize the top floating bar, send a click signal notification, and set whether to enable it.
  14. You can set the screen stretch fill or equal proportion fill.
  15. You can set the decoding to be speed priority, quality priority, and equalization processing.
  16. Can take screenshots (original pictures) and screenshots of videos.
  17. Video file storage supports bare stream and MP4 files.
  18. Supports hard decoding such as qsv, dxva2, d3d11va, etc.
  19. Support OpenGL to draw video data, very low CPU usage.
  20. Support embedded linux, just cross compile.

Three, renderings

Insert picture description here

Four, related sites

  1. Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo
  2. International site: https://github.com/feiyangqingyun/QWidgetDemo
  3. Personal homepage: https://blog.csdn.net/feiyangqingyun
  4. Zhihu Homepage: https://www.zhihu.com/people/feiyangqingyun/
  5. Experience address: https://blog.csdn.net/feiyangqingyun/article/details/97565652

Five, the core code

FFmpegTool::FFmpegTool(QObject *parent) : QObject(parent)
{
    //绑定信号槽
    connect(&process, SIGNAL(started()), this, SIGNAL(started()));
    connect(&process, SIGNAL(finished(int)), this, SIGNAL(finished()));
    connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(readData()));
    process.setProcessChannelMode(QProcess::MergedChannels);
}

void FFmpegTool::readData()
{
    QString data = process.readAllStandardOutput();
    emit receiveData(data);
}

void FFmpegTool::start(const QString &command)
{
    process.start(command);
}

void FFmpegTool::start(const QString &program, const QStringList &arguments)
{
    process.start(program, arguments);
}

void FFmpegTool::getMediaInfo(const QString &mediaFile, bool json)
{
    //ffprobe -print_format json -show_streams d:/out.mp4
    //不同平台可执行文件路径改成自己的
    QString jsonArg = "-print_format json -show_streams";
    QString binFile = qApp->applicationDirPath() + "/ffprobe.exe";
    QString cmd = QString("%1 %2 %3").arg(binFile).arg(json ? jsonArg : "").arg(mediaFile);
    start(cmd);
}

void FFmpegTool::h264ToMp4ByCmd(const QString &h264File, const QString &aacFile, const QString &mp4File)
{
    if (!QFile(h264File).exists() || mp4File.isEmpty()) {
        return;
    }

    //具体参数可以参考 https://www.cnblogs.com/renhui/p/9223969.html
    //ffmpeg.exe -y -i d:/1.aac -i d:/1.mp4 -map 0:0 -map 1:0 d:/out.mp4
    //-y参数表示默认yes覆盖文件
    //不同平台可执行文件路径改成自己的
    QString binFile = qApp->applicationDirPath() + "/ffmpeg.exe";

    //下面两种方法都可以,怎么方便怎么来
#if 0
    QString cmd = QString("%1 -y -i %2 -i %3 -map 0:0 -map 1:0 %4").arg(binFile).arg(h264File).arg(aacFile).arg(mp4File);
    start(cmd);
#else
    QStringList args;
    args << "-y";
    args << "-i" << h264File;

    //如果存在音频文件则添加
    if (QFile(aacFile).exists()) {
        args << "-i" << aacFile;
    }

    //args << "-map" << "0:0";
    //args << "-map" << "1:0";
    args << mp4File;
    start(binFile, args);
#endif
}

Guess you like

Origin blog.csdn.net/feiyangqingyun/article/details/108036865