In this post I’m going to describe how you can use FFmpeg library to convert videos in your Qt applications, or even write a Video Conversion program that uses FFmpeg as its underlying powerful conversion engine.
Unfortunately you can’t use Qt Framework out-of-the-box to convert video files and formats to each other (at least that is the case until the time this post was published, or in other words until Qt5.10.1). One of the most practical workarounds for this missing capability is using 3rd party video conversion libraries and tools such as FFmpeg.
To be able to do this, you can download FFmpeg (according to your operating system) and use its ffmpeg tool in conjunction with QProcess class. Note that you need to extract it to a valid and accessible path, such as the same folder as the application executable.
FFmpeg by default contains the following tools:
What we need, as I mentioned above, is the ffmpeg tool (i.e. ffmpeg.exe on Windows version). Here is how you can use it, in the most simple form:
QString program = "ffmpeg"; // or the path to ffmpeg QStringList arguments; arguments << "-i" << inputFile << outputFile; QProcess *ffmpegProc = new QProcess(this); // this can be replaced by a valid parent ffmpegProc->start(program, arguments);
In the preceding code sample, inputFile must be replaced with the input video file name and path, and outputFile must be replaced with the desired output video file name and path. Preceding code sample will start a video conversion with the default parameters. If you want to customize the conversion, you need to use another set of arguments. Here’s an example set of arguments that will cause a more compressed output video file, although it will take longer to convert the video:
arguments << "-i" << inputFile << "-preset" << "veryslow" << outputFile;
veryslow can be replaced with values such as ultrafast, faster, slow and so on. Here’s a link with more information about this.
Another example that demonstrates setting the audio and video codec and encoding bitrates:
arguments << "-i" << inputFile << "-c:v" << "libx265" << "-b:v" << "2500k" << "-c:a" << "aac" << "-b:a" << "128k" << outputFile;
In the preceding example, libx265 represents the video codec, which can be replaced by any other video codec supported by FFmpeg. Similarly, aac is the audio codec. Obviously, 2500k and 128k are video and audio bitrates respectively.
Make sure to check out FFmpeg documentation for a lot more functionality and different sets of possible arguments.