Updated Ukrainian translation.
[LameXP.git] / src / Thread_FileAnalyzer_ST.cpp
blobd2352e85328239543ec65eba49b2ac9cfb8d23a2
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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 // --------------------------------------------------------------------
23 // !!! THIS IS THE OLD SINGLE-THREADED VERSION OF THE FILE ANALYZER !!!
24 // --------------------------------------------------------------------
26 #include "Thread_FileAnalyzer_ST.h"
28 #include "Global.h"
29 #include "LockedFile.h"
30 #include "Model_AudioFile.h"
31 #include "PlaylistImporter.h"
33 #include <QDir>
34 #include <QFileInfo>
35 #include <QProcess>
36 #include <QDate>
37 #include <QTime>
38 #include <QDebug>
39 #include <QImage>
41 #include <math.h>
43 #define IS_KEY(KEY) (key.compare(KEY, Qt::CaseInsensitive) == 0)
44 #define IS_SEC(SEC) (key.startsWith((SEC "_"), Qt::CaseInsensitive))
45 #define FIRST_TOK(STR) (STR.split(" ", QString::SkipEmptyParts).first())
47 ////////////////////////////////////////////////////////////
48 // Constructor
49 ////////////////////////////////////////////////////////////
51 FileAnalyzer_ST::FileAnalyzer_ST(const QStringList &inputFiles)
53 m_inputFiles(inputFiles),
54 m_mediaInfoBin(lamexp_lookup_tool("mediainfo.exe")),
55 m_avs2wavBin(lamexp_lookup_tool("avs2wav.exe")),
56 m_templateFile(NULL),
57 m_abortFlag(false)
59 m_bSuccess = false;
60 m_bAborted = false;
62 if(m_mediaInfoBin.isEmpty())
64 qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
67 m_filesAccepted = 0;
68 m_filesRejected = 0;
69 m_filesDenied = 0;
70 m_filesDummyCDDA = 0;
71 m_filesCueSheet = 0;
74 FileAnalyzer_ST::~FileAnalyzer_ST(void)
76 if(m_templateFile)
78 QString templatePath = m_templateFile->filePath();
79 LAMEXP_DELETE(m_templateFile);
80 if(QFile::exists(templatePath)) QFile::remove(templatePath);
84 ////////////////////////////////////////////////////////////
85 // Static data
86 ////////////////////////////////////////////////////////////
88 const char *FileAnalyzer_ST::g_tags_gen[] =
90 "ID",
91 "Format",
92 "Format_Profile",
93 "Format_Version",
94 "Duration",
95 "Title", "Track",
96 "Track/Position",
97 "Artist", "Performer",
98 "Album",
99 "Genre",
100 "Released_Date", "Recorded_Date",
101 "Comment",
102 "Cover",
103 "Cover_Type",
104 "Cover_Mime",
105 "Cover_Data",
106 NULL
109 const char *FileAnalyzer_ST::g_tags_aud[] =
111 "ID",
112 "Source",
113 "Format",
114 "Format_Profile",
115 "Format_Version",
116 "Channel(s)",
117 "SamplingRate",
118 "BitDepth",
119 "BitRate",
120 "BitRate_Mode",
121 NULL
124 ////////////////////////////////////////////////////////////
125 // Thread Main
126 ////////////////////////////////////////////////////////////
128 void FileAnalyzer_ST::run()
130 m_bSuccess = false;
131 m_bAborted = false;
133 m_filesAccepted = 0;
134 m_filesRejected = 0;
135 m_filesDenied = 0;
136 m_filesDummyCDDA = 0;
137 m_filesCueSheet = 0;
139 m_inputFiles.sort();
140 m_recentlyAdded.clear();
141 m_abortFlag = false;
143 if(!m_templateFile)
145 if(!createTemplate())
147 qWarning("Failed to create template file!");
148 return;
152 while(!m_inputFiles.isEmpty())
154 int fileType = fileTypeNormal;
155 QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
156 qDebug("Analyzing: %s", currentFile.toUtf8().constData());
157 emit fileSelected(QFileInfo(currentFile).fileName());
158 AudioFileModel file = analyzeFile(currentFile, &fileType);
160 if(m_abortFlag)
162 MessageBeep(MB_ICONERROR);
163 m_bAborted = true;
164 qWarning("Operation cancelled by user!");
165 return;
167 if(fileType == fileTypeSkip)
169 qWarning("File was recently added, skipping!");
170 continue;
172 if(fileType == fileTypeDenied)
174 m_filesDenied++;
175 qWarning("Cannot access file for reading, skipping!");
176 continue;
178 if(fileType == fileTypeCDDA)
180 m_filesDummyCDDA++;
181 qWarning("Dummy CDDA file detected, skipping!");
182 continue;
185 if(file.fileName().isEmpty() || file.formatContainerType().isEmpty() || file.formatAudioType().isEmpty())
187 if(PlaylistImporter::importPlaylist(m_inputFiles, currentFile))
189 qDebug("Imported playlist file.");
191 else if(!QFileInfo(currentFile).suffix().compare("cue", Qt::CaseInsensitive))
193 qWarning("Cue Sheet file detected, skipping!");
194 m_filesCueSheet++;
196 else if(!QFileInfo(currentFile).suffix().compare("avs", Qt::CaseInsensitive))
198 qDebug("Found a potential Avisynth script, investigating...");
199 if(analyzeAvisynthFile(currentFile, file))
201 m_filesAccepted++;
202 emit fileAnalyzed(file);
204 else
206 qDebug("Rejected Avisynth file: %s", file.filePath().toUtf8().constData());
207 m_filesRejected++;
210 else
212 qDebug("Rejected file of unknown type: %s", file.filePath().toUtf8().constData());
213 m_filesRejected++;
215 continue;
218 m_filesAccepted++;
219 m_recentlyAdded.append(file.filePath());
220 emit fileAnalyzed(file);
223 qDebug("All files added.\n");
224 m_bSuccess = true;
227 ////////////////////////////////////////////////////////////
228 // Privtae Functions
229 ////////////////////////////////////////////////////////////
231 const AudioFileModel FileAnalyzer_ST::analyzeFile(const QString &filePath, int *type)
233 *type = fileTypeNormal;
235 AudioFileModel audioFile(filePath);
237 if(m_recentlyAdded.contains(filePath, Qt::CaseInsensitive))
239 *type = fileTypeSkip;
240 return audioFile;
243 QFile readTest(filePath);
244 if(!readTest.open(QIODevice::ReadOnly))
246 *type = fileTypeDenied;
247 return audioFile;
249 if(checkFile_CDDA(readTest))
251 *type = fileTypeCDDA;
252 return audioFile;
254 readTest.close();
256 bool skipNext = false;
257 unsigned int id_val[2] = {UINT_MAX, UINT_MAX};
258 cover_t coverType = coverNone;
259 QByteArray coverData;
261 QStringList params;
262 params << QString("--Inform=file://%1").arg(QDir::toNativeSeparators(m_templateFile->filePath()));
263 params << QDir::toNativeSeparators(filePath);
265 QProcess process;
266 process.setProcessChannelMode(QProcess::MergedChannels);
267 process.setReadChannel(QProcess::StandardOutput);
268 process.start(m_mediaInfoBin, params);
270 if(!process.waitForStarted())
272 qWarning("MediaInfo process failed to create!");
273 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
274 process.kill();
275 process.waitForFinished(-1);
276 return audioFile;
279 while(process.state() != QProcess::NotRunning)
281 if(m_abortFlag)
283 process.kill();
284 qWarning("Process was aborted on user request!");
285 break;
288 if(!process.waitForReadyRead())
290 if(process.state() == QProcess::Running)
292 qWarning("MediaInfo time out. Killing process and skipping file!");
293 process.kill();
294 process.waitForFinished(-1);
295 return audioFile;
299 QByteArray data;
301 while(process.canReadLine())
303 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
304 if(!line.isEmpty())
306 //qDebug("Line:%s", line.toUtf8().constData());
308 int index = line.indexOf('=');
309 if(index > 0)
311 QString key = line.left(index).trimmed();
312 QString val = line.mid(index+1).trimmed();
313 if(!key.isEmpty())
315 updateInfo(audioFile, &skipNext, id_val, &coverType, &coverData, key, val);
322 if(audioFile.fileName().isEmpty())
324 QString baseName = QFileInfo(filePath).fileName();
325 int index = baseName.lastIndexOf(".");
327 if(index >= 0)
329 baseName = baseName.left(index);
332 baseName = baseName.replace("_", " ").simplified();
333 index = baseName.lastIndexOf(" - ");
335 if(index >= 0)
337 baseName = baseName.mid(index + 3).trimmed();
340 audioFile.setFileName(baseName);
343 process.waitForFinished();
344 if(process.state() != QProcess::NotRunning)
346 process.kill();
347 process.waitForFinished(-1);
350 if((coverType != coverNone) && (!coverData.isEmpty()))
352 retrieveCover(audioFile, coverType, coverData);
355 return audioFile;
358 void FileAnalyzer_ST::updateInfo(AudioFileModel &audioFile, bool *skipNext, unsigned int *id_val, cover_t *coverType, QByteArray *coverData, const QString &key, const QString &value)
360 //qWarning("'%s' -> '%s'", key.toUtf8().constData(), value.toUtf8().constData());
362 /*New Stream*/
363 if(IS_KEY("Gen_ID") || IS_KEY("Aud_ID"))
365 if(value.isEmpty())
367 *skipNext = false;
369 else
371 //We ignore all ID's, except for the lowest one!
372 bool ok = false;
373 unsigned int id = value.toUInt(&ok);
374 if(ok)
376 if(IS_KEY("Gen_ID")) { id_val[0] = qMin(id_val[0], id); *skipNext = (id > id_val[0]); }
377 if(IS_KEY("Aud_ID")) { id_val[1] = qMin(id_val[1], id); *skipNext = (id > id_val[1]); }
379 else
381 *skipNext = true;
384 if(*skipNext)
386 qWarning("Skipping info for non-primary stream!");
388 return;
391 /*Skip or empty?*/
392 if((*skipNext) || value.isEmpty())
394 return;
397 /*Playlist file?*/
398 if(IS_KEY("Aud_Source"))
400 *skipNext = true;
401 audioFile.setFormatContainerType(QString());
402 audioFile.setFormatAudioType(QString());
403 qWarning("Skipping info for playlist file!");
404 return;
407 /*General Section*/
408 if(IS_SEC("Gen"))
410 if(IS_KEY("Gen_Format"))
412 audioFile.setFormatContainerType(value);
414 else if(IS_KEY("Gen_Format_Profile"))
416 audioFile.setFormatContainerProfile(value);
418 else if(IS_KEY("Gen_Title") || IS_KEY("Gen_Track"))
420 audioFile.setFileName(value);
422 else if(IS_KEY("Gen_Duration"))
424 unsigned int tmp = parseDuration(value);
425 if(tmp > 0) audioFile.setFileDuration(tmp);
427 else if(IS_KEY("Gen_Artist") || IS_KEY("Gen_Performer"))
429 audioFile.setFileArtist(value);
431 else if(IS_KEY("Gen_Album"))
433 audioFile.setFileAlbum(value);
435 else if(IS_KEY("Gen_Genre"))
437 audioFile.setFileGenre(value);
439 else if(IS_KEY("Gen_Released_Date") || IS_KEY("Gen_Recorded_Date"))
441 unsigned int tmp = parseYear(value);
442 if(tmp > 0) audioFile.setFileYear(tmp);
444 else if(IS_KEY("Gen_Comment"))
446 audioFile.setFileComment(value);
448 else if(IS_KEY("Gen_Track/Position"))
450 bool ok = false;
451 unsigned int tmp = value.toUInt(&ok);
452 if(ok) audioFile.setFilePosition(tmp);
454 else if(IS_KEY("Gen_Cover") || IS_KEY("Gen_Cover_Type"))
456 if(*coverType == coverNone)
458 *coverType = coverJpeg;
461 else if(IS_KEY("Gen_Cover_Mime"))
463 QString temp = FIRST_TOK(value);
464 if(!temp.compare("image/jpeg", Qt::CaseInsensitive)) *coverType = coverJpeg;
465 else if(!temp.compare("image/png", Qt::CaseInsensitive)) *coverType = coverPng;
466 else if(!temp.compare("image/gif", Qt::CaseInsensitive)) *coverType = coverGif;
468 else if(IS_KEY("Gen_Cover_Data"))
470 if(!coverData->isEmpty()) coverData->clear();
471 coverData->append(QByteArray::fromBase64(FIRST_TOK(value).toLatin1()));
473 else
475 qWarning("Unknown key '%s' with value '%s' found!", key.toUtf8().constData(), value.toUtf8().constData());
477 return;
480 /*Audio Section*/
481 if(IS_SEC("Aud"))
484 if(IS_KEY("Aud_Format"))
486 audioFile.setFormatAudioType(value);
488 else if(IS_KEY("Aud_Format_Profile"))
490 audioFile.setFormatAudioProfile(value);
492 else if(IS_KEY("Aud_Format_Version"))
494 audioFile.setFormatAudioVersion(value);
496 else if(IS_KEY("Aud_Channel(s)"))
498 bool ok = false;
499 unsigned int tmp = value.toUInt(&ok);
500 if(ok) audioFile.setFormatAudioChannels(tmp);
502 else if(IS_KEY("Aud_SamplingRate"))
504 bool ok = false;
505 unsigned int tmp = value.toUInt(&ok);
506 if(ok) audioFile.setFormatAudioSamplerate(tmp);
508 else if(IS_KEY("Aud_BitDepth"))
510 bool ok = false;
511 unsigned int tmp = value.toUInt(&ok);
512 if(ok) audioFile.setFormatAudioBitdepth(tmp);
514 else if(IS_KEY("Aud_Duration"))
516 unsigned int tmp = parseDuration(value);
517 if(tmp > 0) audioFile.setFileDuration(tmp);
519 else if(IS_KEY("Aud_BitRate"))
521 bool ok = false;
522 unsigned int tmp = value.toUInt(&ok);
523 if(ok) audioFile.setFormatAudioBitrate(tmp/1000);
525 else if(IS_KEY("Aud_BitRate_Mode"))
527 if(!value.compare("CBR", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeConstant);
528 if(!value.compare("VBR", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeVariable);
530 else
532 qWarning("Unknown key '%s' with value '%s' found!", key.toUtf8().constData(), value.toUtf8().constData());
534 return;
537 /*Section not recognized*/
538 qWarning("Unknown section: %s", key.toUtf8().constData());
541 bool FileAnalyzer_ST::checkFile_CDDA(QFile &file)
543 file.reset();
544 QByteArray data = file.read(128);
546 int i = data.indexOf("RIFF");
547 int j = data.indexOf("CDDA");
548 int k = data.indexOf("fmt ");
550 return ((i >= 0) && (j >= 0) && (k >= 0) && (k > j) && (j > i));
553 void FileAnalyzer_ST::retrieveCover(AudioFileModel &audioFile, cover_t coverType, const QByteArray &coverData)
555 qDebug("Retrieving cover!");
556 QString extension;
558 switch(coverType)
560 case coverPng:
561 extension = QString::fromLatin1("png");
562 break;
563 case coverGif:
564 extension = QString::fromLatin1("gif");
565 break;
566 default:
567 extension = QString::fromLatin1("jpg");
568 break;
571 if(!(QImage::fromData(coverData, extension.toUpper().toLatin1().constData()).isNull()))
573 QFile coverFile(QString("%1/%2.%3").arg(lamexp_temp_folder2(), lamexp_rand_str(), extension));
574 if(coverFile.open(QIODevice::WriteOnly))
576 coverFile.write(coverData);
577 coverFile.close();
578 audioFile.setFileCover(coverFile.fileName(), true);
581 else
583 qWarning("Image data seems to be invalid :-(");
587 bool FileAnalyzer_ST::analyzeAvisynthFile(const QString &filePath, AudioFileModel &info)
589 QProcess process;
590 process.setProcessChannelMode(QProcess::MergedChannels);
591 process.setReadChannel(QProcess::StandardOutput);
592 process.start(m_avs2wavBin, QStringList() << QDir::toNativeSeparators(filePath) << "?");
594 if(!process.waitForStarted())
596 qWarning("AVS2WAV process failed to create!");
597 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
598 process.kill();
599 process.waitForFinished(-1);
600 return false;
603 bool bInfoHeaderFound = false;
605 while(process.state() != QProcess::NotRunning)
607 if(m_abortFlag)
609 process.kill();
610 qWarning("Process was aborted on user request!");
611 break;
614 if(!process.waitForReadyRead())
616 if(process.state() == QProcess::Running)
618 qWarning("AVS2WAV time out. Killing process and skipping file!");
619 process.kill();
620 process.waitForFinished(-1);
621 return false;
625 QByteArray data;
627 while(process.canReadLine())
629 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
630 if(!line.isEmpty())
632 int index = line.indexOf(':');
633 if(index > 0)
635 QString key = line.left(index).trimmed();
636 QString val = line.mid(index+1).trimmed();
638 if(bInfoHeaderFound && !key.isEmpty() && !val.isEmpty())
640 if(key.compare("TotalSeconds", Qt::CaseInsensitive) == 0)
642 bool ok = false;
643 unsigned int duration = val.toUInt(&ok);
644 if(ok) info.setFileDuration(duration);
646 if(key.compare("SamplesPerSec", Qt::CaseInsensitive) == 0)
648 bool ok = false;
649 unsigned int samplerate = val.toUInt(&ok);
650 if(ok) info.setFormatAudioSamplerate (samplerate);
652 if(key.compare("Channels", Qt::CaseInsensitive) == 0)
654 bool ok = false;
655 unsigned int channels = val.toUInt(&ok);
656 if(ok) info.setFormatAudioChannels(channels);
658 if(key.compare("BitsPerSample", Qt::CaseInsensitive) == 0)
660 bool ok = false;
661 unsigned int bitdepth = val.toUInt(&ok);
662 if(ok) info.setFormatAudioBitdepth(bitdepth);
666 else
668 if(line.contains("[Audio Info]", Qt::CaseInsensitive))
670 info.setFormatAudioType("Avisynth");
671 info.setFormatContainerType("Avisynth");
672 bInfoHeaderFound = true;
679 process.waitForFinished();
680 if(process.state() != QProcess::NotRunning)
682 process.kill();
683 process.waitForFinished(-1);
686 //Check exit code
687 switch(process.exitCode())
689 case 0:
690 qDebug("Avisynth script was analyzed successfully.");
691 return true;
692 break;
693 case -5:
694 qWarning("It appears that Avisynth is not installed on the system!");
695 return false;
696 break;
697 default:
698 qWarning("Failed to open the Avisynth script, bad AVS file?");
699 return false;
700 break;
704 bool FileAnalyzer_ST::createTemplate(void)
706 if(m_templateFile)
708 qWarning("Template file already exists!");
709 return true;
712 QString templatePath = QString("%1/%2.txt").arg(lamexp_temp_folder2(), lamexp_rand_str());
714 QFile templateFile(templatePath);
715 if(!templateFile.open(QIODevice::WriteOnly))
717 return false;
720 templateFile.write("General;");
721 for(size_t i = 0; g_tags_gen[i]; i++)
723 templateFile.write(QString("Gen_%1=%%1%\\n").arg(g_tags_gen[i]).toLatin1().constData());
725 templateFile.write("\\n\r\n");
727 templateFile.write("Audio;");
728 for(size_t i = 0; g_tags_aud[i]; i++)
730 templateFile.write(QString("Aud_%1=%%1%\\n").arg(g_tags_aud[i]).toLatin1().constData());
732 templateFile.write("\\n\r\n");
734 bool success = (templateFile.error() == QFile::NoError);
735 templateFile.close();
737 if(!success)
739 QFile::remove(templatePath);
740 return false;
745 m_templateFile = new LockedFile(templatePath);
747 catch(...)
749 qWarning("Failed to lock template file!");
750 return false;
753 return true;
756 unsigned int FileAnalyzer_ST::parseYear(const QString &str)
758 if(str.startsWith("UTC", Qt::CaseInsensitive))
760 QDate date = QDate::fromString(str.mid(3).trimmed().left(10), "yyyy-MM-dd");
761 if(date.isValid())
763 return date.year();
765 else
767 return 0;
770 else
772 bool ok = false;
773 int year = str.toInt(&ok);
774 if(ok && year > 0)
776 return year;
778 else
780 return 0;
785 unsigned int FileAnalyzer_ST::parseDuration(const QString &str)
787 bool ok = false;
788 unsigned int value = str.toUInt(&ok);
789 return ok ? (value/1000) : 0;
792 ////////////////////////////////////////////////////////////
793 // Public Functions
794 ////////////////////////////////////////////////////////////
796 unsigned int FileAnalyzer_ST::filesAccepted(void)
798 return m_filesAccepted;
801 unsigned int FileAnalyzer_ST::filesRejected(void)
803 return m_filesRejected;
806 unsigned int FileAnalyzer_ST::filesDenied(void)
808 return m_filesDenied;
811 unsigned int FileAnalyzer_ST::filesDummyCDDA(void)
813 return m_filesDummyCDDA;
816 unsigned int FileAnalyzer_ST::filesCueSheet(void)
818 return m_filesCueSheet;
821 ////////////////////////////////////////////////////////////
822 // EVENTS
823 ////////////////////////////////////////////////////////////
825 /*NONE*/