FFmpegKit Linux API 6.0
Loading...
Searching...
No Matches
MediaInformationJsonParser.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Taner Sener
3 *
4 * This file is part of FFmpegKit.
5 *
6 * FFmpegKit is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpegKit is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpegKit. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21// OVERRIDING THE MACRO TO PREVENT APPLICATION TERMINATION
22#define RAPIDJSON_ASSERT(x)
23#include "rapidjson/reader.h"
24#include "rapidjson/document.h"
25#include "rapidjson/error/en.h"
26#include <memory>
27
28static const char* MediaInformationJsonParserKeyStreams = "streams";
29static const char* MediaInformationJsonParserKeyChapters = "chapters";
30
31std::shared_ptr<ffmpegkit::MediaInformation> ffmpegkit::MediaInformationJsonParser::from(const std::string& ffprobeJsonOutput) {
32 try {
33 return fromWithError(ffprobeJsonOutput);
34 } catch(const std::exception& exception) {
35 std::cout << "MediaInformation parsing failed: " << exception.what() << std::endl;
36 return nullptr;
37 }
38}
39
40std::shared_ptr<ffmpegkit::MediaInformation> ffmpegkit::MediaInformationJsonParser::fromWithError(const std::string& ffprobeJsonOutput) {
41 std::shared_ptr<rapidjson::Document> document = std::make_shared<rapidjson::Document>();
42
43 document->Parse(ffprobeJsonOutput.c_str());
44
45 if (document->HasParseError()) {
46 throw std::runtime_error(GetParseError_En(document->GetParseError()));
47 } else {
48 std::shared_ptr<std::vector<std::shared_ptr<ffmpegkit::StreamInformation>>> streams = std::make_shared<std::vector<std::shared_ptr<ffmpegkit::StreamInformation>>>();
49 std::shared_ptr<std::vector<std::shared_ptr<ffmpegkit::Chapter>>> chapters = std::make_shared<std::vector<std::shared_ptr<ffmpegkit::Chapter>>>();
50
51 if (document->HasMember(MediaInformationJsonParserKeyStreams)) {
52 rapidjson::Value& streamArray = (*document.get())[MediaInformationJsonParserKeyStreams];
53 if (streamArray.IsArray()) {
54 for (rapidjson::SizeType i = 0; i < streamArray.Size(); i++) {
55 auto stream = std::make_shared<rapidjson::Value>();
56 *stream = streamArray[i];
57 streams->push_back(std::make_shared<ffmpegkit::StreamInformation>(stream));
58 }
59 }
60 }
61
62 if (document->HasMember(MediaInformationJsonParserKeyChapters)) {
63 rapidjson::Value& chapterArray = (*document.get())[MediaInformationJsonParserKeyChapters];
64 if (chapterArray.IsArray()) {
65 for (rapidjson::SizeType i = 0; i < chapterArray.Size(); i++) {
66 auto chapter = std::make_shared<rapidjson::Value>();
67 *chapter = chapterArray[i];
68 chapters->push_back(std::make_shared<ffmpegkit::Chapter>(chapter));
69 }
70 }
71 }
72
73 return std::make_shared<ffmpegkit::MediaInformation>(std::static_pointer_cast<rapidjson::Value>(document), streams, chapters);
74 }
75}
static const char * MediaInformationJsonParserKeyStreams
static const char * MediaInformationJsonParserKeyChapters
static std::shared_ptr< ffmpegkit::MediaInformation > from(const std::string &ffprobeJsonOutput)
static std::shared_ptr< ffmpegkit::MediaInformation > fromWithError(const std::string &ffprobeJsonOutput)