OpenShot Library | libopenshot  0.2.7
ReaderBase.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for ReaderBase class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "ReaderBase.h"
32 
33 using namespace openshot;
34 
35 /// Constructor for the base reader, where many things are initialized.
37 {
38  // Initialize info struct
39  info.has_video = false;
40  info.has_audio = false;
41  info.has_single_image = false;
42  info.duration = 0.0;
43  info.file_size = 0;
44  info.height = 0;
45  info.width = 0;
46  info.pixel_format = -1;
47  info.fps = Fraction();
48  info.video_bit_rate = 0;
51  info.vcodec = "";
52  info.video_length = 0;
55  info.interlaced_frame = false;
56  info.top_field_first = true;
57  info.acodec = "";
58  info.audio_bit_rate = 0;
59  info.sample_rate = 0;
60  info.channels = 0;
64 
65  // Init parent clip
66  clip = NULL;
67 }
68 
69 // Display file information
71  std::cout << std::fixed << std::setprecision(2) << std::boolalpha;
72  std::cout << "----------------------------" << std::endl;
73  std::cout << "----- File Information -----" << std::endl;
74  std::cout << "----------------------------" << std::endl;
75  std::cout << "--> Has Video: " << info.has_video << std::endl;
76  std::cout << "--> Has Audio: " << info.has_audio << std::endl;
77  std::cout << "--> Has Single Image: " << info.has_single_image << std::endl;
78  std::cout << "--> Duration: " << info.duration << " Seconds" << std::endl;
79  std::cout << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << std::endl;
80  std::cout << "----------------------------" << std::endl;
81  std::cout << "----- Video Attributes -----" << std::endl;
82  std::cout << "----------------------------" << std::endl;
83  std::cout << "--> Width: " << info.width << std::endl;
84  std::cout << "--> Height: " << info.height << std::endl;
85  std::cout << "--> Pixel Format: " << info.pixel_format << std::endl;
86  std::cout << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << std::endl;
87  std::cout << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << std::endl;
88  std::cout << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << std::endl;
89  std::cout << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << std::endl;
90  std::cout << "--> Video Codec: " << info.vcodec << std::endl;
91  std::cout << "--> Video Length: " << info.video_length << " Frames" << std::endl;
92  std::cout << "--> Video Stream Index: " << info.video_stream_index << std::endl;
93  std::cout << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << std::endl;
94  std::cout << "--> Interlaced: " << info.interlaced_frame << std::endl;
95  std::cout << "--> Interlaced: Top Field First: " << info.top_field_first << std::endl;
96  std::cout << "----------------------------" << std::endl;
97  std::cout << "----- Audio Attributes -----" << std::endl;
98  std::cout << "----------------------------" << std::endl;
99  std::cout << "--> Audio Codec: " << info.acodec << std::endl;
100  std::cout << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << std::endl;
101  std::cout << "--> Sample Rate: " << info.sample_rate << " Hz" << std::endl;
102  std::cout << "--> # of Channels: " << info.channels << std::endl;
103  std::cout << "--> Channel Layout: " << info.channel_layout << std::endl;
104  std::cout << "--> Audio Stream Index: " << info.audio_stream_index << std::endl;
105  std::cout << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << std::endl;
106  std::cout << "----------------------------" << std::endl;
107  std::cout << "--------- Metadata ---------" << std::endl;
108  std::cout << "----------------------------" << std::endl;
109 
110  // Iterate through metadata
111  for (auto it : info.metadata)
112  std::cout << "--> " << it.first << ": " << it.second << std::endl;
113 }
114 
115 // Generate Json::Value for this object
116 Json::Value ReaderBase::JsonValue() const {
117 
118  // Create root json object
119  Json::Value root;
120  root["has_video"] = info.has_video;
121  root["has_audio"] = info.has_audio;
122  root["has_single_image"] = info.has_single_image;
123  root["duration"] = info.duration;
124  std::stringstream filesize_stream;
125  filesize_stream << info.file_size;
126  root["file_size"] = filesize_stream.str();
127  root["height"] = info.height;
128  root["width"] = info.width;
129  root["pixel_format"] = info.pixel_format;
130  root["fps"] = Json::Value(Json::objectValue);
131  root["fps"]["num"] = info.fps.num;
132  root["fps"]["den"] = info.fps.den;
133  root["video_bit_rate"] = info.video_bit_rate;
134  root["pixel_ratio"] = Json::Value(Json::objectValue);
135  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
136  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
137  root["display_ratio"] = Json::Value(Json::objectValue);
138  root["display_ratio"]["num"] = info.display_ratio.num;
139  root["display_ratio"]["den"] = info.display_ratio.den;
140  root["vcodec"] = info.vcodec;
141  std::stringstream video_length_stream;
142  video_length_stream << info.video_length;
143  root["video_length"] = video_length_stream.str();
144  root["video_stream_index"] = info.video_stream_index;
145  root["video_timebase"] = Json::Value(Json::objectValue);
146  root["video_timebase"]["num"] = info.video_timebase.num;
147  root["video_timebase"]["den"] = info.video_timebase.den;
148  root["interlaced_frame"] = info.interlaced_frame;
149  root["top_field_first"] = info.top_field_first;
150  root["acodec"] = info.acodec;
151  root["audio_bit_rate"] = info.audio_bit_rate;
152  root["sample_rate"] = info.sample_rate;
153  root["channels"] = info.channels;
154  root["channel_layout"] = info.channel_layout;
155  root["audio_stream_index"] = info.audio_stream_index;
156  root["audio_timebase"] = Json::Value(Json::objectValue);
157  root["audio_timebase"]["num"] = info.audio_timebase.num;
158  root["audio_timebase"]["den"] = info.audio_timebase.den;
159 
160  // Append metadata map
161  root["metadata"] = Json::Value(Json::objectValue);
162 
163  for (const auto it : info.metadata)
164  root["metadata"][it.first] = it.second;
165 
166  // return JsonValue
167  return root;
168 }
169 
170 // Load Json::Value into this object
171 void ReaderBase::SetJsonValue(const Json::Value root) {
172 
173  // Set data from Json (if key is found)
174  if (!root["has_video"].isNull())
175  info.has_video = root["has_video"].asBool();
176  if (!root["has_audio"].isNull())
177  info.has_audio = root["has_audio"].asBool();
178  if (!root["has_single_image"].isNull())
179  info.has_single_image = root["has_single_image"].asBool();
180  if (!root["duration"].isNull())
181  info.duration = root["duration"].asDouble();
182  if (!root["file_size"].isNull())
183  info.file_size = std::stoll(root["file_size"].asString());
184  if (!root["height"].isNull())
185  info.height = root["height"].asInt();
186  if (!root["width"].isNull())
187  info.width = root["width"].asInt();
188  if (!root["pixel_format"].isNull())
189  info.pixel_format = root["pixel_format"].asInt();
190  if (!root["fps"].isNull() && root["fps"].isObject()) {
191  if (!root["fps"]["num"].isNull())
192  info.fps.num = root["fps"]["num"].asInt();
193  if (!root["fps"]["den"].isNull())
194  info.fps.den = root["fps"]["den"].asInt();
195  }
196  if (!root["video_bit_rate"].isNull())
197  info.video_bit_rate = root["video_bit_rate"].asInt();
198  if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
199  if (!root["pixel_ratio"]["num"].isNull())
200  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
201  if (!root["pixel_ratio"]["den"].isNull())
202  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
203  }
204  if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
205  if (!root["display_ratio"]["num"].isNull())
206  info.display_ratio.num = root["display_ratio"]["num"].asInt();
207  if (!root["display_ratio"]["den"].isNull())
208  info.display_ratio.den = root["display_ratio"]["den"].asInt();
209  }
210  if (!root["vcodec"].isNull())
211  info.vcodec = root["vcodec"].asString();
212  if (!root["video_length"].isNull())
213  info.video_length = std::stoll(root["video_length"].asString());
214  if (!root["video_stream_index"].isNull())
215  info.video_stream_index = root["video_stream_index"].asInt();
216  if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
217  if (!root["video_timebase"]["num"].isNull())
218  info.video_timebase.num = root["video_timebase"]["num"].asInt();
219  if (!root["video_timebase"]["den"].isNull())
220  info.video_timebase.den = root["video_timebase"]["den"].asInt();
221  }
222  if (!root["interlaced_frame"].isNull())
223  info.interlaced_frame = root["interlaced_frame"].asBool();
224  if (!root["top_field_first"].isNull())
225  info.top_field_first = root["top_field_first"].asBool();
226  if (!root["acodec"].isNull())
227  info.acodec = root["acodec"].asString();
228 
229  if (!root["audio_bit_rate"].isNull())
230  info.audio_bit_rate = root["audio_bit_rate"].asInt();
231  if (!root["sample_rate"].isNull())
232  info.sample_rate = root["sample_rate"].asInt();
233  if (!root["channels"].isNull())
234  info.channels = root["channels"].asInt();
235  if (!root["channel_layout"].isNull())
236  info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
237  if (!root["audio_stream_index"].isNull())
238  info.audio_stream_index = root["audio_stream_index"].asInt();
239  if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
240  if (!root["audio_timebase"]["num"].isNull())
241  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
242  if (!root["audio_timebase"]["den"].isNull())
243  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
244  }
245  if (!root["metadata"].isNull() && root["metadata"].isObject()) {
246  for( Json::Value::const_iterator itr = root["metadata"].begin() ; itr != root["metadata"].end() ; itr++ ) {
247  std::string key = itr.key().asString();
248  info.metadata[key] = root["metadata"][key].asString();
249  }
250  }
251 }
252 
253 /// Parent clip object of this reader (which can be unparented and NULL)
255  return clip;
256 }
257 
258 /// Set parent clip object of this reader
260  clip = new_clip;
261 }
int num
Numerator for the fraction.
Definition: Fraction.h:50
Header file for ReaderBase class.
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
ReaderBase()
Constructor for the base reader, where many things are initialized.
Definition: ReaderBase.cpp:36
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:59
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:62
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66
int audio_bit_rate
The bit rate of the audio stream (in bytes)
Definition: ReaderBase.h:81
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:116
int audio_stream_index
The index of the audio stream.
Definition: ReaderBase.h:85
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
This class represents a fraction.
Definition: Fraction.h:48
std::map< std::string, std::string > metadata
An optional map/dictionary of metadata for this reader.
Definition: ReaderBase.h:87
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:84
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:64
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround...
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:51
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:171
openshot::ClipBase * ParentClip()
Parent clip object of this reader (which can be unparented and NULL)
Definition: ReaderBase.cpp:254
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
openshot::Fraction audio_timebase
The audio timebase determines how long each audio packet should be played.
Definition: ReaderBase.h:86
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
openshot::ClipBase * clip
Pointer to the parent clip instance (if any)
Definition: ReaderBase.h:103
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: ReaderBase.h:69
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:73
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: ReaderBase.h:71
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:72
void DisplayInfo()
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:70
int den
Denominator for the fraction.
Definition: Fraction.h:51
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:83
int video_stream_index
The index of the video stream.
Definition: ReaderBase.h:76
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:80
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:82