Fixed a typo, thanks to Rub3n CT for reporting.
[LameXP.git] / src / Thread_FileAnalyzer.cpp
blob6b69ddc13f0f330539706c8c02139435e380e13f
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Thread_FileAnalyzer.h"
24 #include "Global.h"
25 #include "LockedFile.h"
26 #include "Model_AudioFile.h"
27 #include "PlaylistImporter.h"
29 #include <QDir>
30 #include <QFileInfo>
31 #include <QProcess>
32 #include <QDate>
33 #include <QTime>
34 #include <QDebug>
36 #include <math.h>
38 ////////////////////////////////////////////////////////////
39 // Constructor
40 ////////////////////////////////////////////////////////////
42 FileAnalyzer::FileAnalyzer(const QStringList &inputFiles)
44 m_inputFiles(inputFiles),
45 m_mediaInfoBin(lamexp_lookup_tool("mediainfo.exe")),
46 m_avs2wavBin(lamexp_lookup_tool("avs2wav.exe")),
47 m_abortFlag(false)
49 m_bSuccess = false;
50 m_bAborted = false;
52 if(m_mediaInfoBin.isEmpty())
54 qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
57 m_filesAccepted = 0;
58 m_filesRejected = 0;
59 m_filesDenied = 0;
60 m_filesDummyCDDA = 0;
61 m_filesCueSheet = 0;
64 ////////////////////////////////////////////////////////////
65 // Thread Main
66 ////////////////////////////////////////////////////////////
68 void FileAnalyzer::run()
70 m_bSuccess = false;
71 m_bAborted = false;
73 m_filesAccepted = 0;
74 m_filesRejected = 0;
75 m_filesDenied = 0;
76 m_filesDummyCDDA = 0;
77 m_filesCueSheet = 0;
79 m_inputFiles.sort();
80 m_recentlyAdded.clear();
81 m_abortFlag = false;
83 while(!m_inputFiles.isEmpty())
85 int fileType = fileTypeNormal;
86 QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
87 qDebug("Analyzing: %s", currentFile.toUtf8().constData());
88 emit fileSelected(QFileInfo(currentFile).fileName());
89 AudioFileModel file = analyzeFile(currentFile, &fileType);
91 if(m_abortFlag)
93 MessageBeep(MB_ICONERROR);
94 m_bAborted = true;
95 qWarning("Operation cancelled by user!");
96 return;
98 if(fileType == fileTypeSkip)
100 qWarning("File was recently added, skipping!");
101 continue;
103 if(fileType == fileTypeDenied)
105 m_filesDenied++;
106 qWarning("Cannot access file for reading, skipping!");
107 continue;
109 if(fileType == fileTypeCDDA)
111 m_filesDummyCDDA++;
112 qWarning("Dummy CDDA file detected, skipping!");
113 continue;
116 if(file.fileName().isEmpty() || file.formatContainerType().isEmpty() || file.formatAudioType().isEmpty())
118 if(PlaylistImporter::importPlaylist(m_inputFiles, currentFile))
120 qDebug("Imported playlist file.");
122 else if(!QFileInfo(currentFile).suffix().compare("cue", Qt::CaseInsensitive))
124 qWarning("Cue Sheet file detected, skipping!");
125 m_filesCueSheet++;
127 else if(!QFileInfo(currentFile).suffix().compare("avs", Qt::CaseInsensitive))
129 qDebug("Found a potential Avisynth script, investigating...");
130 if(analyzeAvisynthFile(currentFile, file))
132 m_filesAccepted++;
133 emit fileAnalyzed(file);
135 else
137 qDebug("Rejected Avisynth file: %s", file.filePath().toUtf8().constData());
138 m_filesRejected++;
141 else
143 qDebug("Rejected file of unknown type: %s", file.filePath().toUtf8().constData());
144 m_filesRejected++;
146 continue;
149 m_filesAccepted++;
150 m_recentlyAdded.append(file.filePath());
151 emit fileAnalyzed(file);
154 qDebug("All files added.\n");
155 m_bSuccess = true;
158 ////////////////////////////////////////////////////////////
159 // Privtae Functions
160 ////////////////////////////////////////////////////////////
162 const AudioFileModel FileAnalyzer::analyzeFile(const QString &filePath, int *type)
164 *type = fileTypeNormal;
166 AudioFileModel audioFile(filePath);
167 m_currentSection = sectionOther;
168 m_currentCover = coverNone;
170 if(m_recentlyAdded.contains(filePath, Qt::CaseInsensitive))
172 *type = fileTypeSkip;
173 return audioFile;
176 QFile readTest(filePath);
177 if(!readTest.open(QIODevice::ReadOnly))
179 *type = fileTypeDenied;
180 return audioFile;
183 if(checkFile_CDDA(readTest))
185 *type = fileTypeCDDA;
186 return audioFile;
189 readTest.close();
191 QProcess process;
192 process.setProcessChannelMode(QProcess::MergedChannels);
193 process.setReadChannel(QProcess::StandardOutput);
194 process.start(m_mediaInfoBin, QStringList() << QDir::toNativeSeparators(filePath));
196 if(!process.waitForStarted())
198 qWarning("MediaInfo process failed to create!");
199 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
200 process.kill();
201 process.waitForFinished(-1);
202 return audioFile;
205 while(process.state() != QProcess::NotRunning)
207 if(m_abortFlag)
209 process.kill();
210 qWarning("Process was aborted on user request!");
211 break;
214 if(!process.waitForReadyRead())
216 if(process.state() == QProcess::Running)
218 qWarning("MediaInfo time out. Killing process and skipping file!");
219 process.kill();
220 process.waitForFinished(-1);
221 return audioFile;
225 QByteArray data;
227 while(process.canReadLine())
229 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
230 if(!line.isEmpty())
232 int index = line.indexOf(':');
233 if(index > 0)
235 QString key = line.left(index-1).trimmed();
236 QString val = line.mid(index+1).trimmed();
237 if(!key.isEmpty() && !val.isEmpty())
239 updateInfo(audioFile, key, val);
242 else
244 updateSection(line);
250 if(audioFile.fileName().isEmpty())
252 QString baseName = QFileInfo(filePath).fileName();
253 int index = baseName.lastIndexOf(".");
255 if(index >= 0)
257 baseName = baseName.left(index);
260 baseName = baseName.replace("_", " ").simplified();
261 index = baseName.lastIndexOf(" - ");
263 if(index >= 0)
265 baseName = baseName.mid(index + 3).trimmed();
268 audioFile.setFileName(baseName);
271 process.waitForFinished();
272 if(process.state() != QProcess::NotRunning)
274 process.kill();
275 process.waitForFinished(-1);
278 if(m_currentCover != coverNone)
280 retrieveCover(audioFile, filePath);
283 return audioFile;
286 void FileAnalyzer::updateSection(const QString &section)
288 if(section.startsWith("General", Qt::CaseInsensitive))
290 m_currentSection = sectionGeneral;
292 else if(!section.compare("Audio", Qt::CaseInsensitive) || section.startsWith("Audio #1", Qt::CaseInsensitive))
294 m_currentSection = sectionAudio;
296 else if(section.startsWith("Audio", Qt::CaseInsensitive) || section.startsWith("Video", Qt::CaseInsensitive) || section.startsWith("Text", Qt::CaseInsensitive) ||
297 section.startsWith("Menu", Qt::CaseInsensitive) || section.startsWith("Image", Qt::CaseInsensitive) || section.startsWith("Chapters", Qt::CaseInsensitive))
299 m_currentSection = sectionOther;
301 else
303 m_currentSection = sectionOther;
304 qWarning("Unknown section: %s", section.toUtf8().constData());
308 void FileAnalyzer::updateInfo(AudioFileModel &audioFile, const QString &key, const QString &value)
310 switch(m_currentSection)
312 case sectionGeneral:
313 if(!key.compare("Title", Qt::CaseInsensitive) || !key.compare("Track", Qt::CaseInsensitive) || !key.compare("Track Name", Qt::CaseInsensitive))
315 if(audioFile.fileName().isEmpty()) audioFile.setFileName(value);
317 else if(!key.compare("Duration", Qt::CaseInsensitive))
319 if(!audioFile.fileDuration()) audioFile.setFileDuration(parseDuration(value));
321 else if(!key.compare("Artist", Qt::CaseInsensitive) || !key.compare("Performer", Qt::CaseInsensitive))
323 if(audioFile.fileArtist().isEmpty()) audioFile.setFileArtist(value);
325 else if(!key.compare("Album", Qt::CaseInsensitive))
327 if(audioFile.fileAlbum().isEmpty()) audioFile.setFileAlbum(value);
329 else if(!key.compare("Genre", Qt::CaseInsensitive))
331 if(audioFile.fileGenre().isEmpty()) audioFile.setFileGenre(value);
333 else if(!key.compare("Year", Qt::CaseInsensitive) || !key.compare("Recorded Date", Qt::CaseInsensitive) || !key.compare("Encoded Date", Qt::CaseInsensitive))
335 if(!audioFile.fileYear()) audioFile.setFileYear(parseYear(value));
337 else if(!key.compare("Comment", Qt::CaseInsensitive))
339 if(audioFile.fileComment().isEmpty()) audioFile.setFileComment(value);
341 else if(!key.compare("Track Name/Position", Qt::CaseInsensitive))
343 if(!audioFile.filePosition()) audioFile.setFilePosition(value.toInt());
345 else if(!key.compare("Format", Qt::CaseInsensitive))
347 if(audioFile.formatContainerType().isEmpty()) audioFile.setFormatContainerType(value);
349 else if(!key.compare("Format Profile", Qt::CaseInsensitive))
351 if(audioFile.formatContainerProfile().isEmpty()) audioFile.setFormatContainerProfile(value);
353 else if(!key.compare("Cover", Qt::CaseInsensitive) || !key.compare("Cover type", Qt::CaseInsensitive))
355 if(m_currentCover == coverNone) m_currentCover = coverJpeg;
357 else if(!key.compare("Cover MIME", Qt::CaseInsensitive))
359 QString temp = value.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive).first();
360 if(!temp.compare("image/jpeg", Qt::CaseInsensitive))
362 m_currentCover = coverJpeg;
364 else if(!temp.compare("image/png", Qt::CaseInsensitive))
366 m_currentCover = coverPng;
368 else if(!temp.compare("image/gif", Qt::CaseInsensitive))
370 m_currentCover = coverGif;
373 break;
375 case sectionAudio:
376 if(!key.compare("Year", Qt::CaseInsensitive) || !key.compare("Recorded Date", Qt::CaseInsensitive) || !key.compare("Encoded Date", Qt::CaseInsensitive))
378 if(!audioFile.fileYear()) audioFile.setFileYear(parseYear(value));
380 else if(!key.compare("Format", Qt::CaseInsensitive))
382 if(audioFile.formatAudioType().isEmpty()) audioFile.setFormatAudioType(value);
384 else if(!key.compare("Format Profile", Qt::CaseInsensitive))
386 if(audioFile.formatAudioProfile().isEmpty()) audioFile.setFormatAudioProfile(value);
388 else if(!key.compare("Format Version", Qt::CaseInsensitive))
390 if(audioFile.formatAudioVersion().isEmpty()) audioFile.setFormatAudioVersion(value);
392 else if(!key.compare("Channel(s)", Qt::CaseInsensitive))
394 if(!audioFile.formatAudioChannels()) audioFile.setFormatAudioChannels(value.split(" ", QString::SkipEmptyParts).first().toUInt());
396 else if(!key.compare("Sampling rate", Qt::CaseInsensitive))
398 if(!audioFile.formatAudioSamplerate())
400 bool ok = false;
401 float fTemp = abs(value.split(" ", QString::SkipEmptyParts).first().toFloat(&ok));
402 if(ok) audioFile.setFormatAudioSamplerate(static_cast<unsigned int>(floor(fTemp * 1000.0f + 0.5f)));
405 else if(!key.compare("Bit depth", Qt::CaseInsensitive))
407 if(!audioFile.formatAudioBitdepth()) audioFile.setFormatAudioBitdepth(value.split(" ", QString::SkipEmptyParts).first().toUInt());
409 else if(!key.compare("Duration", Qt::CaseInsensitive))
411 if(!audioFile.fileDuration()) audioFile.setFileDuration(parseDuration(value));
413 else if(!key.compare("Bit rate", Qt::CaseInsensitive))
415 if(!audioFile.formatAudioBitrate())
417 bool ok = false;
418 unsigned int uiTemp = value.split(" ", QString::SkipEmptyParts).first().toUInt(&ok);
419 if(ok)
421 audioFile.setFormatAudioBitrate(uiTemp);
423 else
425 float fTemp = abs(value.split(" ", QString::SkipEmptyParts).first().toFloat(&ok));
426 if(ok) audioFile.setFormatAudioBitrate(static_cast<unsigned int>(floor(fTemp + 0.5f)));
430 else if(!key.compare("Bit rate mode", Qt::CaseInsensitive))
432 if(audioFile.formatAudioBitrateMode() == AudioFileModel::BitrateModeUndefined)
434 if(!value.compare("Constant", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeConstant);
435 if(!value.compare("Variable", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeVariable);
438 break;
442 unsigned int FileAnalyzer::parseYear(const QString &str)
444 if(str.startsWith("UTC", Qt::CaseInsensitive))
446 QDate date = QDate::fromString(str.mid(3).trimmed().left(10), "yyyy-MM-dd");
447 if(date.isValid())
449 return date.year();
451 else
453 return 0;
456 else
458 bool ok = false;
459 int year = str.toInt(&ok);
460 if(ok && year > 0)
462 return year;
464 else
466 return 0;
471 unsigned int FileAnalyzer::parseDuration(const QString &str)
473 QTime time;
475 time = QTime::fromString(str, "z'ms'");
476 if(time.isValid())
478 return max(1, (time.hour() * 60 * 60) + (time.minute() * 60) + time.second());
481 time = QTime::fromString(str, "s's 'z'ms'");
482 if(time.isValid())
484 return max(1, (time.hour() * 60 * 60) + (time.minute() * 60) + time.second());
487 time = QTime::fromString(str, "m'mn 's's'");
488 if(time.isValid())
490 return max(1, (time.hour() * 60 * 60) + (time.minute() * 60) + time.second());
493 time = QTime::fromString(str, "h'h 'm'mn'");
494 if(time.isValid())
496 return max(1, (time.hour() * 60 * 60) + (time.minute() * 60) + time.second());
499 return 0;
502 bool FileAnalyzer::checkFile_CDDA(QFile &file)
504 file.reset();
505 QByteArray data = file.read(128);
507 int i = data.indexOf("RIFF");
508 int j = data.indexOf("CDDA");
509 int k = data.indexOf("fmt ");
511 return ((i >= 0) && (j >= 0) && (k >= 0) && (k > j) && (j > i));
514 void FileAnalyzer::retrieveCover(AudioFileModel &audioFile, const QString &filePath)
516 qDebug("Retrieving cover from: %s", filePath.toUtf8().constData());
517 QString extension;
519 switch(m_currentCover)
521 case coverPng:
522 extension = QString::fromLatin1("png");
523 break;
524 case coverGif:
525 extension = QString::fromLatin1("gif");
526 break;
527 default:
528 extension = QString::fromLatin1("jpg");
529 break;
532 QProcess process;
533 process.setProcessChannelMode(QProcess::MergedChannels);
534 process.setReadChannel(QProcess::StandardOutput);
535 process.start(m_mediaInfoBin, QStringList() << "-f" << QDir::toNativeSeparators(filePath));
537 if(!process.waitForStarted())
539 qWarning("MediaInfo process failed to create!");
540 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
541 process.kill();
542 process.waitForFinished(-1);
543 return;
546 while(process.state() != QProcess::NotRunning)
548 if(m_abortFlag)
550 process.kill();
551 qWarning("Process was aborted on user request!");
552 break;
555 if(!process.waitForReadyRead())
557 if(process.state() == QProcess::Running)
559 qWarning("MediaInfo time out. Killing process and skipping file!");
560 process.kill();
561 process.waitForFinished(-1);
562 return;
566 while(process.canReadLine())
568 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
569 if(!line.isEmpty())
571 int index = line.indexOf(':');
572 if(index > 0)
574 QString key = line.left(index-1).trimmed();
575 QString val = line.mid(index+1).trimmed();
576 if(!key.isEmpty() && !val.isEmpty())
578 if(!key.compare("Cover_Data", Qt::CaseInsensitive))
580 if(val.indexOf(" ") > 0)
582 val = val.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive).first();
584 QByteArray coverData = QByteArray::fromBase64(val.toLatin1());
585 QFile coverFile(QString("%1/%2.%3").arg(lamexp_temp_folder2(), lamexp_rand_str(), extension));
586 if(coverFile.open(QIODevice::WriteOnly))
588 coverFile.write(coverData);
589 coverFile.close();
590 audioFile.setFileCover(coverFile.fileName(), true);
592 break;
600 process.waitForFinished();
601 if(process.state() != QProcess::NotRunning)
603 process.kill();
604 process.waitForFinished(-1);
608 bool FileAnalyzer::analyzeAvisynthFile(const QString &filePath, AudioFileModel &info)
610 QProcess process;
611 process.setProcessChannelMode(QProcess::MergedChannels);
612 process.setReadChannel(QProcess::StandardOutput);
613 process.start(m_avs2wavBin, QStringList() << QDir::toNativeSeparators(filePath) << "?");
615 if(!process.waitForStarted())
617 qWarning("AVS2WAV process failed to create!");
618 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
619 process.kill();
620 process.waitForFinished(-1);
621 return false;
624 bool bInfoHeaderFound = false;
626 while(process.state() != QProcess::NotRunning)
628 if(m_abortFlag)
630 process.kill();
631 qWarning("Process was aborted on user request!");
632 break;
635 if(!process.waitForReadyRead())
637 if(process.state() == QProcess::Running)
639 qWarning("AVS2WAV time out. Killing process and skipping file!");
640 process.kill();
641 process.waitForFinished(-1);
642 return false;
646 QByteArray data;
648 while(process.canReadLine())
650 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
651 if(!line.isEmpty())
653 int index = line.indexOf(':');
654 if(index > 0)
656 QString key = line.left(index).trimmed();
657 QString val = line.mid(index+1).trimmed();
659 if(bInfoHeaderFound && !key.isEmpty() && !val.isEmpty())
661 if(key.compare("TotalSeconds", Qt::CaseInsensitive) == 0)
663 bool ok = false;
664 unsigned int duration = val.toUInt(&ok);
665 if(ok) info.setFileDuration(duration);
667 if(key.compare("SamplesPerSec", Qt::CaseInsensitive) == 0)
669 bool ok = false;
670 unsigned int samplerate = val.toUInt(&ok);
671 if(ok) info.setFormatAudioSamplerate (samplerate);
673 if(key.compare("Channels", Qt::CaseInsensitive) == 0)
675 bool ok = false;
676 unsigned int channels = val.toUInt(&ok);
677 if(ok) info.setFormatAudioChannels(channels);
679 if(key.compare("BitsPerSample", Qt::CaseInsensitive) == 0)
681 bool ok = false;
682 unsigned int bitdepth = val.toUInt(&ok);
683 if(ok) info.setFormatAudioBitdepth(bitdepth);
687 else
689 if(line.contains("[Audio Info]", Qt::CaseInsensitive))
691 info.setFormatAudioType("Avisynth");
692 info.setFormatContainerType("Avisynth");
693 bInfoHeaderFound = true;
700 process.waitForFinished();
701 if(process.state() != QProcess::NotRunning)
703 process.kill();
704 process.waitForFinished(-1);
707 //Check exit code
708 switch(process.exitCode())
710 case 0:
711 qDebug("Avisynth script was analyzed successfully.");
712 return true;
713 break;
714 case -5:
715 qWarning("It appears that Avisynth is not installed on the system!");
716 return false;
717 break;
718 default:
719 qWarning("Failed to open the Avisynth script, bad AVS file?");
720 return false;
721 break;
725 ////////////////////////////////////////////////////////////
726 // Public Functions
727 ////////////////////////////////////////////////////////////
729 unsigned int FileAnalyzer::filesAccepted(void)
731 return m_filesAccepted;
734 unsigned int FileAnalyzer::filesRejected(void)
736 return m_filesRejected;
739 unsigned int FileAnalyzer::filesDenied(void)
741 return m_filesDenied;
744 unsigned int FileAnalyzer::filesDummyCDDA(void)
746 return m_filesDummyCDDA;
749 unsigned int FileAnalyzer::filesCueSheet(void)
751 return m_filesCueSheet;
754 ////////////////////////////////////////////////////////////
755 // EVENTS
756 ////////////////////////////////////////////////////////////
758 /*NONE*/