OpenShot Library | libopenshot  0.1.9
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 }
104 
105 // Generate Json::JsonValue for this object
106 Json::Value ReaderBase::JsonValue() {
107 
108  // Create root json object
109  Json::Value root;
110  root["has_video"] = info.has_video;
111  root["has_audio"] = info.has_audio;
112  root["has_single_image"] = info.has_single_image;
113  root["duration"] = info.duration;
114  stringstream filesize_stream;
115  filesize_stream << info.file_size;
116  root["file_size"] = filesize_stream.str();
117  root["height"] = info.height;
118  root["width"] = info.width;
119  root["pixel_format"] = info.pixel_format;
120  root["fps"] = Json::Value(Json::objectValue);
121  root["fps"]["num"] = info.fps.num;
122  root["fps"]["den"] = info.fps.den;
123  root["video_bit_rate"] = info.video_bit_rate;
124  root["pixel_ratio"] = Json::Value(Json::objectValue);
125  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
126  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
127  root["display_ratio"] = Json::Value(Json::objectValue);
128  root["display_ratio"]["num"] = info.display_ratio.num;
129  root["display_ratio"]["den"] = info.display_ratio.den;
130  root["vcodec"] = info.vcodec;
131  stringstream video_length_stream;
132  video_length_stream << info.video_length;
133  root["video_length"] = video_length_stream.str();
134  root["video_stream_index"] = info.video_stream_index;
135  root["video_timebase"] = Json::Value(Json::objectValue);
136  root["video_timebase"]["num"] = info.video_timebase.num;
137  root["video_timebase"]["den"] = info.video_timebase.den;
138  root["interlaced_frame"] = info.interlaced_frame;
139  root["top_field_first"] = info.top_field_first;
140  root["acodec"] = info.acodec;
141  root["audio_bit_rate"] = info.audio_bit_rate;
142  root["sample_rate"] = info.sample_rate;
143  root["channels"] = info.channels;
144  root["channel_layout"] = info.channel_layout;
145  root["audio_stream_index"] = info.audio_stream_index;
146  root["audio_timebase"] = Json::Value(Json::objectValue);
147  root["audio_timebase"]["num"] = info.audio_timebase.num;
148  root["audio_timebase"]["den"] = info.audio_timebase.den;
149 
150  // return JsonValue
151  return root;
152 }
153 
154 // Load Json::JsonValue into this object
155 void ReaderBase::SetJsonValue(Json::Value root) {
156 
157  // Set data from Json (if key is found)
158  if (!root["has_video"].isNull())
159  info.has_video = root["has_video"].asBool();
160  if (!root["has_audio"].isNull())
161  info.has_audio = root["has_audio"].asBool();
162  if (!root["has_single_image"].isNull())
163  info.has_single_image = root["has_single_image"].asBool();
164  if (!root["duration"].isNull())
165  info.duration = root["duration"].asDouble();
166  if (!root["file_size"].isNull())
167  info.file_size = atoll(root["file_size"].asString().c_str());
168  if (!root["height"].isNull())
169  info.height = root["height"].asInt();
170  if (!root["width"].isNull())
171  info.width = root["width"].asInt();
172  if (!root["pixel_format"].isNull())
173  info.pixel_format = root["pixel_format"].asInt();
174  if (!root["fps"].isNull() && root["fps"].isObject()) {
175  if (!root["fps"]["num"].isNull())
176  info.fps.num = root["fps"]["num"].asInt();
177  if (!root["fps"]["den"].isNull())
178  info.fps.den = root["fps"]["den"].asInt();
179  }
180  if (!root["video_bit_rate"].isNull())
181  info.video_bit_rate = root["video_bit_rate"].asInt();
182  if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
183  if (!root["pixel_ratio"]["num"].isNull())
184  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
185  if (!root["pixel_ratio"]["den"].isNull())
186  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
187  }
188  if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
189  if (!root["display_ratio"]["num"].isNull())
190  info.display_ratio.num = root["display_ratio"]["num"].asInt();
191  if (!root["display_ratio"]["den"].isNull())
192  info.display_ratio.den = root["display_ratio"]["den"].asInt();
193  }
194  if (!root["vcodec"].isNull())
195  info.vcodec = root["vcodec"].asString();
196  if (!root["video_length"].isNull())
197  info.video_length = atoll(root["video_length"].asString().c_str());
198  if (!root["video_stream_index"].isNull())
199  info.video_stream_index = root["video_stream_index"].asInt();
200  if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
201  if (!root["video_timebase"]["num"].isNull())
202  info.video_timebase.num = root["video_timebase"]["num"].asInt();
203  if (!root["video_timebase"]["den"].isNull())
204  info.video_timebase.den = root["video_timebase"]["den"].asInt();
205  }
206  if (!root["interlaced_frame"].isNull())
207  info.interlaced_frame = root["interlaced_frame"].asBool();
208  if (!root["top_field_first"].isNull())
209  info.top_field_first = root["top_field_first"].asBool();
210  if (!root["acodec"].isNull())
211  info.acodec = root["acodec"].asString();
212 
213  if (!root["audio_bit_rate"].isNull())
214  info.audio_bit_rate = root["audio_bit_rate"].asInt();
215  if (!root["sample_rate"].isNull())
216  info.sample_rate = root["sample_rate"].asInt();
217  if (!root["channels"].isNull())
218  info.channels = root["channels"].asInt();
219  if (!root["channel_layout"].isNull())
220  info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
221  if (!root["audio_stream_index"].isNull())
222  info.audio_stream_index = root["audio_stream_index"].asInt();
223  if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
224  if (!root["audio_timebase"]["num"].isNull())
225  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
226  if (!root["audio_timebase"]["den"].isNull())
227  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
228  }
229 }
int max_height
The maximium image height needed by this clip (used for optimizations)
Definition: ReaderBase.h:103
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:106
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:155
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
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
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:71
This namespace is the default namespace for all code in the openshot library.
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:102
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