25 : path(path), chunk_size(24 * 3), is_open(false), version(chunk_version), local_reader(NULL)
28 if (!does_folder_exist(path))
30 throw InvalidFile(
"Chunk folder could not be opened.", path);
33 previous_location.number = 0;
34 previous_location.frame = 0;
42bool ChunkReader::does_folder_exist(std::string
path)
44 QDir dir(
path.c_str());
49void ChunkReader::load_json()
52 std::string json_path = QDir::cleanPath(QString(
path.c_str()) + QDir::separator() +
"info.json").toStdString();
53 std::stringstream json_string;
56 std::ifstream myfile (json_path.c_str());
57 std::string line =
"";
62 getline (myfile, line);
70 Json::CharReaderBuilder rbuilder;
73 bool success = Json::parseFromStream(rbuilder, json_string, &root, &errors);
76 throw InvalidJSON(
"Chunk folder could not be opened.", path);
82 info.has_video = root[
"has_video"].asBool();
83 info.has_audio = root[
"has_audio"].asBool();
84 info.duration = root[
"duration"].asDouble();
85 info.file_size = std::stoll(root[
"file_size"].asString());
86 info.height = root[
"height"].asInt();
87 info.width = root[
"width"].asInt();
88 info.pixel_format = root[
"pixel_format"].asInt();
89 info.fps.num = root[
"fps"][
"num"].asInt();
90 info.fps.den = root[
"fps"][
"den"].asInt();
91 info.video_bit_rate = root[
"video_bit_rate"].asUInt();
92 info.pixel_ratio.num = root[
"pixel_ratio"][
"num"].asInt();
93 info.pixel_ratio.den = root[
"pixel_ratio"][
"den"].asInt();
94 info.display_ratio.num = root[
"display_ratio"][
"num"].asInt();
95 info.display_ratio.den = root[
"display_ratio"][
"den"].asInt();
96 info.vcodec = root[
"vcodec"].asString();
97 info.video_length = std::stoll(root[
"video_length"].asString());
98 info.video_stream_index = root[
"video_stream_index"].asInt();
99 info.video_timebase.num = root[
"video_timebase"][
"num"].asInt();
100 info.video_timebase.den = root[
"video_timebase"][
"den"].asInt();
101 info.interlaced_frame = root[
"interlaced_frame"].asBool();
102 info.top_field_first = root[
"top_field_first"].asBool();
103 info.acodec = root[
"acodec"].asString();
104 info.audio_bit_rate = root[
"audio_bit_rate"].asUInt();
105 info.sample_rate = root[
"sample_rate"].asUInt();
106 info.channels = root[
"channels"].asInt();
107 info.audio_stream_index = root[
"audio_stream_index"].asInt();
108 info.audio_timebase.num = root[
"audio_timebase"][
"num"].asInt();
109 info.audio_timebase.den = root[
"audio_timebase"][
"den"].asInt();
112 catch (
const std::exception& e)
115 throw InvalidJSON(
"JSON could not be parsed (or is invalid).", path);
120ChunkLocation ChunkReader::find_chunk_frame(int64_t requested_frame)
123 int64_t chunk_number = (requested_frame / chunk_size) + 1;
126 int64_t start_frame_of_chunk = (chunk_number - 1) * chunk_size;
127 int64_t chunk_frame_number = (requested_frame - start_frame_of_chunk) + 1;
130 ChunkLocation location = {chunk_number, chunk_frame_number};
161std::string ChunkReader::get_chunk_path(int64_t chunk_number, std::string folder, std::string extension)
164 std::stringstream chunk_count_string;
165 chunk_count_string << chunk_number;
166 QString padded_count =
"%1";
167 padded_count = padded_count.arg(chunk_count_string.str().c_str(), 6,
'0');
168 if (folder.length() != 0 && extension.length() != 0)
170 return QDir::cleanPath(QString(
path.c_str()) + QDir::separator() + folder.c_str() + QDir::separator() + padded_count + extension.c_str()).toStdString();
172 else if (folder.length() == 0 && extension.length() != 0)
174 return QDir::cleanPath(QString(
path.c_str()) + QDir::separator() + padded_count + extension.c_str()).toStdString();
176 else if (folder.length() != 0 && extension.length() == 0)
178 return QDir::cleanPath(QString(
path.c_str()) + QDir::separator() + folder.c_str()).toStdString();
190 if (previous_location.number != location.
number)
193 std::string folder_name =
"";
197 folder_name =
"thumb";
200 folder_name =
"preview";
203 folder_name =
"final";
208 std::string chunk_video_path = get_chunk_path(location.
number, folder_name,
".webm");
214 local_reader->Close();
222 local_reader->Open();
231 previous_location = location;
235 last_frame = local_reader->GetFrame(location.
frame);
238 last_frame->number = requested_frame;
256 root[
"type"] =
"ChunkReader";
258 std::stringstream chunk_size_stream;
259 chunk_size_stream << chunk_size;
260 root[
"chunk_size"] = chunk_size_stream.str();
261 root[
"chunk_version"] = version;
276 catch (
const std::exception& e)
279 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
290 if (!root[
"path"].isNull())
291 path = root[
"path"].asString();
292 if (!root[
"chunk_size"].isNull())
293 chunk_size = std::stoll(root[
"chunk_size"].asString());
294 if (!root[
"chunk_version"].isNull())
Header file for ChunkReader class.
Header file for all Exception classes.
Header file for FFmpegReader class.
Exception when a required chunk is missing.
std::string Json() const override
Generate JSON string of this object.
void Close() override
Close the reader.
void Open() override
Open the reader. This is required before you can access frames or data from the reader.
Json::Value JsonValue() const override
Generate Json::Value for this object.
ChunkReader(std::string path, ChunkVersion chunk_version)
Constructor for ChunkReader. This automatically opens the chunk file or folder and loads frame 1,...
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Get an openshot::Frame object for a specific frame number of this reader.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
void SetJson(const std::string value) override
Load JSON string into this object.
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Exception for files that can not be found or opened.
Exception for invalid JSON.
openshot::ReaderInfo info
Information about the current media file.
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
This namespace is the default namespace for all code in the openshot library.
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low,...
@ THUMBNAIL
The lowest quality stream contained in this chunk file.
@ FINAL
The highest quality stream contained in this chunk file.
@ PREVIEW
The medium quality stream contained in this chunk file.
const Json::Value stringToJson(const std::string value)
This struct holds the location of a frame within a chunk.
int64_t number
The chunk number.
int64_t frame
The frame number.