-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlaylistWorker.cpp
More file actions
204 lines (162 loc) · 6.03 KB
/
PlaylistWorker.cpp
File metadata and controls
204 lines (162 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
File: PlaylistWorker.cpp
Created on: 17/5/2015
Author: Felix
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 3 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, see <http://www.gnu.org/licenses/>.
*/
// Project
#include "PlaylistWorker.h"
#include "Utils.h"
// libav
extern "C"
{
#include <libavformat/avformat.h>
}
const QString PLAYLIST_EXTENSION = QString(".m3u8");
const QChar SEPARATOR = QChar('/');
//-----------------------------------------------------------------
PlaylistWorker::PlaylistWorker(const QFileInfo &source_info, const Utils::TranscoderConfiguration& configuration)
: AudioWorker(source_info, configuration)
{
}
//-----------------------------------------------------------------
void PlaylistWorker::run_implementation()
{
generate_playlist();
}
//-----------------------------------------------------------------
QStringList PlaylistWorker::get_file_names() const
{
QStringList fileNames, filter;
filter << QObject::tr("*.mp3");
auto files = Utils::findFiles(QDir(m_source_info.absoluteFilePath()), filter, false);
for(auto file: files)
{
fileNames << file.absoluteFilePath();
}
fileNames.sort();
return fileNames;
}
//-----------------------------------------------------------------
void PlaylistWorker::generate_playlist()
{
av_register_all();
auto files = get_file_names();
const auto dirName = m_source_info.absoluteFilePath().replace(SEPARATOR,QDir::separator());
if(files.empty())
{
emit information_message(QString("There aren't MP3 files in folder '%1'.").arg(dirName));
return;
}
auto playlistname = m_source_info.absoluteFilePath().split(SEPARATOR).last();
auto baseName = Utils::formatString(playlistname, m_configuration.formatConfiguration(), false);
QFile playlist(m_source_info.absoluteFilePath() + QDir::separator() + baseName + PLAYLIST_EXTENSION);
if(!playlist.open(QFile::WriteOnly|QFile::Truncate))
{
emit error_message(QString("Couldn't create playlist file in folder '%1'.").arg(dirName));
m_fail = true;
return;
}
emit information_message(QString("Creating playlist for %1%2").arg(dirName).arg(QDir::separator()));
auto newline = QString("\n");
QByteArray contents;
contents.append((QString("#EXTM3U") + newline).toLocal8Bit());
int progressVal = 0;
for(auto file: files)
{
const int currentProgress = ((files.indexOf(file) + 1) * 100) / files.size();
if(progressVal != currentProgress)
{
progressVal = currentProgress;
emit progress(progressVal);
}
long long duration = 0;
if(has_been_cancelled() || !get_song_duration(file, duration))
{
playlist.close();
playlist.remove();
return;
}
auto basename = file.split(SEPARATOR).last().toUtf8();
contents.append((QString("#EXTINF:") +
QString().number(duration) +
QString(",") +
basename.split('.').first() +
newline).toLocal8Bit());
contents.append((basename + newline).toLocal8Bit());
}
playlist.write(contents);
playlist.close();
}
//-----------------------------------------------------------------
bool PlaylistWorker::get_song_duration(const QString &file_name, long long &duration)
{
QFile input_file(file_name);
if(!input_file.open(QIODevice::ReadOnly))
{
emit error_message(QString("Couldn't open input file '%1'.").arg(file_name));
return false;
}
QMutexLocker lock(&s_mutex);
av_register_all();
auto ioBuffer = reinterpret_cast<unsigned char *>(av_malloc(s_io_buffer_size)); // can get freed with av_free() by libav
if(nullptr == ioBuffer)
{
emit error_message(QString("Couldn't allocate buffer for custom libav IO for file: '%1'.").arg(file_name));
m_fail = true;
return false;
}
auto avioContext = avio_alloc_context(ioBuffer, s_io_buffer_size - AV_INPUT_BUFFER_PADDING_SIZE, 0, reinterpret_cast<void*>(&input_file), &custom_IO_read, nullptr, &custom_IO_seek);
if(nullptr == avioContext)
{
emit error_message(QString("Couldn't allocate context for custom libav IO for file: '%1'.").arg(file_name));
m_fail = true;
return false;
}
avioContext->seekable = 0;
avioContext->write_flag = 0;
m_libav_context = avformat_alloc_context();
m_libav_context->pb = avioContext;
m_libav_context->flags |= AVFMT_FLAG_CUSTOM_IO;
auto value = avformat_open_input(&m_libav_context, "dummy", nullptr, nullptr);
if(value < 0)
{
emit error_message(QString("Couldn't open file: '%1' with libav. Error is \"%2\"").arg(file_name).arg(av_error_string(value)));
deinit_libav();
m_fail = true;
return false;
}
// avoids a warning message from libav when the duration can't be calculated accurately. this increases the lookup frames.
m_libav_context->max_analyze_duration *= 1000;
value = avformat_find_stream_info(m_libav_context, nullptr);
if(value < 0)
{
emit error_message(QString("Couldn't get the information of '%1'. Error is \"%2\".").arg(file_name).arg(av_error_string(value)));
deinit_libav();
m_fail = true;
return false;
}
AVCodec *dummy_decoder = nullptr;
auto stream_id = av_find_best_stream(m_libav_context, AVMEDIA_TYPE_AUDIO, -1, -1, &dummy_decoder, 0);
if (stream_id < 0)
{
emit error_message(QString("Couldn't find any audio stream in '%1'. Error is \"%2\".").arg(file_name).arg(av_error_string(stream_id)));
deinit_libav();
m_fail = true;
return false;
}
auto stream = m_libav_context->streams[stream_id];
duration = (stream->duration * stream->time_base.num) / stream->time_base.den;
deinit_libav();
input_file.close();
return true;
}