2016-07-29 17:35:10 +12:00
|
|
|
|
#include "tiio_ffmpeg.h"
|
|
|
|
|
#include "../toonz/tapp.h"
|
|
|
|
|
#include "tsystem.h"
|
|
|
|
|
#include "tsound.h"
|
2020-07-23 17:23:55 +12:00
|
|
|
|
#include "tenv.h"
|
2016-07-29 17:35:10 +12:00
|
|
|
|
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QtGui/QImage>
|
2016-10-06 21:22:00 +13:00
|
|
|
|
#include <QRegExp>
|
2016-07-29 17:35:10 +12:00
|
|
|
|
#include "toonz/preferences.h"
|
|
|
|
|
#include "toonz/toonzfolders.h"
|
2017-05-31 21:29:20 +12:00
|
|
|
|
#include "tmsgcore.h"
|
2016-07-29 17:35:10 +12:00
|
|
|
|
|
|
|
|
|
Ffmpeg::Ffmpeg() {
|
2021-03-15 11:19:57 +13:00
|
|
|
|
m_ffmpegPath = Preferences::instance()->getFfmpegPath();
|
|
|
|
|
m_ffmpegTimeout = Preferences::instance()->getFfmpegTimeout();
|
2021-03-16 11:17:06 +13:00
|
|
|
|
if (m_ffmpegTimeout > 0)
|
2021-04-12 16:04:39 +12:00
|
|
|
|
m_ffmpegTimeout *= 1000;
|
2021-03-16 11:17:06 +13:00
|
|
|
|
else
|
|
|
|
|
m_ffmpegTimeout = -1;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
std::string strPath = m_ffmpegPath.toStdString();
|
|
|
|
|
m_intermediateFormat = "png";
|
|
|
|
|
}
|
|
|
|
|
Ffmpeg::~Ffmpeg() {}
|
|
|
|
|
|
|
|
|
|
bool Ffmpeg::checkFfmpeg() {
|
2021-03-14 12:39:35 +13:00
|
|
|
|
QString exe = "ffmpeg";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
#if defined(_WIN32)
|
2021-03-14 12:39:35 +13:00
|
|
|
|
exe = exe + ".exe";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
#endif
|
2021-03-14 12:39:35 +13:00
|
|
|
|
|
|
|
|
|
// check the user defined path in preferences first
|
|
|
|
|
QString path = Preferences::instance()->getFfmpegPath() + "/" + exe;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
if (TSystem::doesExistFileOrLevel(TFilePath(path))) return true;
|
|
|
|
|
|
2021-03-14 12:39:35 +13:00
|
|
|
|
// Let's try and autodetect the exe included with release
|
|
|
|
|
QStringList folderList;
|
|
|
|
|
|
|
|
|
|
folderList.append(".");
|
|
|
|
|
folderList.append("./ffmpeg"); // ffmpeg folder
|
2020-10-11 07:06:49 +13:00
|
|
|
|
|
2020-07-23 17:23:55 +12:00
|
|
|
|
#ifdef MACOSX
|
2021-03-14 12:39:35 +13:00
|
|
|
|
// Look inside app
|
|
|
|
|
folderList.append("./" +
|
|
|
|
|
QString::fromStdString(TEnv::getApplicationFileName()) +
|
|
|
|
|
".app/ffmpeg"); // ffmpeg folder
|
2020-05-23 02:17:41 +12:00
|
|
|
|
#elif defined(LINUX) || defined(FREEBSD)
|
2021-03-14 12:39:35 +13:00
|
|
|
|
// Need to account for symbolic links
|
|
|
|
|
folderList.append(TEnv::getWorkingDirectory().getQString() +
|
|
|
|
|
"/ffmpeg"); // ffmpeg folder
|
2020-07-23 17:23:55 +12:00
|
|
|
|
#endif
|
2016-07-29 17:35:10 +12:00
|
|
|
|
|
2021-03-14 12:39:35 +13:00
|
|
|
|
QString exePath = TSystem::findFileLocation(folderList, exe);
|
|
|
|
|
|
|
|
|
|
if (!exePath.isEmpty()) {
|
|
|
|
|
Preferences::instance()->setValue(ffmpegPath, exePath);
|
2020-10-11 07:06:49 +13:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-03-14 12:39:35 +13:00
|
|
|
|
|
2020-10-11 07:06:49 +13:00
|
|
|
|
// give up
|
2016-07-29 17:35:10 +12:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Ffmpeg::checkFfprobe() {
|
2021-03-14 12:39:35 +13:00
|
|
|
|
QString exe = "ffprobe";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
#if defined(_WIN32)
|
2021-03-14 12:39:35 +13:00
|
|
|
|
exe = exe + ".exe";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
#endif
|
2021-03-14 12:39:35 +13:00
|
|
|
|
|
|
|
|
|
// check the user defined path in preferences first
|
|
|
|
|
QString path = Preferences::instance()->getFfmpegPath() + "/" + exe;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
if (TSystem::doesExistFileOrLevel(TFilePath(path))) return true;
|
|
|
|
|
|
2021-03-14 12:39:35 +13:00
|
|
|
|
// Let's try and autodetect the exe included with release
|
|
|
|
|
QStringList folderList;
|
|
|
|
|
|
|
|
|
|
folderList.append(".");
|
|
|
|
|
folderList.append("./ffmpeg"); // ffmpeg folder
|
2020-10-11 07:06:49 +13:00
|
|
|
|
|
2020-07-23 17:23:55 +12:00
|
|
|
|
#ifdef MACOSX
|
2021-03-14 12:39:35 +13:00
|
|
|
|
// Look inside app
|
|
|
|
|
folderList.append("./" +
|
|
|
|
|
QString::fromStdString(TEnv::getApplicationFileName()) +
|
|
|
|
|
".app/ffmpeg"); // ffmpeg folder
|
2020-05-23 02:17:41 +12:00
|
|
|
|
#elif defined(LINUX) || defined(FREEBSD)
|
2021-03-14 12:39:35 +13:00
|
|
|
|
// Need to account for symbolic links
|
|
|
|
|
folderList.append(TEnv::getWorkingDirectory().getQString() +
|
|
|
|
|
"/ffmpeg"); // ffmpeg folder
|
2020-07-23 17:23:55 +12:00
|
|
|
|
#endif
|
2016-07-29 17:35:10 +12:00
|
|
|
|
|
2021-03-14 12:39:35 +13:00
|
|
|
|
QString exePath = TSystem::findFileLocation(folderList, exe);
|
|
|
|
|
|
|
|
|
|
if (!exePath.isEmpty()) {
|
|
|
|
|
Preferences::instance()->setValue(ffmpegPath, exePath);
|
2020-10-11 07:06:49 +13:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-09-23 15:03:16 +12:00
|
|
|
|
|
2020-10-11 07:06:49 +13:00
|
|
|
|
// give up
|
2016-07-29 17:35:10 +12:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Ffmpeg::checkFormat(std::string format) {
|
2016-08-04 19:23:36 +12:00
|
|
|
|
QString path = Preferences::instance()->getFfmpegPath() + "/ffmpeg";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
#if defined(_WIN32)
|
2016-08-04 19:23:36 +12:00
|
|
|
|
path = path + ".exe";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
#endif
|
2016-08-04 19:23:36 +12:00
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-formats";
|
|
|
|
|
QProcess ffmpeg;
|
|
|
|
|
ffmpeg.start(path, args);
|
|
|
|
|
ffmpeg.waitForFinished();
|
|
|
|
|
QString results = ffmpeg.readAllStandardError();
|
|
|
|
|
results += ffmpeg.readAllStandardOutput();
|
|
|
|
|
ffmpeg.close();
|
|
|
|
|
std::string strResults = results.toStdString();
|
|
|
|
|
std::string::size_type n;
|
|
|
|
|
n = strResults.find(format);
|
|
|
|
|
if (n != std::string::npos)
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TFilePath Ffmpeg::getFfmpegCache() {
|
|
|
|
|
QString cacheRoot = ToonzFolder::getCacheRootFolder().getQString();
|
|
|
|
|
if (!TSystem::doesExistFileOrLevel(TFilePath(cacheRoot + "/ffmpeg"))) {
|
|
|
|
|
TSystem::mkDir(TFilePath(cacheRoot + "/ffmpeg"));
|
|
|
|
|
}
|
|
|
|
|
std::string ffmpegPath =
|
|
|
|
|
TFilePath(cacheRoot + "/ffmpeg").getQString().toStdString();
|
|
|
|
|
return TFilePath(cacheRoot + "/ffmpeg");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::setFrameRate(double fps) { m_frameRate = fps; }
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::setPath(TFilePath path) { m_path = path; }
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::createIntermediateImage(const TImageP &img, int frameIndex) {
|
2016-10-07 23:02:02 +13:00
|
|
|
|
m_frameCount++;
|
2017-06-01 22:50:43 +12:00
|
|
|
|
if (m_frameNumberOffset == -1) m_frameNumberOffset = frameIndex - 1;
|
2016-11-21 23:37:55 +13:00
|
|
|
|
QString tempPath = getFfmpegCache().getQString() + "//" +
|
|
|
|
|
QString::fromStdString(m_path.getName()) + "tempOut" +
|
2017-06-01 22:50:43 +12:00
|
|
|
|
QString::number(frameIndex - m_frameNumberOffset) + "." +
|
|
|
|
|
m_intermediateFormat;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
std::string saveStatus = "";
|
|
|
|
|
TRasterImageP tempImage(img);
|
|
|
|
|
TRasterImage *image = (TRasterImage *)tempImage->cloneImage();
|
|
|
|
|
|
|
|
|
|
m_lx = image->getRaster()->getLx();
|
|
|
|
|
m_ly = image->getRaster()->getLy();
|
|
|
|
|
m_bpp = image->getRaster()->getPixelSize();
|
|
|
|
|
int totalBytes = m_lx * m_ly * m_bpp;
|
|
|
|
|
image->getRaster()->yMirror();
|
|
|
|
|
|
|
|
|
|
// lock raster to get data
|
|
|
|
|
image->getRaster()->lock();
|
|
|
|
|
void *buffin = image->getRaster()->getRawData();
|
|
|
|
|
assert(buffin);
|
|
|
|
|
void *buffer = malloc(totalBytes);
|
|
|
|
|
memcpy(buffer, buffin, totalBytes);
|
|
|
|
|
|
|
|
|
|
image->getRaster()->unlock();
|
|
|
|
|
|
|
|
|
|
// create QImage save format
|
|
|
|
|
QByteArray ba = m_intermediateFormat.toUpper().toLatin1();
|
|
|
|
|
const char *format = ba.data();
|
|
|
|
|
|
|
|
|
|
QImage *qi = new QImage((uint8_t *)buffer, m_lx, m_ly, QImage::Format_ARGB32);
|
|
|
|
|
qi->save(tempPath, format, -1);
|
|
|
|
|
free(buffer);
|
|
|
|
|
m_cleanUpList.push_back(tempPath);
|
2016-10-07 23:02:02 +13:00
|
|
|
|
|
2016-07-29 17:35:10 +12:00
|
|
|
|
delete qi;
|
|
|
|
|
delete image;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::runFfmpeg(QStringList preIArgs, QStringList postIArgs,
|
|
|
|
|
bool includesInPath, bool includesOutPath,
|
|
|
|
|
bool overWriteFiles) {
|
2016-11-21 23:37:55 +13:00
|
|
|
|
QString tempName = "//" + QString::fromStdString(m_path.getName()) +
|
|
|
|
|
"tempOut%d." + m_intermediateFormat;
|
|
|
|
|
tempName = getFfmpegCache().getQString() + tempName;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
|
|
|
|
|
QStringList args;
|
|
|
|
|
args = args + preIArgs;
|
|
|
|
|
if (!includesInPath) { // NOTE: if including the in path, it needs to be in
|
|
|
|
|
// the preIArgs argument.
|
|
|
|
|
args << "-i";
|
|
|
|
|
args << tempName;
|
|
|
|
|
}
|
|
|
|
|
if (m_hasSoundTrack) args = args + m_audioArgs;
|
2020-05-23 16:20:51 +12:00
|
|
|
|
args = args + postIArgs;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
if (overWriteFiles && !includesOutPath) { // if includesOutPath is true, you
|
|
|
|
|
// need to include the overwrite in
|
|
|
|
|
// your postIArgs.
|
|
|
|
|
args << "-y";
|
|
|
|
|
}
|
|
|
|
|
if (!includesOutPath) {
|
|
|
|
|
args << m_path.getQString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write the file
|
|
|
|
|
QProcess ffmpeg;
|
|
|
|
|
ffmpeg.start(m_ffmpegPath + "/ffmpeg", args);
|
2017-05-31 21:29:20 +12:00
|
|
|
|
if (ffmpeg.waitForFinished(m_ffmpegTimeout)) {
|
|
|
|
|
QString results = ffmpeg.readAllStandardError();
|
|
|
|
|
results += ffmpeg.readAllStandardOutput();
|
|
|
|
|
int exitCode = ffmpeg.exitCode();
|
|
|
|
|
ffmpeg.close();
|
|
|
|
|
std::string strResults = results.toStdString();
|
|
|
|
|
} else {
|
|
|
|
|
DVGui::warning(
|
|
|
|
|
QObject::tr("FFmpeg timed out.\n"
|
|
|
|
|
"Please check the file for errors.\n"
|
|
|
|
|
"If the file doesn't play or is incomplete, \n"
|
|
|
|
|
"Please try raising the FFmpeg timeout in Preferences."));
|
|
|
|
|
}
|
2016-07-29 17:35:10 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Ffmpeg::runFfprobe(QStringList args) {
|
|
|
|
|
QProcess ffmpeg;
|
|
|
|
|
ffmpeg.start(m_ffmpegPath + "/ffprobe", args);
|
|
|
|
|
ffmpeg.waitForFinished(m_ffmpegTimeout);
|
|
|
|
|
QString results = ffmpeg.readAllStandardError();
|
|
|
|
|
results += ffmpeg.readAllStandardOutput();
|
|
|
|
|
int exitCode = ffmpeg.exitCode();
|
|
|
|
|
ffmpeg.close();
|
2018-05-21 17:09:43 +12:00
|
|
|
|
// If the url cannot be opened or recognized as a multimedia file, ffprobe
|
|
|
|
|
// returns a positive exit code.
|
|
|
|
|
if (exitCode > 0) throw TImageException(m_path, "error reading info.");
|
2016-07-29 17:35:10 +12:00
|
|
|
|
std::string strResults = results.toStdString();
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::saveSoundTrack(TSoundTrack *st) {
|
|
|
|
|
m_sampleRate = st->getSampleRate();
|
|
|
|
|
m_channelCount = st->getChannelCount();
|
|
|
|
|
m_bitsPerSample = st->getBitPerSample();
|
|
|
|
|
// LONG count = st->getSampleCount();
|
|
|
|
|
int bufSize = st->getSampleCount() * st->getSampleSize();
|
|
|
|
|
const UCHAR *buffer = st->getRawData();
|
|
|
|
|
|
2016-11-21 23:37:55 +13:00
|
|
|
|
m_audioPath = getFfmpegCache().getQString() + "//" +
|
|
|
|
|
QString::fromStdString(m_path.getName()) + "tempOut.raw";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
m_audioFormat = "s" + QString::number(m_bitsPerSample);
|
|
|
|
|
if (m_bitsPerSample > 8) m_audioFormat = m_audioFormat + "le";
|
2020-05-23 16:20:51 +12:00
|
|
|
|
std::string strPath = m_audioPath.toStdString();
|
2016-07-29 17:35:10 +12:00
|
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
data.insert(0, (char *)buffer, bufSize);
|
|
|
|
|
|
|
|
|
|
QFile file(m_audioPath);
|
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
|
file.write(data);
|
|
|
|
|
file.close();
|
|
|
|
|
m_hasSoundTrack = true;
|
|
|
|
|
|
|
|
|
|
m_audioArgs << "-f";
|
|
|
|
|
m_audioArgs << m_audioFormat;
|
|
|
|
|
m_audioArgs << "-ar";
|
|
|
|
|
m_audioArgs << QString::number(m_sampleRate);
|
|
|
|
|
m_audioArgs << "-ac";
|
|
|
|
|
m_audioArgs << QString::number(m_channelCount);
|
|
|
|
|
m_audioArgs << "-i";
|
|
|
|
|
m_audioArgs << m_audioPath;
|
|
|
|
|
|
|
|
|
|
// add file to framesWritten for cleanup
|
|
|
|
|
m_cleanUpList.push_back(m_audioPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Ffmpeg::checkFilesExist() {
|
|
|
|
|
QString ffmpegCachePath = getFfmpegCache().getQString();
|
2016-11-24 15:11:57 +13:00
|
|
|
|
QString tempPath = ffmpegCachePath + "//" + cleanPathSymbols() + "In0001." +
|
|
|
|
|
m_intermediateFormat;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
if (TSystem::doesExistFileOrLevel(TFilePath(tempPath))) {
|
|
|
|
|
return true;
|
|
|
|
|
} else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffmpegFileInfo Ffmpeg::getInfo() {
|
|
|
|
|
QString ffmpegCachePath = getFfmpegCache().getQString();
|
2016-11-24 15:11:57 +13:00
|
|
|
|
QString tempPath = ffmpegCachePath + "//" + cleanPathSymbols() + ".txt";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
if (QFile::exists(tempPath)) {
|
|
|
|
|
QFile infoText(tempPath);
|
|
|
|
|
infoText.open(QIODevice::ReadOnly);
|
|
|
|
|
QByteArray ba = infoText.readAll();
|
|
|
|
|
infoText.close();
|
|
|
|
|
QString text = QString::fromStdString(ba.toStdString());
|
|
|
|
|
m_lx = text.split(" ")[0].toInt();
|
|
|
|
|
m_ly = text.split(" ")[1].toInt();
|
|
|
|
|
m_frameRate = text.split(" ")[2].toDouble();
|
|
|
|
|
m_frameCount = text.split(" ")[3].toInt();
|
|
|
|
|
} else {
|
|
|
|
|
QFile infoText(tempPath);
|
|
|
|
|
getSize();
|
|
|
|
|
getFrameCount();
|
2019-11-14 18:07:58 +13:00
|
|
|
|
getFrameRate();
|
2016-07-29 17:35:10 +12:00
|
|
|
|
infoText.open(QIODevice::WriteOnly);
|
|
|
|
|
std::string infoToWrite =
|
|
|
|
|
std::to_string(m_lx) + " " + std::to_string(m_ly) + " " +
|
|
|
|
|
std::to_string(m_frameRate) + " " + std::to_string(m_frameCount);
|
|
|
|
|
int infoLength = infoToWrite.length();
|
|
|
|
|
infoText.write(infoToWrite.c_str(), infoLength);
|
|
|
|
|
infoText.close();
|
|
|
|
|
}
|
|
|
|
|
ffmpegFileInfo info;
|
|
|
|
|
info.m_lx = m_lx;
|
|
|
|
|
info.m_ly = m_ly;
|
|
|
|
|
info.m_frameRate = m_frameRate;
|
|
|
|
|
info.m_frameCount = m_frameCount;
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
TRasterImageP Ffmpeg::getImage(int frameIndex) {
|
|
|
|
|
QString ffmpegCachePath = getFfmpegCache().getQString();
|
2016-11-24 15:11:57 +13:00
|
|
|
|
QString tempPath = ffmpegCachePath + "//" + cleanPathSymbols();
|
|
|
|
|
std::string tmpPath = tempPath.toStdString();
|
2016-07-29 17:35:10 +12:00
|
|
|
|
// QString tempPath= m_path.getQString();
|
|
|
|
|
QString number = QString("%1").arg(frameIndex, 4, 10, QChar('0'));
|
|
|
|
|
QString tempName = "In" + number + ".png";
|
|
|
|
|
tempName = tempPath + tempName;
|
|
|
|
|
|
|
|
|
|
// for debugging
|
|
|
|
|
std::string strPath = tempName.toStdString();
|
|
|
|
|
if (TSystem::doesExistFileOrLevel(TFilePath(tempName))) {
|
|
|
|
|
QImage *temp = new QImage(tempName, "PNG");
|
|
|
|
|
if (temp) {
|
|
|
|
|
QImage tempToo = temp->convertToFormat(QImage::Format_ARGB32);
|
|
|
|
|
delete temp;
|
|
|
|
|
const UCHAR *bits = tempToo.bits();
|
|
|
|
|
|
|
|
|
|
TRasterPT<TPixelRGBM32> ret;
|
|
|
|
|
ret.create(m_lx, m_ly);
|
|
|
|
|
ret->lock();
|
|
|
|
|
memcpy(ret->getRawData(), bits, m_lx * m_ly * 4);
|
|
|
|
|
ret->unlock();
|
|
|
|
|
ret->yMirror();
|
|
|
|
|
return TRasterImageP(ret);
|
|
|
|
|
}
|
2020-04-12 17:19:20 +12:00
|
|
|
|
}
|
|
|
|
|
return TRasterImageP();
|
2016-07-29 17:35:10 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double Ffmpeg::getFrameRate() {
|
2019-11-14 18:07:58 +13:00
|
|
|
|
QStringList fpsArgs;
|
2019-11-15 06:55:06 +13:00
|
|
|
|
int fpsNum = 0, fpsDen = 0;
|
2019-11-14 18:07:58 +13:00
|
|
|
|
fpsArgs << "-v";
|
|
|
|
|
fpsArgs << "error";
|
|
|
|
|
fpsArgs << "-select_streams";
|
|
|
|
|
fpsArgs << "v:0";
|
|
|
|
|
fpsArgs << "-show_entries";
|
|
|
|
|
fpsArgs << "stream=r_frame_rate";
|
|
|
|
|
fpsArgs << "-of";
|
|
|
|
|
fpsArgs << "default=noprint_wrappers=1:nokey=1";
|
|
|
|
|
fpsArgs << m_path.getQString();
|
|
|
|
|
QString fpsResults = runFfprobe(fpsArgs);
|
|
|
|
|
|
2019-11-15 06:55:06 +13:00
|
|
|
|
QStringList fpsResultsList = fpsResults.split("/");
|
|
|
|
|
if (fpsResultsList.size() > 1) {
|
|
|
|
|
fpsNum = fpsResultsList[0].toInt();
|
|
|
|
|
fpsDen = fpsResultsList[1].toInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if for some reason we don't have enough info to calculate it. Use the
|
|
|
|
|
// avg_frame_rate
|
|
|
|
|
if (!fpsDen) {
|
|
|
|
|
fpsArgs.clear();
|
|
|
|
|
fpsArgs << "-v";
|
|
|
|
|
fpsArgs << "error";
|
|
|
|
|
fpsArgs << "-select_streams";
|
|
|
|
|
fpsArgs << "v:0";
|
|
|
|
|
fpsArgs << "-show_entries";
|
|
|
|
|
fpsArgs << "stream=avg_frame_rate";
|
|
|
|
|
fpsArgs << "-of";
|
|
|
|
|
fpsArgs << "default=noprint_wrappers=1:nokey=1";
|
|
|
|
|
fpsArgs << m_path.getQString();
|
|
|
|
|
QString fpsResults = runFfprobe(fpsArgs);
|
|
|
|
|
|
|
|
|
|
fpsResultsList = fpsResults.split("/");
|
|
|
|
|
if (fpsResultsList.size() > 1) {
|
|
|
|
|
fpsNum = fpsResultsList[0].toInt();
|
|
|
|
|
fpsDen = fpsResultsList[1].toInt();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 18:07:58 +13:00
|
|
|
|
if (fpsDen > 0) {
|
|
|
|
|
m_frameRate = (double)fpsNum / (double)fpsDen;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
}
|
|
|
|
|
return m_frameRate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TDimension Ffmpeg::getSize() {
|
|
|
|
|
QStringList sizeArgs;
|
|
|
|
|
sizeArgs << "-v";
|
|
|
|
|
sizeArgs << "error";
|
|
|
|
|
sizeArgs << "-of";
|
|
|
|
|
sizeArgs << "flat=s=_";
|
|
|
|
|
sizeArgs << "-select_streams";
|
|
|
|
|
sizeArgs << "v:0";
|
|
|
|
|
sizeArgs << "-show_entries";
|
|
|
|
|
sizeArgs << "stream=height,width";
|
|
|
|
|
sizeArgs << m_path.getQString();
|
|
|
|
|
|
|
|
|
|
QString sizeResults = runFfprobe(sizeArgs);
|
|
|
|
|
QStringList split = sizeResults.split("\n");
|
|
|
|
|
m_lx = split[0].split("=")[1].toInt();
|
|
|
|
|
m_ly = split[1].split("=")[1].toInt();
|
|
|
|
|
return TDimension(m_lx, m_ly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Ffmpeg::getFrameCount() {
|
2019-11-15 06:55:06 +13:00
|
|
|
|
// nb_read_frames from files may not be accurate. Let's calculate it based on
|
|
|
|
|
// r_frame_rate * duration
|
2016-07-29 17:35:10 +12:00
|
|
|
|
QStringList frameCountArgs;
|
|
|
|
|
frameCountArgs << "-v";
|
|
|
|
|
frameCountArgs << "error";
|
|
|
|
|
frameCountArgs << "-count_frames";
|
|
|
|
|
frameCountArgs << "-select_streams";
|
|
|
|
|
frameCountArgs << "v:0";
|
|
|
|
|
frameCountArgs << "-show_entries";
|
2019-11-14 18:07:58 +13:00
|
|
|
|
frameCountArgs << "stream=duration";
|
2016-07-29 17:35:10 +12:00
|
|
|
|
frameCountArgs << "-of";
|
|
|
|
|
frameCountArgs << "default=nokey=1:noprint_wrappers=1";
|
|
|
|
|
frameCountArgs << m_path.getQString();
|
|
|
|
|
|
|
|
|
|
QString frameResults = runFfprobe(frameCountArgs);
|
2019-11-14 18:07:58 +13:00
|
|
|
|
m_frameCount = frameResults.toDouble() * getFrameRate();
|
2019-11-15 06:55:06 +13:00
|
|
|
|
|
|
|
|
|
// if for some reason we don't have enough info to calculate it. Use the
|
|
|
|
|
// nb_read_frames
|
|
|
|
|
if (!m_frameCount) {
|
|
|
|
|
frameCountArgs.clear();
|
|
|
|
|
frameCountArgs << "-v";
|
|
|
|
|
frameCountArgs << "error";
|
|
|
|
|
frameCountArgs << "-count_frames";
|
|
|
|
|
frameCountArgs << "-select_streams";
|
|
|
|
|
frameCountArgs << "v:0";
|
|
|
|
|
frameCountArgs << "-show_entries";
|
|
|
|
|
frameCountArgs << "stream=nb_read_frames";
|
|
|
|
|
frameCountArgs << "-of";
|
|
|
|
|
frameCountArgs << "default=nokey=1:noprint_wrappers=1";
|
|
|
|
|
frameCountArgs << m_path.getQString();
|
|
|
|
|
|
|
|
|
|
frameResults = runFfprobe(frameCountArgs);
|
|
|
|
|
m_frameCount = frameResults.toInt();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 17:35:10 +12:00
|
|
|
|
return m_frameCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::getFramesFromMovie(int frame) {
|
|
|
|
|
QString ffmpegCachePath = getFfmpegCache().getQString();
|
2016-11-24 15:11:57 +13:00
|
|
|
|
QString tempPath = ffmpegCachePath + "//" + cleanPathSymbols();
|
|
|
|
|
std::string tmpPath = tempPath.toStdString();
|
|
|
|
|
QString tempName = "In%04d." + m_intermediateFormat;
|
|
|
|
|
tempName = tempPath + tempName;
|
2016-07-29 17:35:10 +12:00
|
|
|
|
QString tempStart;
|
|
|
|
|
if (frame == -1) {
|
|
|
|
|
tempStart = "In0001." + m_intermediateFormat;
|
|
|
|
|
tempStart = tempPath + tempStart;
|
|
|
|
|
} else {
|
|
|
|
|
QString number = QString("%1").arg(frame, 4, 10, QChar('0'));
|
|
|
|
|
tempStart = tempPath + "In" + number + "." + m_intermediateFormat;
|
|
|
|
|
}
|
|
|
|
|
QString tempBase = tempPath + "In";
|
|
|
|
|
QString addToDelete;
|
|
|
|
|
if (!TSystem::doesExistFileOrLevel(TFilePath(tempStart))) {
|
|
|
|
|
// for debugging
|
|
|
|
|
std::string strPath = tempName.toStdString();
|
|
|
|
|
|
|
|
|
|
QStringList preIFrameArgs;
|
|
|
|
|
QStringList postIFrameArgs;
|
|
|
|
|
// frameArgs << "-accurate_seek";
|
|
|
|
|
// frameArgs << "-ss";
|
|
|
|
|
// frameArgs << "0" + QString::number(frameIndex / m_info->m_frameRate);
|
2021-04-23 09:15:54 +12:00
|
|
|
|
if (m_path.getType() == "webm") {
|
|
|
|
|
// To load in webm transparency
|
|
|
|
|
preIFrameArgs << "-vcodec";
|
|
|
|
|
preIFrameArgs << "libvpx";
|
|
|
|
|
}
|
2016-07-29 17:35:10 +12:00
|
|
|
|
preIFrameArgs << "-i";
|
|
|
|
|
preIFrameArgs << m_path.getQString();
|
|
|
|
|
postIFrameArgs << "-y";
|
|
|
|
|
postIFrameArgs << "-f";
|
|
|
|
|
postIFrameArgs << "image2";
|
|
|
|
|
|
|
|
|
|
postIFrameArgs << tempName;
|
|
|
|
|
|
|
|
|
|
runFfmpeg(preIFrameArgs, postIFrameArgs, true, true, true);
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= m_frameCount; i++) {
|
|
|
|
|
QString number = QString("%1").arg(i, 4, 10, QChar('0'));
|
|
|
|
|
addToDelete = tempBase + number + "." + m_intermediateFormat;
|
|
|
|
|
std::string delPath = addToDelete.toStdString();
|
|
|
|
|
// addToCleanUp(addToDelete);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:11:57 +13:00
|
|
|
|
QString Ffmpeg::cleanPathSymbols() {
|
|
|
|
|
return m_path.getQString().remove(QRegExp(
|
2018-05-21 17:09:43 +12:00
|
|
|
|
QString::fromUtf8("[-`~!@#$%^&*()_+=|:;<>«»,.?/{}\'\"\\[\\]\\\\]")));
|
2016-11-24 15:11:57 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 19:52:57 +13:00
|
|
|
|
int Ffmpeg::getGifFrameCount() {
|
|
|
|
|
int frame = 1;
|
|
|
|
|
QString ffmpegCachePath = getFfmpegCache().getQString();
|
2016-11-24 15:11:57 +13:00
|
|
|
|
QString tempPath = ffmpegCachePath + "//" + cleanPathSymbols();
|
|
|
|
|
std::string tmpPath = tempPath.toStdString();
|
|
|
|
|
QString tempName = "In%04d." + m_intermediateFormat;
|
|
|
|
|
tempName = tempPath + tempName;
|
2016-09-28 19:52:57 +13:00
|
|
|
|
QString tempStart;
|
|
|
|
|
tempStart = "In0001." + m_intermediateFormat;
|
|
|
|
|
tempStart = tempPath + tempStart;
|
|
|
|
|
while (TSystem::doesExistFileOrLevel(TFilePath(tempStart))) {
|
|
|
|
|
frame++;
|
|
|
|
|
QString number = QString("%1").arg(frame, 4, 10, QChar('0'));
|
|
|
|
|
tempStart = tempPath + "In" + number + "." + m_intermediateFormat;
|
|
|
|
|
}
|
|
|
|
|
return frame - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 17:35:10 +12:00
|
|
|
|
void Ffmpeg::addToCleanUp(QString path) {
|
|
|
|
|
if (TSystem::doesExistFileOrLevel(TFilePath(path))) {
|
|
|
|
|
m_cleanUpList.push_back(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::cleanUpFiles() {
|
|
|
|
|
for (QString path : m_cleanUpList) {
|
|
|
|
|
if (TSystem::doesExistFileOrLevel(TFilePath(path))) {
|
|
|
|
|
TSystem::deleteFile(TFilePath(path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ffmpeg::disablePrecompute() {
|
|
|
|
|
Preferences::instance()->setPrecompute(false);
|
2020-07-23 17:23:55 +12:00
|
|
|
|
}
|