2012-05-04 04:01:10 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2013-02-08 23:50:51 +01:00
|
|
|
// Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
|
2012-05-04 04:01:10 +02:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Thread_FileAnalyzer_Task.h"
|
|
|
|
|
|
|
|
#include "Global.h"
|
|
|
|
#include "LockedFile.h"
|
|
|
|
#include "Model_AudioFile.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QDate>
|
|
|
|
#include <QTime>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QReadLocker>
|
|
|
|
#include <QWriteLocker>
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
2012-05-14 00:50:16 +02:00
|
|
|
#include <assert.h>
|
2012-05-04 04:01:10 +02:00
|
|
|
|
|
|
|
#define IS_KEY(KEY) (key.compare(KEY, Qt::CaseInsensitive) == 0)
|
|
|
|
#define IS_SEC(SEC) (key.startsWith((SEC "_"), Qt::CaseInsensitive))
|
|
|
|
#define FIRST_TOK(STR) (STR.split(" ", QString::SkipEmptyParts).first())
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Constructor
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2013-10-06 19:26:08 +02:00
|
|
|
AnalyzeTask::AnalyzeTask(const int taskId, const QString &inputFile, const QString &templateFile, volatile bool *abortFlag)
|
2012-05-04 04:01:10 +02:00
|
|
|
:
|
2013-10-06 19:26:08 +02:00
|
|
|
m_taskId(taskId),
|
2012-05-04 04:01:10 +02:00
|
|
|
m_inputFile(inputFile),
|
|
|
|
m_templateFile(templateFile),
|
|
|
|
m_mediaInfoBin(lamexp_lookup_tool("mediainfo.exe")),
|
|
|
|
m_avs2wavBin(lamexp_lookup_tool("avs2wav.exe")),
|
|
|
|
m_abortFlag(abortFlag)
|
|
|
|
{
|
|
|
|
if(m_mediaInfoBin.isEmpty() || m_avs2wavBin.isEmpty())
|
|
|
|
{
|
|
|
|
qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalyzeTask::~AnalyzeTask(void)
|
|
|
|
{
|
2013-10-06 19:26:08 +02:00
|
|
|
emit taskCompleted(m_taskId);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Thread Main
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2012-05-05 21:56:14 +02:00
|
|
|
void AnalyzeTask::run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
run_ex();
|
|
|
|
}
|
2013-10-18 21:37:40 +02:00
|
|
|
catch(const std::exception &error)
|
|
|
|
{
|
|
|
|
fflush(stdout); fflush(stderr);
|
|
|
|
fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
|
|
|
|
lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
|
|
|
|
}
|
2012-05-05 21:56:14 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
2013-10-18 21:37:40 +02:00
|
|
|
fflush(stdout); fflush(stderr);
|
|
|
|
fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
|
|
|
|
lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
|
2012-05-05 21:56:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyzeTask::run_ex(void)
|
2012-05-04 04:01:10 +02:00
|
|
|
{
|
|
|
|
int fileType = fileTypeNormal;
|
|
|
|
QString currentFile = QDir::fromNativeSeparators(m_inputFile);
|
2013-10-21 15:00:55 +02:00
|
|
|
qDebug("Analyzing: %s", QUTF8(currentFile));
|
2012-05-06 04:57:00 +02:00
|
|
|
|
2012-05-04 04:01:10 +02:00
|
|
|
AudioFileModel file = analyzeFile(currentFile, &fileType);
|
2012-05-06 04:57:00 +02:00
|
|
|
|
2012-05-04 04:01:10 +02:00
|
|
|
if(*m_abortFlag)
|
|
|
|
{
|
|
|
|
qWarning("Operation cancelled by user!");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-06 19:26:08 +02:00
|
|
|
|
|
|
|
switch(fileType)
|
2012-05-04 04:01:10 +02:00
|
|
|
{
|
2013-10-06 19:26:08 +02:00
|
|
|
case fileTypeDenied:
|
2012-05-04 04:01:10 +02:00
|
|
|
qWarning("Cannot access file for reading, skipping!");
|
2013-10-06 19:26:08 +02:00
|
|
|
break;
|
|
|
|
case fileTypeCDDA:
|
2012-05-04 04:01:10 +02:00
|
|
|
qWarning("Dummy CDDA file detected, skipping!");
|
2013-10-06 19:26:08 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-12 22:55:41 +02:00
|
|
|
if(file.metaInfo().title().isEmpty() || file.techInfo().containerType().isEmpty() || file.techInfo().audioType().isEmpty())
|
2012-05-04 04:01:10 +02:00
|
|
|
{
|
2013-10-06 19:26:08 +02:00
|
|
|
fileType = fileTypeUnknown;
|
|
|
|
if(!QFileInfo(currentFile).suffix().compare("cue", Qt::CaseInsensitive))
|
2012-05-04 04:01:10 +02:00
|
|
|
{
|
2013-10-06 19:26:08 +02:00
|
|
|
qWarning("Cue Sheet file detected, skipping!");
|
|
|
|
fileType = fileTypeCueSheet;
|
|
|
|
}
|
|
|
|
else if(!QFileInfo(currentFile).suffix().compare("avs", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
qDebug("Found a potential Avisynth script, investigating...");
|
|
|
|
if(analyzeAvisynthFile(currentFile, file))
|
|
|
|
{
|
|
|
|
fileType = fileTypeNormal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-21 15:00:55 +02:00
|
|
|
qDebug("Rejected Avisynth file: %s", QUTF8(file.filePath()));
|
2013-10-06 19:26:08 +02:00
|
|
|
}
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-21 15:00:55 +02:00
|
|
|
qDebug("Rejected file of unknown type: %s", QUTF8(file.filePath()));
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-06 19:26:08 +02:00
|
|
|
break;
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
|
2012-05-12 02:51:24 +02:00
|
|
|
//Emit the file now!
|
2013-10-06 19:26:08 +02:00
|
|
|
emit fileAnalyzed(m_taskId, fileType, file);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Privtae Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
const AudioFileModel AnalyzeTask::analyzeFile(const QString &filePath, int *type)
|
|
|
|
{
|
|
|
|
*type = fileTypeNormal;
|
|
|
|
AudioFileModel audioFile(filePath);
|
|
|
|
|
|
|
|
QFile readTest(filePath);
|
|
|
|
if(!readTest.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
*type = fileTypeDenied;
|
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
if(checkFile_CDDA(readTest))
|
|
|
|
{
|
|
|
|
*type = fileTypeCDDA;
|
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
readTest.close();
|
|
|
|
|
|
|
|
bool skipNext = false;
|
|
|
|
unsigned int id_val[2] = {UINT_MAX, UINT_MAX};
|
|
|
|
cover_t coverType = coverNone;
|
|
|
|
QByteArray coverData;
|
|
|
|
|
|
|
|
QStringList params;
|
|
|
|
params << QString("--Inform=file://%1").arg(QDir::toNativeSeparators(m_templateFile));
|
|
|
|
params << QDir::toNativeSeparators(filePath);
|
|
|
|
|
|
|
|
QProcess process;
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
|
|
|
process.start(m_mediaInfoBin, params);
|
|
|
|
|
|
|
|
if(!process.waitForStarted())
|
|
|
|
{
|
|
|
|
qWarning("MediaInfo process failed to create!");
|
|
|
|
qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
if(*m_abortFlag)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
qWarning("Process was aborted on user request!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!process.waitForReadyRead())
|
|
|
|
{
|
|
|
|
if(process.state() == QProcess::Running)
|
|
|
|
{
|
|
|
|
qWarning("MediaInfo time out. Killing process and skipping file!");
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
|
|
|
while(process.canReadLine())
|
|
|
|
{
|
|
|
|
QString line = QString::fromUtf8(process.readLine().constData()).simplified();
|
|
|
|
if(!line.isEmpty())
|
|
|
|
{
|
2013-10-21 15:00:55 +02:00
|
|
|
//qDebug("Line:%s", QUTF8(line));
|
2012-05-04 04:01:10 +02:00
|
|
|
|
|
|
|
int index = line.indexOf('=');
|
|
|
|
if(index > 0)
|
|
|
|
{
|
|
|
|
QString key = line.left(index).trimmed();
|
|
|
|
QString val = line.mid(index+1).trimmed();
|
|
|
|
if(!key.isEmpty())
|
|
|
|
{
|
|
|
|
updateInfo(audioFile, &skipNext, id_val, &coverType, &coverData, key, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-12 22:55:41 +02:00
|
|
|
if(audioFile.metaInfo().title().isEmpty())
|
2012-05-04 04:01:10 +02:00
|
|
|
{
|
|
|
|
QString baseName = QFileInfo(filePath).fileName();
|
|
|
|
int index = baseName.lastIndexOf(".");
|
|
|
|
|
|
|
|
if(index >= 0)
|
|
|
|
{
|
|
|
|
baseName = baseName.left(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
baseName = baseName.replace("_", " ").simplified();
|
|
|
|
index = baseName.lastIndexOf(" - ");
|
|
|
|
|
|
|
|
if(index >= 0)
|
|
|
|
{
|
|
|
|
baseName = baseName.mid(index + 3).trimmed();
|
|
|
|
}
|
|
|
|
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.metaInfo().setTitle(baseName);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
process.waitForFinished();
|
|
|
|
if(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((coverType != coverNone) && (!coverData.isEmpty()))
|
|
|
|
{
|
|
|
|
retrieveCover(audioFile, coverType, coverData);
|
|
|
|
}
|
|
|
|
|
2013-10-12 22:55:41 +02:00
|
|
|
if((audioFile.techInfo().audioType().compare("PCM", Qt::CaseInsensitive) == 0) && (audioFile.techInfo().audioProfile().compare("Float", Qt::CaseInsensitive) == 0))
|
2012-08-22 23:52:55 +02:00
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
if(audioFile.techInfo().audioBitdepth() == 32) audioFile.techInfo().setAudioBitdepth(AudioFileModel::BITDEPTH_IEEE_FLOAT32);
|
2012-08-22 23:52:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-04 04:01:10 +02:00
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyzeTask::updateInfo(AudioFileModel &audioFile, bool *skipNext, unsigned int *id_val, cover_t *coverType, QByteArray *coverData, const QString &key, const QString &value)
|
|
|
|
{
|
2013-10-21 15:00:55 +02:00
|
|
|
//qWarning("'%s' -> '%s'", QUTF8(key), QUTF8(value));
|
2012-05-04 04:01:10 +02:00
|
|
|
|
|
|
|
/*New Stream*/
|
|
|
|
if(IS_KEY("Gen_ID") || IS_KEY("Aud_ID"))
|
|
|
|
{
|
|
|
|
if(value.isEmpty())
|
|
|
|
{
|
|
|
|
*skipNext = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//We ignore all ID's, except for the lowest one!
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int id = value.toUInt(&ok);
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
if(IS_KEY("Gen_ID")) { id_val[0] = qMin(id_val[0], id); *skipNext = (id > id_val[0]); }
|
|
|
|
if(IS_KEY("Aud_ID")) { id_val[1] = qMin(id_val[1], id); *skipNext = (id > id_val[1]); }
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*skipNext = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(*skipNext)
|
|
|
|
{
|
|
|
|
qWarning("Skipping info for non-primary stream!");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Skip or empty?*/
|
|
|
|
if((*skipNext) || value.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Playlist file?*/
|
|
|
|
if(IS_KEY("Aud_Source"))
|
|
|
|
{
|
|
|
|
*skipNext = true;
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.techInfo().setContainerType(QString());
|
|
|
|
audioFile.techInfo().setAudioType(QString());
|
2012-05-04 04:01:10 +02:00
|
|
|
qWarning("Skipping info for playlist file!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*General Section*/
|
|
|
|
if(IS_SEC("Gen"))
|
|
|
|
{
|
|
|
|
if(IS_KEY("Gen_Format"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.techInfo().setContainerType(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Format_Profile"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.techInfo().setContainerProfile(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Title") || IS_KEY("Gen_Track"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.metaInfo().setTitle(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Duration"))
|
|
|
|
{
|
|
|
|
unsigned int tmp = parseDuration(value);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(tmp > 0) audioFile.techInfo().setDuration(tmp);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Artist") || IS_KEY("Gen_Performer"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.metaInfo().setArtist(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Album"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.metaInfo().setAlbum(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Genre"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.metaInfo().setGenre(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Released_Date") || IS_KEY("Gen_Recorded_Date"))
|
|
|
|
{
|
|
|
|
unsigned int tmp = parseYear(value);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(tmp > 0) audioFile.metaInfo().setYear(tmp);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Comment"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.metaInfo().setComment(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Track/Position"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) audioFile.metaInfo().setPosition(tmp);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Cover") || IS_KEY("Gen_Cover_Type"))
|
|
|
|
{
|
|
|
|
if(*coverType == coverNone)
|
|
|
|
{
|
|
|
|
*coverType = coverJpeg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Cover_Mime"))
|
|
|
|
{
|
|
|
|
QString temp = FIRST_TOK(value);
|
|
|
|
if(!temp.compare("image/jpeg", Qt::CaseInsensitive)) *coverType = coverJpeg;
|
|
|
|
else if(!temp.compare("image/png", Qt::CaseInsensitive)) *coverType = coverPng;
|
|
|
|
else if(!temp.compare("image/gif", Qt::CaseInsensitive)) *coverType = coverGif;
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Cover_Data"))
|
|
|
|
{
|
|
|
|
if(!coverData->isEmpty()) coverData->clear();
|
|
|
|
coverData->append(QByteArray::fromBase64(FIRST_TOK(value).toLatin1()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-21 15:00:55 +02:00
|
|
|
qWarning("Unknown key '%s' with value '%s' found!", QUTF8(key), QUTF8(value));
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Audio Section*/
|
|
|
|
if(IS_SEC("Aud"))
|
|
|
|
{
|
|
|
|
|
|
|
|
if(IS_KEY("Aud_Format"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.techInfo().setAudioType(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Format_Profile"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.techInfo().setAudioProfile(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Format_Version"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.techInfo().setAudioVersion(value);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Channel(s)"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) audioFile.techInfo().setAudioChannels(tmp);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_SamplingRate"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) audioFile.techInfo().setAudioSamplerate(tmp);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_BitDepth"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) audioFile.techInfo().setAudioBitdepth(tmp);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Duration"))
|
|
|
|
{
|
|
|
|
unsigned int tmp = parseDuration(value);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(tmp > 0) audioFile.techInfo().setDuration(tmp);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_BitRate"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) audioFile.techInfo().setAudioBitrate(tmp/1000);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_BitRate_Mode"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
if(!value.compare("CBR", Qt::CaseInsensitive)) audioFile.techInfo().setAudioBitrateMode(AudioFileModel::BitrateModeConstant);
|
|
|
|
if(!value.compare("VBR", Qt::CaseInsensitive)) audioFile.techInfo().setAudioBitrateMode(AudioFileModel::BitrateModeVariable);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
2012-08-01 00:17:12 +02:00
|
|
|
else if(IS_KEY("Aud_Encoded_Library"))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.techInfo().setAudioEncodeLib(value);
|
2012-08-01 00:17:12 +02:00
|
|
|
}
|
2012-05-04 04:01:10 +02:00
|
|
|
else
|
|
|
|
{
|
2013-10-21 15:00:55 +02:00
|
|
|
qWarning("Unknown key '%s' with value '%s' found!", QUTF8(key), QUTF8(value));
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Section not recognized*/
|
2013-10-21 15:00:55 +02:00
|
|
|
qWarning("Unknown section: %s", QUTF8(key));
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AnalyzeTask::checkFile_CDDA(QFile &file)
|
|
|
|
{
|
|
|
|
file.reset();
|
|
|
|
QByteArray data = file.read(128);
|
|
|
|
|
|
|
|
int i = data.indexOf("RIFF");
|
|
|
|
int j = data.indexOf("CDDA");
|
|
|
|
int k = data.indexOf("fmt ");
|
|
|
|
|
|
|
|
return ((i >= 0) && (j >= 0) && (k >= 0) && (k > j) && (j > i));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyzeTask::retrieveCover(AudioFileModel &audioFile, cover_t coverType, const QByteArray &coverData)
|
|
|
|
{
|
|
|
|
qDebug("Retrieving cover!");
|
|
|
|
QString extension;
|
|
|
|
|
|
|
|
switch(coverType)
|
|
|
|
{
|
|
|
|
case coverPng:
|
|
|
|
extension = QString::fromLatin1("png");
|
|
|
|
break;
|
|
|
|
case coverGif:
|
|
|
|
extension = QString::fromLatin1("gif");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
extension = QString::fromLatin1("jpg");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(QImage::fromData(coverData, extension.toUpper().toLatin1().constData()).isNull()))
|
|
|
|
{
|
|
|
|
QFile coverFile(QString("%1/%2.%3").arg(lamexp_temp_folder2(), lamexp_rand_str(), extension));
|
|
|
|
if(coverFile.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
coverFile.write(coverData);
|
|
|
|
coverFile.close();
|
2013-10-12 22:55:41 +02:00
|
|
|
audioFile.metaInfo().setCover(coverFile.fileName(), true);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("Image data seems to be invalid :-(");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnalyzeTask::analyzeAvisynthFile(const QString &filePath, AudioFileModel &info)
|
|
|
|
{
|
|
|
|
QProcess process;
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
|
|
|
process.start(m_avs2wavBin, QStringList() << QDir::toNativeSeparators(filePath) << "?");
|
|
|
|
|
|
|
|
if(!process.waitForStarted())
|
|
|
|
{
|
|
|
|
qWarning("AVS2WAV process failed to create!");
|
|
|
|
qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bInfoHeaderFound = false;
|
|
|
|
|
|
|
|
while(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
if(*m_abortFlag)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
qWarning("Process was aborted on user request!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!process.waitForReadyRead())
|
|
|
|
{
|
|
|
|
if(process.state() == QProcess::Running)
|
|
|
|
{
|
|
|
|
qWarning("AVS2WAV time out. Killing process and skipping file!");
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
|
|
|
while(process.canReadLine())
|
|
|
|
{
|
|
|
|
QString line = QString::fromUtf8(process.readLine().constData()).simplified();
|
|
|
|
if(!line.isEmpty())
|
|
|
|
{
|
|
|
|
int index = line.indexOf(':');
|
|
|
|
if(index > 0)
|
|
|
|
{
|
|
|
|
QString key = line.left(index).trimmed();
|
|
|
|
QString val = line.mid(index+1).trimmed();
|
|
|
|
|
|
|
|
if(bInfoHeaderFound && !key.isEmpty() && !val.isEmpty())
|
|
|
|
{
|
|
|
|
if(key.compare("TotalSeconds", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int duration = val.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) info.techInfo().setDuration(duration);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
if(key.compare("SamplesPerSec", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int samplerate = val.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) info.techInfo().setAudioSamplerate (samplerate);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
if(key.compare("Channels", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int channels = val.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) info.techInfo().setAudioChannels(channels);
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
if(key.compare("BitsPerSample", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int bitdepth = val.toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) info.techInfo().setAudioBitdepth(bitdepth);
|
|
|
|
}
|
2012-05-04 04:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(line.contains("[Audio Info]", Qt::CaseInsensitive))
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
info.techInfo().setAudioType("Avisynth");
|
|
|
|
info.techInfo().setContainerType("Avisynth");
|
2012-05-04 04:01:10 +02:00
|
|
|
bInfoHeaderFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.waitForFinished();
|
|
|
|
if(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check exit code
|
|
|
|
switch(process.exitCode())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
qDebug("Avisynth script was analyzed successfully.");
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case -5:
|
|
|
|
qWarning("It appears that Avisynth is not installed on the system!");
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qWarning("Failed to open the Avisynth script, bad AVS file?");
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AnalyzeTask::parseYear(const QString &str)
|
|
|
|
{
|
|
|
|
if(str.startsWith("UTC", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
QDate date = QDate::fromString(str.mid(3).trimmed().left(10), "yyyy-MM-dd");
|
|
|
|
if(date.isValid())
|
|
|
|
{
|
|
|
|
return date.year();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
int year = str.toInt(&ok);
|
|
|
|
if(ok && year > 0)
|
|
|
|
{
|
|
|
|
return year;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AnalyzeTask::parseDuration(const QString &str)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int value = str.toUInt(&ok);
|
|
|
|
return ok ? (value/1000) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Public Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2013-10-06 19:26:08 +02:00
|
|
|
/*NONE*/
|
2012-05-12 02:51:24 +02:00
|
|
|
|
2012-05-04 04:01:10 +02:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// EVENTS
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*NONE*/
|