2010-11-06 23:04:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2012-01-02 00:52:27 +01:00
|
|
|
// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
|
2010-11-06 23:04:47 +01: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.h"
|
|
|
|
|
|
|
|
#include "Global.h"
|
|
|
|
#include "LockedFile.h"
|
|
|
|
#include "Model_AudioFile.h"
|
2011-01-31 21:02:47 +01:00
|
|
|
#include "PlaylistImporter.h"
|
2010-11-06 23:04:47 +01:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QDate>
|
|
|
|
#include <QTime>
|
2010-11-22 21:45:00 +01:00
|
|
|
#include <QDebug>
|
2011-11-19 14:06:39 +01:00
|
|
|
#include <QImage>
|
2010-11-06 23:04:47 +01:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
#define IS_KEY(KEY) (key.compare(KEY, Qt::CaseInsensitive) == 0)
|
2012-01-14 20:28:43 +01:00
|
|
|
#define IS_SEC(SEC) (key.startsWith((SEC "_"), Qt::CaseInsensitive))
|
|
|
|
#define FIRST_TOK(STR) (STR.split(" ", QString::SkipEmptyParts).first())
|
2012-01-14 01:11:58 +01:00
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Constructor
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
FileAnalyzer::FileAnalyzer(const QStringList &inputFiles)
|
2010-12-18 15:15:17 +01:00
|
|
|
:
|
|
|
|
m_inputFiles(inputFiles),
|
2011-05-16 21:02:24 +02:00
|
|
|
m_mediaInfoBin(lamexp_lookup_tool("mediainfo.exe")),
|
2011-05-21 21:08:10 +02:00
|
|
|
m_avs2wavBin(lamexp_lookup_tool("avs2wav.exe")),
|
2012-01-14 01:11:58 +01:00
|
|
|
m_templateFile(NULL),
|
2011-05-16 21:02:24 +02:00
|
|
|
m_abortFlag(false)
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
|
|
|
m_bSuccess = false;
|
2011-05-16 21:02:24 +02:00
|
|
|
m_bAborted = false;
|
|
|
|
|
2011-04-11 02:53:29 +02:00
|
|
|
if(m_mediaInfoBin.isEmpty())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
|
|
|
qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
|
|
|
|
}
|
2010-11-11 19:37:16 +01:00
|
|
|
|
|
|
|
m_filesAccepted = 0;
|
|
|
|
m_filesRejected = 0;
|
|
|
|
m_filesDenied = 0;
|
2011-03-19 15:35:17 +01:00
|
|
|
m_filesDummyCDDA = 0;
|
2011-05-17 01:14:50 +02:00
|
|
|
m_filesCueSheet = 0;
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
FileAnalyzer::~FileAnalyzer(void)
|
|
|
|
{
|
|
|
|
if(m_templateFile)
|
|
|
|
{
|
|
|
|
QString templatePath = m_templateFile->filePath();
|
|
|
|
LAMEXP_DELETE(m_templateFile);
|
|
|
|
if(QFile::exists(templatePath)) QFile::remove(templatePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Static data
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
const char *FileAnalyzer::g_tags_gen[] =
|
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
"ID",
|
2012-01-14 01:11:58 +01:00
|
|
|
"Format",
|
|
|
|
"Format_Profile",
|
|
|
|
"Format_Version",
|
|
|
|
"Duration",
|
|
|
|
"Title", "Track",
|
|
|
|
"Track/Position",
|
|
|
|
"Artist", "Performer",
|
|
|
|
"Album",
|
|
|
|
"Genre",
|
|
|
|
"Released_Date", "Recorded_Date",
|
|
|
|
"Comment",
|
|
|
|
"Cover",
|
|
|
|
"Cover_Type",
|
|
|
|
"Cover_Mime",
|
|
|
|
"Cover_Data",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *FileAnalyzer::g_tags_aud[] =
|
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
"ID",
|
2012-01-14 01:11:58 +01:00
|
|
|
"Format",
|
|
|
|
"Format_Profile",
|
|
|
|
"Format_Version",
|
|
|
|
"Channel(s)",
|
|
|
|
"SamplingRate",
|
2012-03-08 01:34:00 +01:00
|
|
|
"BitDepth",
|
2012-01-14 01:11:58 +01:00
|
|
|
"BitRate",
|
|
|
|
"BitRate_Mode",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Thread Main
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void FileAnalyzer::run()
|
|
|
|
{
|
|
|
|
m_bSuccess = false;
|
2011-05-16 21:02:24 +02:00
|
|
|
m_bAborted = false;
|
2010-11-11 19:37:16 +01:00
|
|
|
|
|
|
|
m_filesAccepted = 0;
|
|
|
|
m_filesRejected = 0;
|
|
|
|
m_filesDenied = 0;
|
2011-03-19 15:35:17 +01:00
|
|
|
m_filesDummyCDDA = 0;
|
2011-05-17 01:14:50 +02:00
|
|
|
m_filesCueSheet = 0;
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
m_inputFiles.sort();
|
2011-09-06 00:51:00 +02:00
|
|
|
m_recentlyAdded.clear();
|
2011-05-16 21:02:24 +02:00
|
|
|
m_abortFlag = false;
|
2011-04-11 21:57:16 +02:00
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
if(!m_templateFile)
|
|
|
|
{
|
|
|
|
if(!createTemplate())
|
|
|
|
{
|
|
|
|
qWarning("Failed to create template file!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
while(!m_inputFiles.isEmpty())
|
|
|
|
{
|
2011-05-17 01:14:50 +02:00
|
|
|
int fileType = fileTypeNormal;
|
2010-11-06 23:04:47 +01:00
|
|
|
QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
|
2011-09-29 20:06:09 +02:00
|
|
|
qDebug("Analyzing: %s", currentFile.toUtf8().constData());
|
2010-11-06 23:04:47 +01:00
|
|
|
emit fileSelected(QFileInfo(currentFile).fileName());
|
2011-05-17 01:14:50 +02:00
|
|
|
AudioFileModel file = analyzeFile(currentFile, &fileType);
|
2011-04-11 21:57:16 +02:00
|
|
|
|
2011-05-16 21:02:24 +02:00
|
|
|
if(m_abortFlag)
|
|
|
|
{
|
|
|
|
MessageBeep(MB_ICONERROR);
|
|
|
|
m_bAborted = true;
|
|
|
|
qWarning("Operation cancelled by user!");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-06 00:51:00 +02:00
|
|
|
if(fileType == fileTypeSkip)
|
|
|
|
{
|
|
|
|
qWarning("File was recently added, skipping!");
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-17 01:14:50 +02:00
|
|
|
if(fileType == fileTypeDenied)
|
|
|
|
{
|
|
|
|
m_filesDenied++;
|
|
|
|
qWarning("Cannot access file for reading, skipping!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(fileType == fileTypeCDDA)
|
|
|
|
{
|
|
|
|
m_filesDummyCDDA++;
|
|
|
|
qWarning("Dummy CDDA file detected, skipping!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-11-08 00:24:54 +01:00
|
|
|
if(file.fileName().isEmpty() || file.formatContainerType().isEmpty() || file.formatAudioType().isEmpty())
|
2010-11-07 23:06:30 +01:00
|
|
|
{
|
2011-05-17 01:14:50 +02:00
|
|
|
if(PlaylistImporter::importPlaylist(m_inputFiles, currentFile))
|
|
|
|
{
|
|
|
|
qDebug("Imported playlist file.");
|
|
|
|
}
|
|
|
|
else if(!QFileInfo(currentFile).suffix().compare("cue", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
qWarning("Cue Sheet file detected, skipping!");
|
|
|
|
m_filesCueSheet++;
|
|
|
|
}
|
2011-05-19 23:07:46 +02:00
|
|
|
else if(!QFileInfo(currentFile).suffix().compare("avs", Qt::CaseInsensitive))
|
|
|
|
{
|
2011-05-21 23:51:28 +02:00
|
|
|
qDebug("Found a potential Avisynth script, investigating...");
|
2011-05-21 21:08:10 +02:00
|
|
|
if(analyzeAvisynthFile(currentFile, file))
|
|
|
|
{
|
|
|
|
m_filesAccepted++;
|
|
|
|
emit fileAnalyzed(file);
|
|
|
|
}
|
2011-05-22 20:35:17 +02:00
|
|
|
else
|
|
|
|
{
|
2011-09-29 20:06:09 +02:00
|
|
|
qDebug("Rejected Avisynth file: %s", file.filePath().toUtf8().constData());
|
2011-05-22 20:35:17 +02:00
|
|
|
m_filesRejected++;
|
|
|
|
}
|
2011-05-19 23:07:46 +02:00
|
|
|
}
|
2011-05-17 01:14:50 +02:00
|
|
|
else
|
2011-01-30 21:49:32 +01:00
|
|
|
{
|
2011-09-29 20:06:09 +02:00
|
|
|
qDebug("Rejected file of unknown type: %s", file.filePath().toUtf8().constData());
|
2011-01-30 21:49:32 +01:00
|
|
|
m_filesRejected++;
|
|
|
|
}
|
2010-11-07 23:06:30 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-17 01:14:50 +02:00
|
|
|
|
2010-11-11 19:37:16 +01:00
|
|
|
m_filesAccepted++;
|
2011-09-06 00:51:00 +02:00
|
|
|
m_recentlyAdded.append(file.filePath());
|
2010-11-06 23:04:47 +01:00
|
|
|
emit fileAnalyzed(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("All files added.\n");
|
|
|
|
m_bSuccess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2011-01-30 21:49:32 +01:00
|
|
|
// Privtae Functions
|
2010-11-06 23:04:47 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2011-05-17 01:14:50 +02:00
|
|
|
const AudioFileModel FileAnalyzer::analyzeFile(const QString &filePath, int *type)
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2011-05-17 01:14:50 +02:00
|
|
|
*type = fileTypeNormal;
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
AudioFileModel audioFile(filePath);
|
|
|
|
|
2011-09-06 00:51:00 +02:00
|
|
|
if(m_recentlyAdded.contains(filePath, Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
*type = fileTypeSkip;
|
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
|
2010-11-08 00:24:54 +01:00
|
|
|
QFile readTest(filePath);
|
|
|
|
if(!readTest.open(QIODevice::ReadOnly))
|
|
|
|
{
|
2011-05-17 01:14:50 +02:00
|
|
|
*type = fileTypeDenied;
|
2010-11-08 00:24:54 +01:00
|
|
|
return audioFile;
|
|
|
|
}
|
2011-03-19 15:35:17 +01:00
|
|
|
if(checkFile_CDDA(readTest))
|
2010-11-08 00:24:54 +01:00
|
|
|
{
|
2011-05-17 01:14:50 +02:00
|
|
|
*type = fileTypeCDDA;
|
2011-03-19 15:35:17 +01:00
|
|
|
return audioFile;
|
2010-11-08 00:24:54 +01:00
|
|
|
}
|
2011-03-19 15:35:17 +01:00
|
|
|
readTest.close();
|
2010-11-08 00:24:54 +01:00
|
|
|
|
2012-01-14 20:28:43 +01:00
|
|
|
bool skipNext = false;
|
|
|
|
unsigned int id_val[2] = {UINT_MAX, UINT_MAX};
|
2012-01-14 01:11:58 +01:00
|
|
|
cover_t coverType = coverNone;
|
|
|
|
QByteArray coverData;
|
|
|
|
|
|
|
|
QStringList params;
|
|
|
|
params << QString("--Inform=file://%1").arg(QDir::toNativeSeparators(m_templateFile->filePath()));
|
|
|
|
params << QDir::toNativeSeparators(filePath);
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
QProcess process;
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
2012-01-14 01:11:58 +01:00
|
|
|
process.start(m_mediaInfoBin, params);
|
2011-04-17 15:53:28 +02:00
|
|
|
|
2010-11-12 15:58:53 +01:00
|
|
|
if(!process.waitForStarted())
|
|
|
|
{
|
|
|
|
qWarning("MediaInfo process failed to create!");
|
2010-11-18 22:37:35 +01:00
|
|
|
qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
|
2010-11-12 15:58:53 +01:00
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
return audioFile;
|
|
|
|
}
|
2010-11-06 23:04:47 +01:00
|
|
|
|
|
|
|
while(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
2011-05-16 21:02:24 +02:00
|
|
|
if(m_abortFlag)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
qWarning("Process was aborted on user request!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-11-08 00:24:54 +01:00
|
|
|
if(!process.waitForReadyRead())
|
|
|
|
{
|
|
|
|
if(process.state() == QProcess::Running)
|
|
|
|
{
|
|
|
|
qWarning("MediaInfo time out. Killing process and skipping file!");
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-18 22:37:35 +01:00
|
|
|
QByteArray data;
|
|
|
|
|
|
|
|
while(process.canReadLine())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2010-11-18 22:37:35 +01:00
|
|
|
QString line = QString::fromUtf8(process.readLine().constData()).simplified();
|
2010-11-06 23:04:47 +01:00
|
|
|
if(!line.isEmpty())
|
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
//qDebug("Line:%s", line.toUtf8().constData());
|
|
|
|
|
|
|
|
int index = line.indexOf('=');
|
2010-11-06 23:04:47 +01:00
|
|
|
if(index > 0)
|
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
QString key = line.left(index).trimmed();
|
2010-11-06 23:04:47 +01:00
|
|
|
QString val = line.mid(index+1).trimmed();
|
2012-01-14 20:28:43 +01:00
|
|
|
if(!key.isEmpty())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
updateInfo(audioFile, &skipNext, id_val, &coverType, &coverData, key, val);
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(audioFile.fileName().isEmpty())
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
audioFile.setFileName(baseName);
|
|
|
|
}
|
2010-11-08 00:24:54 +01:00
|
|
|
|
2011-05-21 21:08:10 +02:00
|
|
|
process.waitForFinished();
|
|
|
|
if(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
}
|
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
if((coverType != coverNone) && (!coverData.isEmpty()))
|
2011-03-20 23:32:11 +01:00
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
retrieveCover(audioFile, coverType, coverData);
|
2011-03-20 23:32:11 +01:00
|
|
|
}
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
return audioFile;
|
|
|
|
}
|
|
|
|
|
2012-01-14 20:28:43 +01:00
|
|
|
void FileAnalyzer::updateInfo(AudioFileModel &audioFile, bool *skipNext, unsigned int *id_val, cover_t *coverType, QByteArray *coverData, const QString &key, const QString &value)
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
//qWarning("'%s' -> '%s'", key.toUtf8().constData(), value.toUtf8().constData());
|
|
|
|
|
2012-01-14 20:28:43 +01:00
|
|
|
/*New Stream*/
|
|
|
|
if(IS_KEY("Gen_ID") || IS_KEY("Aud_ID"))
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
if(value.isEmpty())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
*skipNext = false;
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
2012-01-14 20:28:43 +01:00
|
|
|
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;
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
2012-01-14 20:28:43 +01:00
|
|
|
|
|
|
|
/*Skip?*/
|
|
|
|
if((*skipNext) || value.isEmpty())
|
2012-01-14 01:11:58 +01:00
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
return;
|
2012-01-14 01:11:58 +01:00
|
|
|
}
|
2012-01-14 20:28:43 +01:00
|
|
|
|
|
|
|
/*General Section*/
|
|
|
|
if(IS_SEC("Gen"))
|
2012-01-14 01:11:58 +01:00
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
if(IS_KEY("Gen_Format"))
|
|
|
|
{
|
|
|
|
audioFile.setFormatContainerType(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Format_Profile"))
|
|
|
|
{
|
|
|
|
audioFile.setFormatContainerProfile(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Title") || IS_KEY("Gen_Track"))
|
|
|
|
{
|
|
|
|
audioFile.setFileName(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Duration"))
|
|
|
|
{
|
|
|
|
unsigned int tmp = parseDuration(value);
|
|
|
|
if(tmp > 0) audioFile.setFileDuration(tmp);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Artist") || IS_KEY("Gen_Performer"))
|
|
|
|
{
|
|
|
|
audioFile.setFileArtist(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Album"))
|
|
|
|
{
|
|
|
|
audioFile.setFileAlbum(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Genre"))
|
|
|
|
{
|
|
|
|
audioFile.setFileGenre(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Released_Date") || IS_KEY("Gen_Recorded_Date"))
|
|
|
|
{
|
|
|
|
unsigned int tmp = parseYear(value);
|
|
|
|
if(tmp > 0) audioFile.setFileYear(tmp);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Comment"))
|
|
|
|
{
|
|
|
|
audioFile.setFileComment(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Gen_Track/Position"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
|
|
|
if(ok) audioFile.setFilePosition(tmp);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
|
|
|
qWarning("Unknown key '%s' with value '%s' found!", key.toUtf8().constData(), value.toUtf8().constData());
|
|
|
|
}
|
|
|
|
return;
|
2012-01-14 01:11:58 +01:00
|
|
|
}
|
2012-01-14 20:28:43 +01:00
|
|
|
|
|
|
|
/*Audio Section*/
|
|
|
|
if(IS_SEC("Aud"))
|
2012-01-14 01:11:58 +01:00
|
|
|
{
|
2012-01-14 20:28:43 +01:00
|
|
|
|
|
|
|
if(IS_KEY("Aud_Format"))
|
|
|
|
{
|
|
|
|
audioFile.setFormatAudioType(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Format_Profile"))
|
|
|
|
{
|
|
|
|
audioFile.setFormatAudioProfile(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Format_Version"))
|
|
|
|
{
|
|
|
|
audioFile.setFormatAudioVersion(value);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Channel(s)"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
|
|
|
if(ok) audioFile.setFormatAudioChannels(tmp);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_SamplingRate"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
|
|
|
if(ok) audioFile.setFormatAudioSamplerate(tmp);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_BitDepth"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
|
|
|
if(ok) audioFile.setFormatAudioBitdepth(tmp);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_Duration"))
|
|
|
|
{
|
|
|
|
unsigned int tmp = parseDuration(value);
|
|
|
|
if(tmp > 0) audioFile.setFileDuration(tmp);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_BitRate"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int tmp = value.toUInt(&ok);
|
|
|
|
if(ok) audioFile.setFormatAudioBitrate(tmp/1000);
|
|
|
|
}
|
|
|
|
else if(IS_KEY("Aud_BitRate_Mode"))
|
|
|
|
{
|
|
|
|
if(!value.compare("CBR", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeConstant);
|
|
|
|
if(!value.compare("VBR", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeVariable);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("Unknown key '%s' with value '%s' found!", key.toUtf8().constData(), value.toUtf8().constData());
|
|
|
|
}
|
|
|
|
return;
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
2012-01-14 20:28:43 +01:00
|
|
|
|
|
|
|
/*Section not recognized*/
|
|
|
|
qWarning("Unknown section: %s", key.toUtf8().constData());
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:35:17 +01:00
|
|
|
bool FileAnalyzer::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));
|
|
|
|
}
|
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
void FileAnalyzer::retrieveCover(AudioFileModel &audioFile, cover_t coverType, const QByteArray &coverData)
|
2011-03-20 23:32:11 +01:00
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
qDebug("Retrieving cover!");
|
2011-03-20 23:32:11 +01:00
|
|
|
QString extension;
|
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
switch(coverType)
|
2011-03-20 23:32:11 +01:00
|
|
|
{
|
|
|
|
case coverPng:
|
|
|
|
extension = QString::fromLatin1("png");
|
|
|
|
break;
|
|
|
|
case coverGif:
|
|
|
|
extension = QString::fromLatin1("gif");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
extension = QString::fromLatin1("jpg");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
if(!(QImage::fromData(coverData, extension.toUpper().toLatin1().constData()).isNull()))
|
2011-03-20 23:32:11 +01:00
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
QFile coverFile(QString("%1/%2.%3").arg(lamexp_temp_folder2(), lamexp_rand_str(), extension));
|
|
|
|
if(coverFile.open(QIODevice::WriteOnly))
|
2011-05-16 21:02:24 +02:00
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
coverFile.write(coverData);
|
|
|
|
coverFile.close();
|
|
|
|
audioFile.setFileCover(coverFile.fileName(), true);
|
2011-03-20 23:32:11 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-14 01:11:58 +01:00
|
|
|
else
|
2011-05-21 21:08:10 +02:00
|
|
|
{
|
2012-01-14 01:11:58 +01:00
|
|
|
qWarning("Image data seems to be invalid :-(");
|
2011-05-21 21:08:10 +02:00
|
|
|
}
|
2011-03-20 23:32:11 +01:00
|
|
|
}
|
|
|
|
|
2011-05-21 21:08:10 +02:00
|
|
|
bool FileAnalyzer::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;
|
|
|
|
}
|
|
|
|
|
2011-05-21 23:51:28 +02:00
|
|
|
bool bInfoHeaderFound = false;
|
|
|
|
|
2011-05-21 21:08:10 +02:00
|
|
|
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();
|
|
|
|
|
2011-05-21 23:51:28 +02:00
|
|
|
if(bInfoHeaderFound && !key.isEmpty() && !val.isEmpty())
|
2011-05-21 21:08:10 +02:00
|
|
|
{
|
|
|
|
if(key.compare("TotalSeconds", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int duration = val.toUInt(&ok);
|
|
|
|
if(ok) info.setFileDuration(duration);
|
|
|
|
}
|
|
|
|
if(key.compare("SamplesPerSec", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int samplerate = val.toUInt(&ok);
|
|
|
|
if(ok) info.setFormatAudioSamplerate (samplerate);
|
|
|
|
}
|
|
|
|
if(key.compare("Channels", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int channels = val.toUInt(&ok);
|
|
|
|
if(ok) info.setFormatAudioChannels(channels);
|
|
|
|
}
|
|
|
|
if(key.compare("BitsPerSample", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int bitdepth = val.toUInt(&ok);
|
|
|
|
if(ok) info.setFormatAudioBitdepth(bitdepth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(line.contains("[Audio Info]", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
info.setFormatAudioType("Avisynth");
|
|
|
|
info.setFormatContainerType("Avisynth");
|
2011-05-21 23:51:28 +02:00
|
|
|
bInfoHeaderFound = true;
|
2011-05-21 21:08:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.waitForFinished();
|
|
|
|
if(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
}
|
|
|
|
|
2011-05-21 23:51:28 +02:00
|
|
|
//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;
|
|
|
|
}
|
2011-05-21 21:08:10 +02:00
|
|
|
}
|
|
|
|
|
2012-01-14 01:11:58 +01:00
|
|
|
bool FileAnalyzer::createTemplate(void)
|
|
|
|
{
|
|
|
|
if(m_templateFile)
|
|
|
|
{
|
|
|
|
qWarning("Template file already exists!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString templatePath = QString("%1/%2.txt").arg(lamexp_temp_folder2(), lamexp_rand_str());
|
|
|
|
|
|
|
|
QFile templateFile(templatePath);
|
|
|
|
if(!templateFile.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
templateFile.write("General;");
|
|
|
|
for(size_t i = 0; g_tags_gen[i]; i++)
|
|
|
|
{
|
|
|
|
templateFile.write(QString("Gen_%1=%%1%\\n").arg(g_tags_gen[i]).toLatin1().constData());
|
|
|
|
}
|
2012-01-14 20:28:43 +01:00
|
|
|
templateFile.write("\\n\r\n");
|
2012-01-14 01:11:58 +01:00
|
|
|
|
|
|
|
templateFile.write("Audio;");
|
|
|
|
for(size_t i = 0; g_tags_aud[i]; i++)
|
|
|
|
{
|
|
|
|
templateFile.write(QString("Aud_%1=%%1%\\n").arg(g_tags_aud[i]).toLatin1().constData());
|
|
|
|
}
|
2012-01-14 20:28:43 +01:00
|
|
|
templateFile.write("\\n\r\n");
|
2012-01-14 01:11:58 +01:00
|
|
|
|
|
|
|
bool success = (templateFile.error() == QFile::NoError);
|
|
|
|
templateFile.close();
|
|
|
|
|
|
|
|
if(!success)
|
|
|
|
{
|
|
|
|
QFile::remove(templatePath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_templateFile = new LockedFile(templatePath);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
qWarning("Failed to lock template file!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int FileAnalyzer::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 FileAnalyzer::parseDuration(const QString &str)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int value = str.toUInt(&ok);
|
|
|
|
return ok ? (value/1000) : 0;
|
|
|
|
}
|
|
|
|
|
2011-01-30 21:49:32 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Public Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2010-11-11 19:37:16 +01:00
|
|
|
unsigned int FileAnalyzer::filesAccepted(void)
|
|
|
|
{
|
|
|
|
return m_filesAccepted;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int FileAnalyzer::filesRejected(void)
|
|
|
|
{
|
2011-05-17 01:14:50 +02:00
|
|
|
return m_filesRejected;
|
2010-11-11 19:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int FileAnalyzer::filesDenied(void)
|
|
|
|
{
|
|
|
|
return m_filesDenied;
|
|
|
|
}
|
2010-11-06 23:04:47 +01:00
|
|
|
|
2011-03-19 15:35:17 +01:00
|
|
|
unsigned int FileAnalyzer::filesDummyCDDA(void)
|
|
|
|
{
|
|
|
|
return m_filesDummyCDDA;
|
|
|
|
}
|
|
|
|
|
2011-05-17 01:14:50 +02:00
|
|
|
unsigned int FileAnalyzer::filesCueSheet(void)
|
|
|
|
{
|
|
|
|
return m_filesCueSheet;
|
|
|
|
}
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// EVENTS
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2010-11-08 00:24:54 +01:00
|
|
|
/*NONE*/
|