OpenShot Library | libopenshot  0.1.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 }
62 
63 // Display file information
65  cout << fixed << setprecision(2) << boolalpha;
66  cout << "----------------------------" << endl;
67  cout << "----- File Information -----" << endl;
68  cout << "----------------------------" << endl;
69  cout << "--> Has Video: " << info.has_video << endl;
70  cout << "--> Has Audio: " << info.has_audio << endl;
71  cout << "--> Has Single Image: " << info.has_single_image << endl;
72  cout << "--> Duration: " << info.duration << " Seconds" << endl;
73  cout << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << endl;
74  cout << "----------------------------" << endl;
75  cout << "----- Video Attributes -----" << endl;
76  cout << "----------------------------" << endl;
77  cout << "--> Width: " << info.width << endl;
78  cout << "--> Height: " << info.height << endl;
79  cout << "--> Pixel Format: " << info.pixel_format << endl;
80  cout << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << endl;
81  cout << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << endl;
82  cout << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << endl;
83  cout << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << endl;
84  cout << "--> Video Codec: " << info.vcodec << endl;
85  cout << "--> Video Length: " << info.video_length << " Frames" << endl;
86  cout << "--> Video Stream Index: " << info.video_stream_index << endl;
87  cout << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << endl;
88  cout << "--> Interlaced: " << info.interlaced_frame << endl;
89  cout << "--> Interlaced: Top Field First: " << info.top_field_first << endl;
90  cout << "----------------------------" << endl;
91  cout << "----- Audio Attributes -----" << endl;
92  cout << "----------------------------" << endl;
93  cout << "--> Audio Codec: " << info.acodec << endl;
94  cout << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << endl;
95  cout << "--> Sample Rate: " << info.sample_rate << " Hz" << endl;
96  cout << "--> # of Channels: " << info.channels << endl;
97  cout << "--> Channel Layout: " << info.channel_layout << endl;
98  cout << "--> Audio Stream Index: " << info.audio_stream_index << endl;
99  cout << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << endl;
100  cout << "----------------------------" << endl;
101 }
102 
103 // Generate Json::JsonValue for this object
104 Json::Value ReaderBase::JsonValue() {
105 
106  // Create root json object
107  Json::Value root;
108  root["has_video"] = info.has_video;
109  root["has_audio"] = info.has_audio;
110  root["has_single_image"] = info.has_single_image;
111  root["duration"] = info.duration;
112  stringstream filesize_stream;
113  filesize_stream << info.file_size;
114  root["file_size"] = filesize_stream.str();
115  root["height"] = info.height;
116  root["width"] = info.width;
117  root["pixel_format"] = info.pixel_format;
118  root["fps"] = Json::Value(Json::objectValue);
119  root["fps"]["num"] = info.fps.num;
120  root["fps"]["den"] = info.fps.den;
121  root["video_bit_rate"] = info.video_bit_rate;
122  root["pixel_ratio"] = Json::Value(Json::objectValue);
123  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
124  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
125  root["display_ratio"] = Json::Value(Json::objectValue);
126  root["display_ratio"]["num"] = info.display_ratio.num;
127  root["display_ratio"]["den"] = info.display_ratio.den;
128  root["vcodec"] = info.vcodec;
129  stringstream video_length_stream;
130  video_length_stream << info.video_length;
131  root["video_length"] = video_length_stream.str();
132  root["video_stream_index"] = info.video_stream_index;
133  root["video_timebase"] = Json::Value(Json::objectValue);
134  root["video_timebase"]["num"] = info.video_timebase.num;
135  root["video_timebase"]["den"] = info.video_timebase.den;
136  root["interlaced_frame"] = info.interlaced_frame;
137  root["top_field_first"] = info.top_field_first;
138  root["acodec"] = info.acodec;
139  root["audio_bit_rate"] = info.audio_bit_rate;
140  root["sample_rate"] = info.sample_rate;
141  root["channels"] = info.channels;
142  root["channel_layout"] = info.channel_layout;
143  root["audio_stream_index"] = info.audio_stream_index;
144  root["audio_timebase"] = Json::Value(Json::objectValue);
145  root["audio_timebase"]["num"] = info.audio_timebase.num;
146  root["audio_timebase"]["den"] = info.audio_timebase.den;
147 
148  // return JsonValue
149  return root;
150 }
151 
152 // Load Json::JsonValue into this object
153 void ReaderBase::SetJsonValue(Json::Value root) {
154 
155  // Set data from Json (if key is found)
156  if (!root["has_video"].isNull())
157  info.has_video = root["has_video"].asBool();
158  if (!root["has_audio"].isNull())
159  info.has_audio = root["has_audio"].asBool();
160  if (!root["has_single_image"].isNull())
161  info.has_single_image = root["has_single_image"].asBool();
162  if (!root["duration"].isNull())
163  info.duration = root["duration"].asDouble();
164  if (!root["file_size"].isNull())
165  info.file_size = atoll(root["file_size"].asString().c_str());
166  if (!root["height"].isNull())
167  info.height = root["height"].asInt();
168  if (!root["width"].isNull())
169  info.width = root["width"].asInt();
170  if (!root["pixel_format"].isNull())
171  info.pixel_format = root["pixel_format"].asInt();
172  if (!root["fps"].isNull() && root["fps"].isObject()) {
173  if (!root["fps"]["num"].isNull())
174  info.fps.num = root["fps"]["num"].asInt();
175  if (!root["fps"]["den"].isNull())
176  info.fps.den = root["fps"]["den"].asInt();
177  }
178  if (!root["video_bit_rate"].isNull())
179  info.video_bit_rate = root["video_bit_rate"].asInt();
180  if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
181  if (!root["pixel_ratio"]["num"].isNull())
182  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
183  if (!root["pixel_ratio"]["den"].isNull())
184  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
185  }
186  if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
187  if (!root["display_ratio"]["num"].isNull())
188  info.display_ratio.num = root["display_ratio"]["num"].asInt();
189  if (!root["display_ratio"]["den"].isNull())
190  info.display_ratio.den = root["display_ratio"]["den"].asInt();
191  }
192  if (!root["vcodec"].isNull())
193  info.vcodec = root["vcodec"].asString();
194  if (!root["video_length"].isNull())
195  info.video_length = atoll(root["video_length"].asString().c_str());
196  if (!root["video_stream_index"].isNull())
197  info.video_stream_index = root["video_stream_index"].asInt();
198  if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
199  if (!root["video_timebase"]["num"].isNull())
200  info.video_timebase.num = root["video_timebase"]["num"].asInt();
201  if (!root["video_timebase"]["den"].isNull())
202  info.video_timebase.den = root["video_timebase"]["den"].asInt();
203  }
204  if (!root["interlaced_frame"].isNull())
205  info.interlaced_frame = root["interlaced_frame"].asBool();
206  if (!root["top_field_first"].isNull())
207  info.top_field_first = root["top_field_first"].asBool();
208  if (!root["acodec"].isNull())
209  info.acodec = root["acodec"].asString();
210 
211  if (!root["audio_bit_rate"].isNull())
212  info.audio_bit_rate = root["audio_bit_rate"].asInt();
213  if (!root["sample_rate"].isNull())
214  info.sample_rate = root["sample_rate"].asInt();
215  if (!root["channels"].isNull())
216  info.channels = root["channels"].asInt();
217  if (!root["channel_layout"].isNull())
218  info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
219  if (!root["audio_stream_index"].isNull())
220  info.audio_stream_index = root["audio_stream_index"].asInt();
221  if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
222  if (!root["audio_timebase"]["num"].isNull())
223  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
224  if (!root["audio_timebase"]["den"].isNull())
225  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
226  }
227 }
228 
229 // Test method to draw a bitmap on a Qt QGraphicsScene
230 void ReaderBase::DrawFrameOnScene(string path, long _graphics_scene_address) {
231 
232  // Get pixmap
233  QGraphicsScene *scene = reinterpret_cast<QGraphicsScene*>(_graphics_scene_address);
234  QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap(QString(path.c_str())));
235  scene->addItem(item);
236 
237 }
long long file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
int num
Numerator for the fraction.
Definition: Fraction.h:44
void DrawFrameOnScene(string path, long _graphics_scene_address)
Test method to draw a bitmap on a Qt QGraphicsScene.
Definition: ReaderBase.cpp:230
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
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
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:104
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:153
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:108
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:64
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
long int video_length
The number of frames in the video stream.
Definition: ReaderBase.h:74
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