-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerThread.cpp
More file actions
347 lines (267 loc) · 9.52 KB
/
WorkerThread.cpp
File metadata and controls
347 lines (267 loc) · 9.52 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "WorkerThread.h"
#include <QMediaPlayer>
#include <QThread>
#include <QDebug>
#include <iostream>
#include <ostream>
#include <QFile>
#include <mlt++/Mlt.h>
#include <iostream>
#include <Windows.h>
#include <vector>
#include <string>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QMessageBox>
using namespace Mlt;
using namespace std;
bool checkMP4FileExists(const QString &filePath) {
QFile file(filePath);
if (file.exists()) {
qDebug() << "The file exists:" << filePath;
return true;
} else {
qDebug() << "The file does not exist:" << filePath;
return false;
}
}
QJsonObject readJsonFile(const QString &filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Could not open file:" << filePath;
return QJsonObject();
}
QByteArray fileContent = file.readAll();
file.close();
QJsonDocument jsonDoc = QJsonDocument::fromJson(fileContent);
if (!jsonDoc.isObject()) {
qWarning() << "The JSON document is not an object.";
return QJsonObject();
}
return jsonDoc.object();
}
WorkerThread::WorkerThread(QObject *parent)
: QObject(parent)
{
QByteArray mlt_repo = qgetenv("MLT_REPOSITORY");
if(mlt_repo.isEmpty()){
QMessageBox msgBox;
msgBox.setText("set MLT_REPOSITORY first.");
msgBox.exec();
}
else{
Mlt::Factory::init(qgetenv("MLT_REPOSITORY"));
}
}
void WorkerThread::loadMedia(const QString &filePath)
{
currentFilePath = filePath;
}
WorkerThread::~WorkerThread()
{
delete mediaPlayer;
mediaPlayer = nullptr;
}
void WorkerThread::multi_videos_audio_compose_transition(const std::vector<std::string> &filenames, const std::string &bgm_filename,
float bgm_gain) {
Profile profile; // defaults to dv_pal
profile.set_frame_rate(30, 1);
profile.set_width(640);
profile.set_height(360);
profile.set_progressive(1);
profile.set_sample_aspect(1, 1);
profile.set_display_aspect(16, 9);
profile.set_colorspace(709);
Playlist video_track(profile);
for (auto &filename : filenames) {
Producer producer(profile, NULL, filename.c_str());
video_track.append(producer);
}
Producer bgm_track(profile, NULL, bgm_filename.c_str());
Filter volume(profile, "volume");
volume.set("level", bgm_gain);
bgm_track.attach(volume);
Tractor tractor(profile);
tractor.set_track(bgm_track, 0);
tractor.set_track(video_track, 1);
Transition audio_mixer(profile, "mix");
audio_mixer.set("out", 9999);
tractor.plant_transition(audio_mixer, 0, 1);
// Create a consumer and connect it to the playlist
Consumer consumer(profile, nullptr, nullptr);
// Start playing
consumer.set("real_time", 1);
consumer.set("terminate_on_pause", 1);
consumer.connect(tractor);
consumer.start();
while (!consumer.is_stopped()) {
Sleep(1);
}
consumer.stop();
}
void WorkerThread::videos_audio_compose_transition(const std::string &filename, const std::string &bgm_filename, float bgm_gain) {
Profile profile; // defaults to dv_pal
Playlist video_track(profile);
Producer producer(profile, NULL, filename.c_str());
video_track.append(producer);
Producer bgm_track(profile, NULL, bgm_filename.c_str());
// select [0, bgm_length] of bgm
int bgm_length = bgm_track.get_playtime();
bgm_track.set("int", 0);
bgm_track.set("out", bgm_length - 1);
// set volume of that bgm
Filter volume(profile, "volume");
volume.set("level", bgm_gain);
bgm_track.attach(volume);
// set tractor of video and bgm
Tractor tractor(profile);
tractor.set_track(bgm_track, 0);
tractor.set_track(video_track, 1);
// plant transition
Transition audio_mixer(profile, "mix");
audio_mixer.set("out", bgm_length - 1);
tractor.plant_transition(audio_mixer, 0, 1);
// Create a consumer and connect it to the playlist
// show video
Consumer consumer(profile, NULL, nullptr);
// Start playing
consumer.set("real_time", 1);
consumer.set("terminate_on_pause", 1);
consumer.connect(tractor);
consumer.start();
while (!consumer.is_stopped()) {
Sleep(1);
}
consumer.stop();
}
int WorkerThread::videos_compose_transition(const std::string& filename0, const std::string& filename1){
Mlt::Profile profile("HD_1080p_25"); // Example profile
//
Producer track0(profile, NULL, filename0.c_str());
int length = track0.get_playtime();
int length_out = length - 1;
track0.set("out", length_out);
Playlist track1(profile);
Producer p1(profile, NULL, filename1.c_str());
int length_p1 = p1.get_playtime();
int length_p1_out = length_p1 - 1;
p1.set("out", int(length_p1_out/15));
track1.blank(std::to_string(length_out/4).c_str());
track1.append(p1);
Transition t0(profile, "affine", "70%/70%:30%x30%");
t0.set("progressive", 1);
t0.set("fill", 0);
t0.set("distort", 1);
t0.set("in", int((length_out)/4));
t0.set("out", int((length_out)/4) + int(length_p1_out/15));
t0.set("sliced_composite", 1);
t0.set("a_track", 0);
t0.set("b_track", 1);
Tractor tractor;
tractor.set_track(track0, 0);
tractor.set_track(track1, 1);
tractor.plant_transition(t0, 0, 1);
// save video
Consumer consumer(profile, "avformat","output.mp4");
consumer.set("real_time", 1);
consumer.set("terminate_on_pause", 1);
consumer.connect(tractor);
consumer.run();
while (!consumer.is_stopped()) {
Sleep(1);
}
return 0;
}
int WorkerThread::compose_transition(QJsonObject jsonData){
// |-----------main video----------------| track 0 main video 18s
// |---blank------|-----work video-------| track 1 work video 18s
//
QJsonDocument jsonDoc(jsonData);
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(jsonDoc.toJson(),&err);
if(err.error != QJsonParseError::NoError){
qDebug() << "parse json failed:"<<err.errorString();
return -1;
}
// main video
QString main_video = doc["main_video"].toString();
// added video
QString work_video = doc["work_video"].toString();
// output video
QString out_video = doc["out_video"].toString();
// 从main中截取一片段
double main_video_beg = doc["main_video_beg"].toDouble();
double main_video_end = doc["main_video_end"].toDouble();
// 在work前放置blank,即main播放一段时间(blank的长度)以后,开始播放work
double blank_relative_main = doc["blank"].toDouble();
// 从work中截取一片段
double work_video_beg = doc["work_video_beg"].toDouble();
double work_video_end = doc["work_video_end"].toDouble();
// work video在main video中的位置和大小,相对于main左上角
QString work_video_pos = doc["work_video_pos"].toString();
Mlt::Profile profile("HD_1080p_25"); // Example profile
//
Producer track0(profile, NULL, main_video.toStdString().c_str());
// get length of main video
int length = track0.get_playtime();
int length_out = length - 1;
// for main video: clip it from main_in to main_out as the first track
int main_in = int(length_out * main_video_beg);
int main_out = int(length_out * main_video_end);
track0.set("in", main_in);
track0.set("out", main_out);
Playlist track1(profile); // second track
Producer p1(profile, NULL, work_video.toStdString().c_str());
int length_p1 = p1.get_playtime();
int length_p1_out = length_p1 - 1;
// for work video: clip it from work_in to work_out
int work_in = int(length_p1_out * work_video_beg);
int work_out = int(length_p1_out * work_video_end);
p1.set("in", work_in);
p1.set("out", work_out);
// add a blank at the second track, the length of the blank is blank_main, then append the work video at this track
int blank_main = int(length_out * blank_relative_main);
track1.blank(std::to_string(blank_main).c_str());
track1.append(p1);
Transition t0(profile, "affine", work_video_pos.toStdString().c_str());
t0.set("progressive", 1);
t0.set("fill", 0);
t0.set("distort", 1);
t0.set("in", blank_main);
t0.set("out", blank_main + work_out);
t0.set("sliced_composite", 1);
t0.set("a_track", 0);
t0.set("b_track", 1);
Tractor tractor;
tractor.set_track(track0, 0);
tractor.set_track(track1, 1);
tractor.plant_transition(t0, 0, 1);
// save video
Consumer consumer(profile, "avformat",out_video.toStdString().c_str());
consumer.set("real_time", 1);
consumer.set("terminate_on_pause", 1);
consumer.connect(tractor);
consumer.run();
while (!consumer.is_stopped()) {
Sleep(1);
}
return 0;
}
void WorkerThread::doCompose()
{
QString filePath = "D:/code/editvideo/build/config.json";
QJsonObject jsonData = readJsonFile(filePath);
if (!jsonData.isEmpty()) {
qDebug() << "Parsed JSON data:\n" << jsonData;
}
compose_transition(jsonData);
}
void WorkerThread::doWork()
{
// This is where the media player logic will run.
if (!currentFilePath.isEmpty()) {
// doCompose();
}
}