-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeghandler.cpp
632 lines (505 loc) · 19.5 KB
/
ffmpeghandler.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#include <string>
#include <chrono>
#include <iostream>
#include <fstream>
#include "ffmpeghandler.h"
#include "logger.h"
std::map<std::string, std::string> transparentVideos;
bool endsWith(const std::string& str, const std::string& suffix) {
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}
std::vector<std::string> split(std::string s, std::string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr (pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back (token);
}
res.push_back (s.substr (pos_start));
return res;
}
FFmpegHandler::FFmpegHandler(std::string browserIp, int browserPort, std::string vdrIp, int vdrPort, TranscodeConfig& tc, BrowserClient *client, std::string movie_path, const std::string& tmovie) : browserIp(browserIp), browserPort(browserPort), browserClient(client), transcodeConfig(tc), movie_path(movie_path), transparent_movie(tmovie) {
streamHandler = nullptr;
streamError = false;
stopRequest = false;
vdrClient = new VdrClient(vdrIp, vdrPort);
}
FFmpegHandler::~FFmpegHandler() {
streamError = false;
stopRequest = false;
stopVideo();
remove((movie_path + "/" + transparentVideoFile).c_str());
delete vdrClient;
}
std::shared_ptr<std::string> FFmpegHandler::probeVideo(std::string url, std::string position, std::string cookies, std::string referer, std::string userAgent, std::string postfix) {
currentUrl = url;
this->cookies = cookies;
this->referer = referer;
this->userAgent = userAgent;
this->postfix = postfix;
// create transparent video
DEBUG("Create empty video");
transparentVideoFile = "transparent-video-" + browserIp + "_" + std::to_string(browserPort) + "-" + this->postfix + ".webm";
struct stat sb{};
if ((stat(transparentVideoFile.c_str(), &sb) != -1) && (remove(transparentVideoFile.c_str()) != 0)) {
ERROR("File {} exists and delete failed. Aborting...\n", transparentVideoFile.c_str());
return nullptr;
}
std::shared_ptr<std::string> videoInfo;
if (endsWith(url, ".m3u") || endsWith(url, ".m3u8")) {
auto m = M3u8Handler(url);
m3u = m.parseM3u();
if (m3u.height == 0 && m3u.width == 0) {
// something went wrong
return nullptr;
}
duration = "N/A";
videoInfo = std::make_shared<std::string>();
*videoInfo = std::string("m3u video stream");
} else {
videoInfo = probe(url);
}
if (videoInfo == nullptr) {
return nullptr;
}
if (createVideoWithLength(duration, transparentVideoFile)) {
return videoInfo;
}
return nullptr;
}
std::vector<std::string> FFmpegHandler::prepareStreamCmd(std::string url, std::string position, std::string cookies, std::string referer, std::string userAgent) {
// create parameter list
std::vector<std::string> callStr {
"ffmpeg", "-hide_banner", "-re", "-y"
};
if (!referer.empty()) {
callStr.push_back("-referer");
callStr.push_back(referer);
}
if (!userAgent.empty()) {
callStr.push_back("-user_agent");
callStr.push_back(userAgent);
}
if (!cookies.empty()) {
callStr.push_back("-headers");
callStr.push_back("Cookie: " + cookies);
}
if (transcodeConfig.threads() > 0) {
callStr.emplace_back("-threads");
callStr.emplace_back(std::to_string(transcodeConfig.threads()));
}
// in case of mpeg-dash ignore the seek command
if (!endsWith(url, ".mpd")) {
callStr.emplace_back("-ss");
callStr.emplace_back(position);
}
callStr.emplace_back("-i");
callStr.emplace_back(url);
if (!ffmpegCopy) {
// copy dedicated streams
callStr.emplace_back("-c:v");
callStr.emplace_back("copy");
callStr.emplace_back("-c:a");
callStr.emplace_back("copy");
callStr.emplace_back("-ar");
callStr.emplace_back("48000");
// add video stream
for (auto &v: bstreams) {
if (v["codec_type"] == "video") {
callStr.emplace_back("-map");
callStr.emplace_back("0:" + std::to_string((int) v["index"]));
callStr.emplace_back("-c:" + std::to_string((int) v["index"]));
if (transcodeConfig.isCopyVideo(v["codec_name"])) {
callStr.emplace_back("copy");
} else {
auto videoParameter = transcodeConfig.getVideoTranscodeParameter();
callStr.insert(callStr.end(), videoParameter.begin(), videoParameter.end());
}
}
}
// add audio streams
for (auto &v: bstreams) {
if (v["codec_type"] == "audio") {
callStr.emplace_back("-map");
callStr.emplace_back("0:" + std::to_string((int) v["index"]));
callStr.emplace_back("-c:" + std::to_string((int) v["index"]));
if (transcodeConfig.isCopyAudio(v["codec_name"], v["sample_rate"])) {
callStr.emplace_back("copy");
callStr.emplace_back("-ar");
callStr.emplace_back("48000");
} else {
auto audioParameter = transcodeConfig.getAudioTranscodeParameter();
callStr.insert(callStr.end(), audioParameter.begin(), audioParameter.end());
}
}
}
} else {
// let ffmpeg decide which streams to copy
callStr.emplace_back("-codec");
callStr.emplace_back("copy");
}
callStr.emplace_back("-f");
callStr.emplace_back("mpegts");
callStr.emplace_back("pipe:1");
std::ostringstream paramOut;
if (!callStr.empty()) {
std::copy(callStr.begin(), callStr.end() - 1, std::ostream_iterator<std::string>(paramOut, " "));
paramOut << callStr.back();
}
DEBUG("Call ffmpeg: {}", paramOut.str());
return callStr;
}
std::vector<std::string> FFmpegHandler::prepareStreamM3uCmd(std::string url, std::string position, std::string cookies, std::string referer, std::string userAgent) {
// create parameter list
std::vector<std::string> callStr {
"ffmpeg", "-hide_banner", "-re", "-y"
};
if (!referer.empty()) {
callStr.push_back("-referer");
callStr.push_back(referer);
}
if (!userAgent.empty()) {
callStr.push_back("-user_agent");
callStr.push_back(userAgent);
}
if (!cookies.empty()) {
callStr.push_back("-headers");
callStr.push_back("Cookie: " + cookies);
}
if (transcodeConfig.threads() > 0) {
callStr.emplace_back("-threads");
callStr.emplace_back(std::to_string(transcodeConfig.threads()));
}
// in case of mpeg-dash ignore the seek command
if (!endsWith(url, ".mpd")) {
callStr.emplace_back("-ss");
callStr.emplace_back(position);
}
// add main input
callStr.emplace_back("-i");
callStr.emplace_back(m3u.url);
// add optional audio input
std::vector<std::string> audioMetadata;
for (auto a : m3u.audio) {
callStr.emplace_back("-i");
callStr.emplace_back(a.uri);
// fill audio info in bstreams
json st = json::parse("{ \"codec_type\": \"audio\", \"tags\": { \"language\": \"" + a.language + "\", \"comment\": \"" + a.name + "\" }}");
bstreams.push_back(st);
}
// transmux
callStr.emplace_back("-codec");
callStr.emplace_back("copy");
// main input
if (!m3u.audio.empty()) {
callStr.emplace_back("-map");
callStr.emplace_back("0:v");
int idx = 1;
for (auto a: m3u.audio) {
callStr.emplace_back("-map");
callStr.emplace_back(std::to_string(idx) + ":a");
idx++;
}
callStr.insert(std::end(callStr), std::begin(audioMetadata), std::end(audioMetadata));
}
callStr.emplace_back("-f");
callStr.emplace_back("mpegts");
callStr.emplace_back("pipe:1");
std::ostringstream paramOut;
if (!callStr.empty()) {
std::copy(callStr.begin(), callStr.end() - 1, std::ostream_iterator<std::string>(paramOut, " "));
paramOut << callStr.back();
}
DEBUG("Call ffmpeg: {}", paramOut.str());
return callStr;
}
bool FFmpegHandler::streamVideo(std::string url, std::string position, std::string cookies, std::string referer, std::string userAgent) {
currentUrl = url;
this->cookies = cookies;
this->referer = referer;
this->userAgent = userAgent;
DEBUG("StreamVideo: {} -> {}", position, url);
// create parameter list
std::vector<std::string> callStr;
if (endsWith(url, ".m3u") || endsWith(url, ".m3u8")) {
DEBUG("Pepare stream video (m3u)");
callStr = prepareStreamM3uCmd(url, position, cookies, referer, userAgent);
} else {
DEBUG("Pepare stream video (non-m3u)");
callStr = prepareStreamCmd(url, position, cookies, referer, userAgent);
}
streamHandler = new TinyProcessLib::Process(callStr, "",
[this](const char *bytes, size_t n) {
if (stopRequest) {
return;
}
if (hasStreamError()) {
browserClient->StreamError("Encrypted Stream");
} else {
if (!browserClient->Heartbeat()) {
// connection problems? abort transcoding
ERROR("Unable to connect to browser. Abort transcoding...");
// stopVideo();
stopRequest = true;
}
if (!vdrClient->ProcessTSPacket(std::string(bytes, n))) {
// connection problems? abort transcoding
// This can be happen by intention, e.g. if VDR stops the video player
DEBUG("Unable to connect to vdr. Abort transcoding...");
stopRequest = true;
}
}
},
[this](const char *bytes, size_t n) {
// catch errors
std::string msg = std::string(bytes, n);
if (msg.find("Failed to seek for auxiliary info") != std::string::npos) {
// stop reader thread
streamError = true;
}
TRACE("{}", std::string(bytes, n));
},
true
);
return true;
}
bool FFmpegHandler::pauseVideo() {
if (streamHandler != nullptr) {
stopVideo();
}
return true;
}
bool FFmpegHandler::resumeVideo(std::string position) {
streamVideo(currentUrl, position, cookies, referer, userAgent);
return true;
}
void FFmpegHandler::stopVideo() {
if (streamHandler != nullptr) {
streamHandler->kill(true);
streamHandler->get_exit_status();
delete streamHandler;
streamHandler = nullptr;
}
}
bool FFmpegHandler::seekTo(std::string pos) {
pauseVideo();
vdrClient->Seeked();
resumeVideo(pos);
return true;
}
std::shared_ptr<std::string> FFmpegHandler::probe(const std::string& url) {
auto output = std::make_shared<std::string>();
auto videoResult = std::make_shared<std::string>();
bstreams.clear();
// get stream infos
DEBUG("Starte ffprobe");
std::vector<std::string> callStr{
"ffprobe", "-hide_banner", "-i", url,
"-loglevel", "quiet"
};
if (!referer.empty()) {
callStr.push_back("-referer");
callStr.push_back(referer);
}
if (!userAgent.empty()) {
callStr.push_back("-user_agent");
callStr.push_back(userAgent);
}
if (!cookies.empty()) {
callStr.push_back("-headers");
callStr.push_back("Cookie: " + cookies);
}
callStr.push_back("-print_format");
callStr.push_back("json");
callStr.push_back("-show_format");
callStr.push_back("-show_streams");
callStr.push_back("-show_entries");
callStr.push_back("format=duration");
TinyProcessLib::Process process(callStr, "", [output](const char *bytes, size_t n) {
*output += std::string(bytes, n);
});
int exit = process.get_exit_status();
DEBUG("Probe exit code: {}", exit);
if (exit == 1) {
ERROR("Probe of '{}' failed.", url);
return nullptr;
}
std::ostringstream paramOut;
if (!callStr.empty()) {
std::copy(callStr.begin(), callStr.end() - 1, std::ostream_iterator<std::string>(paramOut, " "));
paramOut << callStr.back();
}
DEBUG("Call ffprobe: {}", paramOut.str());
DEBUG("OUTPUT( {} ) {}", exit, *output);
*videoResult = "no video";
json st = json::parse(*output);
ffmpegCopy = false;
json bestProgram;
int bestWidth = -1;
json bestVideo;
unsigned long bestAudioBitrate = 0;
json bestAudio;
auto prgs = st["programs"];
// search the best video in programs
for (auto& el1 : prgs.items()) {
for (auto& el2 : el1.value()) {
for (auto& el3 : el2) {
if (el3.contains("width")) {
int w = el3["width"];
if (w > bestWidth) {
bestProgram = el2;
bestWidth = w;
std::string sample_rate;
if (el3.contains("sample_rate")) {
sample_rate = el3["sample_rate"];
} else {
sample_rate = "0";
}
std::string bit_rate;
if (el3.contains("bit_rate")) {
sample_rate = el3["bit_rate"];
} else {
sample_rate = "0";
}
std::string cn = el3["codec_name"];
std::string cnt = el3["codec_tag_string"];
*videoResult = cn + "/" + cnt + "/" + sample_rate + "/" + bit_rate;
}
}
}
}
}
DEBUG("Best Programms, size {}", bestProgram.size());
// programs not found. search all streams and get best video/audio
if (bestProgram == nullptr || bestProgram.empty()) {
for (auto& el : st["streams"]) {
if (el.contains("width")) {
int w = el["width"];
if (w > bestWidth) {
bestVideo = el;
bestWidth = w;
std::string sample_rate;
if (el.contains("sample_rate")) {
sample_rate = el["sample_rate"];
} else {
sample_rate = "0";
}
std::string bit_rate;
if (el.contains("bit_rate")) {
sample_rate = el["bit_rate"];
} else {
sample_rate = "0";
}
std::string cn = el["codec_name"];
std::string cnt = el["codec_tag_string"];
*videoResult = cn + "/" + cnt + "/" + sample_rate + "/" + bit_rate;
}
} else if (el.contains("codec_type") && el["codec_type"] == "audio") {
// check if the stream has a sample rate
if (el.contains("sample_rate")) {
std::string sr = el["sample_rate"];
if (sr == "0") {
int idx = el["index"];
// it is possible that the valid audio streams are shuffled.
// Let ffmpeg decide which streams to copy.
ffmpegCopy = true;
continue;
}
if (el.contains("tags")) {
if (el["tags"].contains("variant_bitrate")) {
std::string br = el["tags"]["variant_bitrate"];
unsigned long currentBitrate = std::atol(br.c_str());
DEBUG("CurrentBitrate: {}, BestBitrate {}", currentBitrate, bestAudioBitrate);
if (currentBitrate > bestAudioBitrate) {
bestAudioBitrate = currentBitrate;
bestAudio = el;
DEBUG("Set Best AUDIO to {}", bestAudio.dump());
}
} else if (el["tags"].contains("language")) {
// always add this audio track
bestProgram.push_back(el);
} else {
bestProgram.push_back(el);
}
}
}
}
}
if (bestVideo.size() > 0) {
bestProgram.push_back(bestVideo);
}
if (bestAudio.size() > 0) {
bestProgram.push_back(bestAudio);
}
}
DEBUG("videoResult: {}", *videoResult);
auto f = st["format"];
if (f.contains("duration")) {
duration = f["duration"];
} else {
duration = "N/A";
}
bstreams = bestProgram;
DEBUG("Result of bestStreams: {}", bstreams.dump());
return videoResult;
}
bool FFmpegHandler::createVideoWithLength(std::string seconds, const std::string& name) {
auto output = std::make_shared<std::string>();
std::string video;
DEBUG("CreateVideoWithLength: seconds:{} seconds, name:{}", seconds, name);
if (seconds == "N/A") {
// not necessary to remux, split or anything else
transparentVideos.erase(name);
transparentVideos[name] = transparent_movie;
return true;
}
std::vector<std::string> callStr {
"ffmpeg", "-hide_banner" , "-y", "-i", "pipe:0", "-t", seconds, "-codec", "copy", "-f", "webm", "pipe:1"
};
TinyProcessLib::Process process(callStr, "",
[output, &video](const char *bytes, size_t n) {
std::string part = std::string(bytes, n);
video.append(part);
},
[output](const char *bytes, size_t n) {
*output += std::string(bytes, n);
},
true
);
process.write(transparent_movie);
int exit = process.get_exit_status();
if (exit == 0) {
DEBUG("ffmpeg:\n {}", *output);
transparentVideos.erase(name);
transparentVideos[name] = video;
return true;
} else {
ERROR("Call of ffmpeg failed. Tried to create an empty video with length {} seconds and name {}", seconds, name);
ERROR("Output:\n{}", *output);
return false;
}
}
std::string FFmpegHandler::getAudioInfo() {
json result;
// add audio streams
int audioIndex = 0;
for (auto& v : bstreams) {
if (v["codec_type"] == "audio") {
std::string language;
std::string comment;
if (v.contains("tags")) {
language = v["tags"]["language"];
comment = v["tags"]["comment"];
json lang;
lang["index"] = audioIndex;
lang["language"] = language;
lang["comment"] = comment;
result["stream"].push_back(lang);
}
}
audioIndex++;
}
return result.dump();
}