-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to remux WEBM to MP4 using FFmpeg (fix #3092)
- Loading branch information
Showing
8 changed files
with
211 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "ffmpeg.h" | ||
#include <QDir> | ||
#include <QFileInfo> | ||
#include <QProcess> | ||
#include "logger.h" | ||
|
||
|
||
QString FFmpeg::version(int msecs) | ||
{ | ||
QProcess process; | ||
process.start("ffmpeg", { "-version" }); | ||
|
||
if (!process.waitForStarted(msecs)) { | ||
return ""; | ||
} | ||
if (!process.waitForFinished(msecs)) { | ||
process.kill(); | ||
return ""; | ||
} | ||
if (process.exitCode() != 0) { | ||
return ""; | ||
} | ||
|
||
const QString output = QString::fromLocal8Bit(process.readAllStandardOutput()); | ||
QString line = output.split("\n").first().trimmed(); | ||
|
||
if (line.startsWith("ffmpeg version ")) { | ||
line = line.mid(15); | ||
} | ||
|
||
const qsizetype index = line.indexOf("Copyright"); | ||
if (index != -1) { | ||
return line.left(index).trimmed(); | ||
} | ||
|
||
return line.trimmed(); | ||
} | ||
|
||
|
||
QString FFmpeg::remux(const QString &file, const QString &extension, bool deleteOriginal, int msecs) | ||
{ | ||
// Since the method takes an extension, build an absolute path to the input file with that extension | ||
const QFileInfo info(file); | ||
const QString destination = info.path() + QDir::separator() + info.completeBaseName() + "." + extension; | ||
|
||
// Ensure the operation is safe to do | ||
if (!QFile::exists(file)) { | ||
log(QStringLiteral("Cannot remux file that does not exist: `%1`").arg(file), Logger::Error); | ||
return file; | ||
} | ||
if (QFile::exists(destination)) { | ||
log(QStringLiteral("Remuxing the file `%1` would overwrite another file: `%2`").arg(file, destination), Logger::Error); | ||
return file; | ||
} | ||
|
||
QProcess process; | ||
process.start("ffmpeg", { "-n", "-loglevel", "error", "-i", file, "-c", "copy", destination }); | ||
|
||
// Ensure the process started successfully | ||
if (!process.waitForStarted(msecs)) { | ||
log(QStringLiteral("Could not start FFmpeg")); | ||
return file; | ||
} | ||
|
||
// Wait for FFmpeg to finish | ||
bool ok = process.waitForFinished(msecs); | ||
|
||
// Print stdout and stderr to the log | ||
const QString standardOutput = QString::fromLocal8Bit(process.readAllStandardOutput()).trimmed(); | ||
if (!standardOutput.isEmpty()) { | ||
log(QString("[Exiftool] %1").arg(standardOutput), Logger::Debug); | ||
} | ||
const QString standardError = QString::fromLocal8Bit(process.readAllStandardError()).trimmed(); | ||
if (!standardError.isEmpty()) { | ||
log(QString("[Exiftool] %1").arg(standardError), Logger::Error); | ||
} | ||
|
||
// On success, delete the original file if requested | ||
if (ok && deleteOriginal) { | ||
QFile::remove(file); | ||
} | ||
|
||
return destination; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef FFMPEG_H | ||
#define FFMPEG_H | ||
|
||
#include <QString> | ||
|
||
|
||
class FFmpeg | ||
{ | ||
public: | ||
/** | ||
* Get the version of FFmpeg. | ||
* | ||
* @param msecs The duration to wait in milliseconds for the version command to run. | ||
* @return The version number found, with basic parsing done (ex: "4.4.3"). | ||
*/ | ||
static QString version(int msecs = 30000); | ||
|
||
/** | ||
* Remux a file to a different format, copying the streams. | ||
* | ||
* @param file The file to remux. | ||
* @param extension The target extension (ex: "mp4"). | ||
* @param deleteOriginal Whether to delete the original file on success. | ||
* @param msecs The duration to wait in milliseconds for the command to run. | ||
* @return The destination file path on success, the original file path on error. | ||
*/ | ||
static QString remux(const QString &file, const QString &extension, bool deleteOriginal = true, int msecs = 30000); | ||
}; | ||
|
||
#endif // FFMPEG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters