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