OpenShot Library | libopenshot  0.1.9
WriterBase.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for WriterBase 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/WriterBase.h"
29 
30 using namespace openshot;
31 
32 // Constructor
34 {
35  // Initialized writer info
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 // This method copy's the info struct of a reader, and sets the writer with the same info
65 {
66  info.has_video = reader->info.has_video;
67  info.has_audio = reader->info.has_audio;
69  info.duration = reader->info.duration;
70  info.file_size = reader->info.file_size;
71  info.height = reader->info.height;
72  info.width = reader->info.width;
74  info.fps.num = reader->info.fps.num;
75  info.fps.den = reader->info.fps.den;
81  info.vcodec = reader->info.vcodec;
88  info.acodec = reader->info.acodec;
90  info.sample_rate = reader->info.sample_rate;
91  info.channels = reader->info.channels;
96 }
97 
98 // Display file information
100  cout << fixed << setprecision(2) << boolalpha;
101  cout << "----------------------------" << endl;
102  cout << "----- File Information -----" << endl;
103  cout << "----------------------------" << endl;
104  cout << "--> Has Video: " << info.has_video << endl;
105  cout << "--> Has Audio: " << info.has_audio << endl;
106  cout << "--> Has Single Image: " << info.has_single_image << endl;
107  cout << "--> Duration: " << info.duration << " Seconds" << endl;
108  cout << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << endl;
109  cout << "----------------------------" << endl;
110  cout << "----- Video Attributes -----" << endl;
111  cout << "----------------------------" << endl;
112  cout << "--> Width: " << info.width << endl;
113  cout << "--> Height: " << info.height << endl;
114  cout << "--> Pixel Format: " << info.pixel_format << endl;
115  cout << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << endl;
116  cout << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << endl;
117  cout << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << endl;
118  cout << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << endl;
119  cout << "--> Video Codec: " << info.vcodec << endl;
120  cout << "--> Video Length: " << info.video_length << " Frames" << endl;
121  cout << "--> Video Stream Index: " << info.video_stream_index << endl;
122  cout << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << endl;
123  cout << "--> Interlaced: " << info.interlaced_frame << endl;
124  cout << "--> Interlaced: Top Field First: " << info.top_field_first << endl;
125  cout << "----------------------------" << endl;
126  cout << "----- Audio Attributes -----" << endl;
127  cout << "----------------------------" << endl;
128  cout << "--> Audio Codec: " << info.acodec << endl;
129  cout << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << endl;
130  cout << "--> Sample Rate: " << info.sample_rate << " Hz" << endl;
131  cout << "--> # of Channels: " << info.channels << endl;
132  cout << "--> Channel Layout: " << info.channel_layout << endl;
133  cout << "--> Audio Stream Index: " << info.audio_stream_index << endl;
134  cout << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << endl;
135  cout << "----------------------------" << endl;
136 }
137 
138 // Generate JSON string of this object
140 
141  // Return formatted string
142  return JsonValue().toStyledString();
143 }
144 
145 // Generate Json::JsonValue for this object
146 Json::Value WriterBase::JsonValue() {
147 
148  // Create root json object
149  Json::Value root;
150  root["has_video"] = info.has_video;
151  root["has_audio"] = info.has_audio;
152  root["has_single_image"] = info.has_single_image;
153  root["duration"] = info.duration;
154  stringstream filesize_stream;
155  filesize_stream << info.file_size;
156  root["file_size"] = filesize_stream.str();
157  root["height"] = info.height;
158  root["width"] = info.width;
159  root["pixel_format"] = info.pixel_format;
160  root["fps"] = Json::Value(Json::objectValue);
161  root["fps"]["num"] = info.fps.num;
162  root["fps"]["den"] = info.fps.den;
163  root["video_bit_rate"] = info.video_bit_rate;
164  root["pixel_ratio"] = Json::Value(Json::objectValue);
165  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
166  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
167  root["display_ratio"] = Json::Value(Json::objectValue);
168  root["display_ratio"]["num"] = info.display_ratio.num;
169  root["display_ratio"]["den"] = info.display_ratio.den;
170  root["vcodec"] = info.vcodec;
171  stringstream video_length_stream;
172  video_length_stream << info.video_length;
173  root["video_length"] = video_length_stream.str();
174  root["video_stream_index"] = info.video_stream_index;
175  root["video_timebase"] = Json::Value(Json::objectValue);
176  root["video_timebase"]["num"] = info.video_timebase.num;
177  root["video_timebase"]["den"] = info.video_timebase.den;
178  root["interlaced_frame"] = info.interlaced_frame;
179  root["top_field_first"] = info.top_field_first;
180  root["acodec"] = info.acodec;
181  root["audio_bit_rate"] = info.audio_bit_rate;
182  root["sample_rate"] = info.sample_rate;
183  root["channels"] = info.channels;
184  root["channel_layout"] = info.channel_layout;
185  root["audio_stream_index"] = info.audio_stream_index;
186  root["audio_timebase"] = Json::Value(Json::objectValue);
187  root["audio_timebase"]["num"] = info.audio_timebase.num;
188  root["audio_timebase"]["den"] = info.audio_timebase.den;
189 
190  // return JsonValue
191  return root;
192 }
193 
194 // Load JSON string into this object
195 void WriterBase::SetJson(string value) {
196 
197  // Parse JSON string into JSON objects
198  Json::Value root;
199  Json::Reader reader;
200  bool success = reader.parse( value, root );
201  if (!success)
202  // Raise exception
203  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
204 
205  try
206  {
207  // Set all values that match
208  SetJsonValue(root);
209  }
210  catch (exception e)
211  {
212  // Error parsing JSON (or missing keys)
213  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
214  }
215 }
216 
217 // Load Json::JsonValue into this object
218 void WriterBase::SetJsonValue(Json::Value root) {
219 
220  // Set data from Json (if key is found)
221  if (!root["has_video"].isNull())
222  info.has_video = root["has_video"].asBool();
223  if (!root["has_audio"].isNull())
224  info.has_audio = root["has_audio"].asBool();
225  if (!root["has_single_image"].isNull())
226  info.has_single_image = root["has_single_image"].asBool();
227  if (!root["duration"].isNull())
228  info.duration = root["duration"].asDouble();
229  if (!root["file_size"].isNull())
230  info.file_size = (int64_t) root["file_size"].asUInt();
231  if (!root["height"].isNull())
232  info.height = root["height"].asInt();
233  if (!root["width"].isNull())
234  info.width = root["width"].asInt();
235  if (!root["pixel_format"].isNull())
236  info.pixel_format = root["pixel_format"].asInt();
237  if (!root["fps"].isNull() && root["fps"].isObject()) {
238  if (!root["fps"]["num"].isNull())
239  info.fps.num = root["fps"]["num"].asInt();
240  if (!root["fps"]["den"].isNull())
241  info.fps.den = root["fps"]["den"].asInt();
242  }
243  if (!root["video_bit_rate"].isNull())
244  info.video_bit_rate = root["video_bit_rate"].asInt();
245  if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
246  if (!root["pixel_ratio"]["num"].isNull())
247  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
248  if (!root["pixel_ratio"]["den"].isNull())
249  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
250  }
251  if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
252  if (!root["display_ratio"]["num"].isNull())
253  info.display_ratio.num = root["display_ratio"]["num"].asInt();
254  if (!root["display_ratio"]["den"].isNull())
255  info.display_ratio.den = root["display_ratio"]["den"].asInt();
256  }
257  if (!root["vcodec"].isNull())
258  info.vcodec = root["vcodec"].asString();
259  if (!root["video_length"].isNull())
260  info.video_length = (int64_t) root["video_length"].asUInt();
261  if (!root["video_stream_index"].isNull())
262  info.video_stream_index = root["video_stream_index"].asInt();
263  if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
264  if (!root["video_timebase"]["num"].isNull())
265  info.video_timebase.num = root["video_timebase"]["num"].asInt();
266  if (!root["video_timebase"]["den"].isNull())
267  info.video_timebase.den = root["video_timebase"]["den"].asInt();
268  }
269  if (!root["interlaced_frame"].isNull())
270  info.interlaced_frame = root["interlaced_frame"].asBool();
271  if (!root["top_field_first"].isNull())
272  info.top_field_first = root["top_field_first"].asBool();
273  if (!root["acodec"].isNull())
274  info.acodec = root["acodec"].asString();
275 
276  if (!root["audio_bit_rate"].isNull())
277  info.audio_bit_rate = root["audio_bit_rate"].asInt();
278  if (!root["sample_rate"].isNull())
279  info.sample_rate = root["sample_rate"].asInt();
280  if (!root["channels"].isNull())
281  info.channels = root["channels"].asInt();
282  if (!root["channel_layout"].isNull())
283  info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
284  if (!root["audio_stream_index"].isNull())
285  info.audio_stream_index = root["audio_stream_index"].asInt();
286  if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
287  if (!root["audio_timebase"]["num"].isNull())
288  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
289  if (!root["audio_timebase"]["den"].isNull())
290  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
291  }
292 }
int channels
The number of audio channels used in the audio stream.
Definition: WriterBase.h:72
int num
Numerator for the fraction.
Definition: Fraction.h:44
WriterInfo info
Information about the current media file.
Definition: WriterBase.h:92
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: WriterBase.cpp:218
void SetJson(string value)
Load JSON string into this object.
Definition: WriterBase.cpp:195
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: WriterBase.h:60
ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:83
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: WriterBase.h:61
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
void CopyReaderInfo(ReaderBase *reader)
This method copy&#39;s the info struct of a reader, and sets the writer with the same info...
Definition: WriterBase.cpp:64
int64_t video_length
The number of frames in the video stream.
Definition: WriterBase.h:64
string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: WriterBase.h:69
float duration
Length of time (in seconds)
Definition: ReaderBase.h:64
void DisplayInfo()
Display file information in the standard output stream (stdout)
Definition: WriterBase.cpp:99
string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:79
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: WriterBase.h:63
WriterBase()
Constructor for WriterBase class, many things are initialized here.
Definition: WriterBase.cpp:33
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
int width
The width of the video (in pixels)
Definition: WriterBase.h:57
int audio_bit_rate
The bit rate of the audio stream (in bytes)
Definition: WriterBase.h:70
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
int64_t file_size
Size of file (in bytes)
Definition: WriterBase.h:55
Fraction audio_timebase
The audio timebase determines how long each audio packet should be played.
Definition: WriterBase.h:75
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: WriterBase.h:66
int video_stream_index
The index of the video stream.
Definition: WriterBase.h:65
float duration
Length of time (in seconds)
Definition: WriterBase.h:54
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
bool top_field_first
Which interlaced field should be displayed first.
Definition: WriterBase.h:68
This class represents a fraction.
Definition: Fraction.h:42
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: WriterBase.h:58
Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3) ...
Definition: WriterBase.h:62
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...
bool interlaced_frame
Are the contents of this frame interlaced.
Definition: WriterBase.h:67
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: WriterBase.h:71
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
bool has_video
Determines if this file has a video stream.
Definition: WriterBase.h:51
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: WriterBase.h:59
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:71
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: WriterBase.cpp:146
This namespace is the default namespace for all code in the openshot library.
int audio_stream_index
The index of the audio stream.
Definition: WriterBase.h:74
Exception for invalid JSON.
Definition: Exceptions.h:152
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
bool has_audio
Determines if this file has an audio stream.
Definition: WriterBase.h:52
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
ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: WriterBase.h:73
int height
The height of the video (in pixels)
Definition: WriterBase.h:56
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
string Json()
Get and Set JSON methods.
Definition: WriterBase.cpp:139
int video_stream_index
The index of the video stream.
Definition: ReaderBase.h:75
bool has_single_image
Determines if this file only contains a single image.
Definition: WriterBase.h:53
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