/*
* Copyright (c) 2022 Taner Sener
*
* This file is part of FFmpegKit.
*
* FFmpegKit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FFmpegKit 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FFmpegKit. If not, see .
*/
#include "MediaInformationJsonParser.h"
// OVERRIDING THE MACRO TO PREVENT APPLICATION TERMINATION
#define RAPIDJSON_ASSERT(x)
#include "rapidjson/reader.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include
static const char* MediaInformationJsonParserKeyStreams = "streams";
static const char* MediaInformationJsonParserKeyChapters = "chapters";
std::shared_ptr ffmpegkit::MediaInformationJsonParser::from(const std::string& ffprobeJsonOutput) {
std::string error;
std::shared_ptr mediaInformation = fromWithError(ffprobeJsonOutput, error);
if (mediaInformation == nullptr) {
std::cout << "MediaInformation parsing failed: " << error << std::endl;
}
return mediaInformation;
}
std::shared_ptr ffmpegkit::MediaInformationJsonParser::fromWithError(const std::string& ffprobeJsonOutput, std::string& error) {
std::shared_ptr document = std::make_shared();
document->Parse(ffprobeJsonOutput.c_str());
if (document->HasParseError()) {
error = GetParseError_En(document->GetParseError());
return nullptr;
} else {
std::shared_ptr>> streams = std::make_shared>>();
std::shared_ptr>> chapters = std::make_shared>>();
if (document->HasMember(MediaInformationJsonParserKeyStreams)) {
rapidjson::Value& streamArray = (*document.get())[MediaInformationJsonParserKeyStreams];
if (streamArray.IsArray()) {
for (rapidjson::SizeType i = 0; i < streamArray.Size(); i++) {
auto stream = std::make_shared();
*stream = streamArray[i];
streams->push_back(std::make_shared(stream));
}
}
}
if (document->HasMember(MediaInformationJsonParserKeyChapters)) {
rapidjson::Value& chapterArray = (*document.get())[MediaInformationJsonParserKeyChapters];
if (chapterArray.IsArray()) {
for (rapidjson::SizeType i = 0; i < chapterArray.Size(); i++) {
auto chapter = std::make_shared();
*chapter = chapterArray[i];
chapters->push_back(std::make_shared(chapter));
}
}
}
return std::make_shared(std::static_pointer_cast(document), streams, chapters);
}
}