OpenShot Library | libopenshot  0.2.7
ChunkReader.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for ChunkReader class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "ChunkReader.h"
14 #include "Exceptions.h"
15 #include "FFmpegReader.h"
16 
17 #include <QDir>
18 
19 using namespace openshot;
20 
21 ChunkReader::ChunkReader(std::string path, ChunkVersion chunk_version)
22  : path(path), chunk_size(24 * 3), is_open(false), version(chunk_version), local_reader(NULL)
23 {
24  // Check if folder exists?
25  if (!does_folder_exist(path))
26  // Raise exception
27  throw InvalidFile("Chunk folder could not be opened.", path);
28 
29  // Init previous location
30  previous_location.number = 0;
31  previous_location.frame = 0;
32 
33  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
34  Open();
35  Close();
36 }
37 
38 // Check if folder path existing
39 bool ChunkReader::does_folder_exist(std::string path)
40 {
41  QDir dir(path.c_str());
42  return dir.exists();
43 }
44 
45 // Load JSON meta data about this chunk folder
46 void ChunkReader::load_json()
47 {
48  // Load path of chunk folder
49  std::string json_path = QDir::cleanPath(QString(path.c_str()) + QDir::separator() + "info.json").toStdString();
50  std::stringstream json_string;
51 
52  // Read the JSON file
53  std::ifstream myfile (json_path.c_str());
54  std::string line = "";
55  if (myfile.is_open())
56  {
57  while (myfile.good())
58  {
59  getline (myfile, line);
60  json_string << line;
61  }
62  myfile.close();
63  }
64 
65  // Parse JSON string into JSON objects
66  Json::Value root;
67  Json::CharReaderBuilder rbuilder;
68 
69  std::string errors;
70  bool success = Json::parseFromStream(rbuilder, json_string, &root, &errors);
71  if (!success)
72  // Raise exception
73  throw InvalidJSON("Chunk folder could not be opened.", path);
74 
75 
76  // Set info from the JSON objects
77  try
78  {
79  info.has_video = root["has_video"].asBool();
80  info.has_audio = root["has_audio"].asBool();
81  info.duration = root["duration"].asDouble();
82  info.file_size = std::stoll(root["file_size"].asString());
83  info.height = root["height"].asInt();
84  info.width = root["width"].asInt();
85  info.pixel_format = root["pixel_format"].asInt();
86  info.fps.num = root["fps"]["num"].asInt();
87  info.fps.den = root["fps"]["den"].asInt();
88  info.video_bit_rate = root["video_bit_rate"].asUInt();
89  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
90  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
91  info.display_ratio.num = root["display_ratio"]["num"].asInt();
92  info.display_ratio.den = root["display_ratio"]["den"].asInt();
93  info.vcodec = root["vcodec"].asString();
94  info.video_length = std::stoll(root["video_length"].asString());
95  info.video_stream_index = root["video_stream_index"].asInt();
96  info.video_timebase.num = root["video_timebase"]["num"].asInt();
97  info.video_timebase.den = root["video_timebase"]["den"].asInt();
98  info.interlaced_frame = root["interlaced_frame"].asBool();
99  info.top_field_first = root["top_field_first"].asBool();
100  info.acodec = root["acodec"].asString();
101  info.audio_bit_rate = root["audio_bit_rate"].asUInt();
102  info.sample_rate = root["sample_rate"].asUInt();
103  info.channels = root["channels"].asInt();
104  info.audio_stream_index = root["audio_stream_index"].asInt();
105  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
106  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
107 
108  }
109  catch (const std::exception& e)
110  {
111  // Error parsing JSON (or missing keys)
112  throw InvalidJSON("JSON could not be parsed (or is invalid).", path);
113  }
114 }
115 
116 // Find the location of a frame in a chunk
117 ChunkLocation ChunkReader::find_chunk_frame(int64_t requested_frame)
118 {
119  // Determine which chunk contains this frame.
120  int64_t chunk_number = (requested_frame / chunk_size) + 1;
121 
122  // Determine which frame in this chunk
123  int64_t start_frame_of_chunk = (chunk_number - 1) * chunk_size;
124  int64_t chunk_frame_number = (requested_frame - start_frame_of_chunk) + 1; // Add 1 to adjust for the 1st frame of every chunk is just there to "stoke" the audio samples from the previous chunk.
125 
126  // Prepare chunk location struct
127  ChunkLocation location = {chunk_number, chunk_frame_number};
128 
129  return location;
130 }
131 
132 // Open chunk folder or file
134 {
135  // Open reader if not already open
136  if (!is_open)
137  {
138  // parse JSON and load info.json file
139  load_json();
140 
141  // Mark as "open"
142  is_open = true;
143  }
144 }
145 
146 // Close image file
148 {
149  // Close all objects, if reader is 'open'
150  if (is_open)
151  {
152  // Mark as "closed"
153  is_open = false;
154  }
155 }
156 
157 // get a formatted path of a specific chunk
158 std::string ChunkReader::get_chunk_path(int64_t chunk_number, std::string folder, std::string extension)
159 {
160  // Create path of new chunk video
161  std::stringstream chunk_count_string;
162  chunk_count_string << chunk_number;
163  QString padded_count = "%1"; //chunk_count_string.str().c_str();
164  padded_count = padded_count.arg(chunk_count_string.str().c_str(), 6, '0');
165  if (folder.length() != 0 && extension.length() != 0)
166  // Return path with FOLDER and EXTENSION name
167  return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str() + QDir::separator() + padded_count + extension.c_str()).toStdString();
168 
169  else if (folder.length() == 0 && extension.length() != 0)
170  // Return path with NO FOLDER and EXTENSION name
171  return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + padded_count + extension.c_str()).toStdString();
172 
173  else if (folder.length() != 0 && extension.length() == 0)
174  // Return path with FOLDER and NO EXTENSION
175  return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str()).toStdString();
176  else
177  return "";
178 }
179 
180 // Get an openshot::Frame object for a specific frame number of this reader.
181 std::shared_ptr<Frame> ChunkReader::GetFrame(int64_t requested_frame)
182 {
183  // Determine what chunk contains this frame
184  ChunkLocation location = find_chunk_frame(requested_frame);
185 
186  // New Chunk (Close the old reader, and open the new one)
187  if (previous_location.number != location.number)
188  {
189  // Determine version of chunk
190  std::string folder_name = "";
191  switch (version)
192  {
193  case THUMBNAIL:
194  folder_name = "thumb";
195  break;
196  case PREVIEW:
197  folder_name = "preview";
198  break;
199  case FINAL:
200  folder_name = "final";
201  break;
202  }
203 
204  // Load path of chunk video
205  std::string chunk_video_path = get_chunk_path(location.number, folder_name, ".webm");
206 
207  // Close existing reader (if needed)
208  if (local_reader)
209  {
210  // Close and delete old reader
211  local_reader->Close();
212  delete local_reader;
213  }
214 
215  try
216  {
217  // Load new FFmpegReader
218  local_reader = new FFmpegReader(chunk_video_path);
219  local_reader->Open(); // open reader
220 
221  } catch (const InvalidFile& e)
222  {
223  // Invalid Chunk (possibly it is not found)
224  throw ChunkNotFound(path, requested_frame, location.number, location.frame);
225  }
226 
227  // Set the new location
228  previous_location = location;
229  }
230 
231  // Get the frame (from the current reader)
232  last_frame = local_reader->GetFrame(location.frame);
233 
234  // Update the frame number property
235  last_frame->number = requested_frame;
236 
237  // Return the frame
238  return last_frame;
239 }
240 
241 // Generate JSON string of this object
242 std::string ChunkReader::Json() const {
243 
244  // Return formatted string
245  return JsonValue().toStyledString();
246 }
247 
248 // Generate Json::Value for this object
249 Json::Value ChunkReader::JsonValue() const {
250 
251  // Create root json object
252  Json::Value root = ReaderBase::JsonValue(); // get parent properties
253  root["type"] = "ChunkReader";
254  root["path"] = path;
255  std::stringstream chunk_size_stream;
256  chunk_size_stream << chunk_size;
257  root["chunk_size"] = chunk_size_stream.str();
258  root["chunk_version"] = version;
259 
260  // return JsonValue
261  return root;
262 }
263 
264 // Load JSON string into this object
265 void ChunkReader::SetJson(const std::string value) {
266 
267  try
268  {
269  const Json::Value root = openshot::stringToJson(value);
270  // Set all values that match
271  SetJsonValue(root);
272  }
273  catch (const std::exception& e)
274  {
275  // Error parsing JSON (or missing keys)
276  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
277  }
278 }
279 
280 // Load Json::Value into this object
281 void ChunkReader::SetJsonValue(const Json::Value root) {
282 
283  // Set parent data
285 
286  // Set data from Json (if key is found)
287  if (!root["path"].isNull())
288  path = root["path"].asString();
289  if (!root["chunk_size"].isNull())
290  chunk_size = std::stoll(root["chunk_size"].asString());
291  if (!root["chunk_version"].isNull())
292  version = (ChunkVersion) root["chunk_version"].asInt();
293 
294  // Re-Open path, and re-init everything (if needed)
295  if (is_open)
296  {
297  Close();
298  Open();
299  }
300 }
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Get an openshot::Frame object for a specific frame number of this reader.
std::string Json() const override
Generate JSON string of this object.
int num
Numerator for the fraction.
Definition: Fraction.h:32
Exception when a required chunk is missing.
Definition: Exceptions.h:40
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:48
ChunkReader(std::string path, ChunkVersion chunk_version)
Constructor for ChunkReader. This automatically opens the chunk file or folder and loads frame 1...
Definition: ChunkReader.cpp:21
float duration
Length of time (in seconds)
Definition: ReaderBase.h:45
The lowest quality stream contained in this chunk file.
Definition: ChunkReader.h:52
Header file for FFmpegReader class.
void SetJson(const std::string value) override
Load JSON string into this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
virtual void Close()=0
Close the reader (and any resources it was consuming)
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:42
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:46
void Close() override
Close the reader.
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
int audio_bit_rate
The bit rate of the audio stream (in bytes)
Definition: ReaderBase.h:61
Header file for all Exception classes.
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:43
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:104
Json::Value JsonValue() const override
Generate Json::Value for this object.
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:75
The highest quality stream contained in this chunk file.
Definition: ChunkReader.h:54
int audio_stream_index
The index of the audio stream.
Definition: ReaderBase.h:65
The medium quality stream contained in this chunk file.
Definition: ChunkReader.h:53
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:55
int height
The height of the video (in pixels)
Definition: ReaderBase.h:47
Header file for ChunkReader class.
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:57
Exception for files that can not be found or opened.
Definition: Exceptions.h:155
void Open() override
Open the reader. This is required before you can access frames or data from the reader.
int64_t number
The chunk number.
Definition: ChunkReader.h:36
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low...
Definition: ChunkReader.h:50
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:159
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:91
openshot::Fraction audio_timebase
The audio timebase determines how long each audio packet should be played.
Definition: ReaderBase.h:66
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:54
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
Exception for invalid JSON.
Definition: Exceptions.h:187
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: ReaderBase.h:49
openshot::Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3) ...
Definition: ReaderBase.h:53
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: ReaderBase.h:51
int64_t frame
The frame number.
Definition: ChunkReader.h:37
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:52
int den
Denominator for the fraction.
Definition: Fraction.h:33
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:63
This struct holds the location of a frame within a chunk.
Definition: ChunkReader.h:34
int video_stream_index
The index of the video stream.
Definition: ReaderBase.h:56
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:50
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:60
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:62