OpenShot Library | libopenshot  0.1.1
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  // Initialize debug
63  debug = false;
64 }
65 
66 // Append debug information as JSON
67 void WriterBase::AppendDebugMethod(string method_name, string arg1_name, float arg1_value,
68  string arg2_name, float arg2_value,
69  string arg3_name, float arg3_value,
70  string arg4_name, float arg4_value,
71  string arg5_name, float arg5_value,
72  string arg6_name, float arg6_value)
73 {
74  if (!debug)
75  // Don't do anything
76  return;
77 
78  // Output to standard output
79  #pragma omp critical (debug_output)
80  {
81  stringstream message;
82  message << fixed << setprecision(4);
83  message << method_name << " (";
84 
85  // Add attributes to method JSON
86  if (arg1_name.length() > 0)
87  message << arg1_name << "=" << arg1_value;
88 
89  if (arg2_name.length() > 0)
90  message << ", " << arg2_name << "=" << arg2_value;
91 
92  if (arg3_name.length() > 0)
93  message << ", " << arg3_name << "=" << arg3_value;
94 
95  if (arg4_name.length() > 0)
96  message << ", " << arg4_name << "=" << arg4_value;
97 
98  if (arg5_name.length() > 0)
99  message << ", " << arg5_name << "=" << arg5_value;
100 
101  if (arg6_name.length() > 0)
102  message << ", " << arg6_name << "=" << arg6_value;
103 
104  // Output to standard output
105  message << ")" << endl;
106 
107  // Send message through ZMQ
108  ZmqLogger::Instance()->Log(message.str());
109  }
110 }
111 
112 // This method copy's the info struct of a reader, and sets the writer with the same info
114 {
115  info.has_video = reader->info.has_video;
116  info.has_audio = reader->info.has_audio;
118  info.duration = reader->info.duration;
119  info.file_size = reader->info.file_size;
120  info.height = reader->info.height;
121  info.width = reader->info.width;
123  info.fps.num = reader->info.fps.num;
124  info.fps.den = reader->info.fps.den;
130  info.vcodec = reader->info.vcodec;
137  info.acodec = reader->info.acodec;
139  info.sample_rate = reader->info.sample_rate;
140  info.channels = reader->info.channels;
145 }
146 
147 // Display file information
149  cout << fixed << setprecision(2) << boolalpha;
150  cout << "----------------------------" << endl;
151  cout << "----- File Information -----" << endl;
152  cout << "----------------------------" << endl;
153  cout << "--> Has Video: " << info.has_video << endl;
154  cout << "--> Has Audio: " << info.has_audio << endl;
155  cout << "--> Has Single Image: " << info.has_single_image << endl;
156  cout << "--> Duration: " << info.duration << " Seconds" << endl;
157  cout << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << endl;
158  cout << "----------------------------" << endl;
159  cout << "----- Video Attributes -----" << endl;
160  cout << "----------------------------" << endl;
161  cout << "--> Width: " << info.width << endl;
162  cout << "--> Height: " << info.height << endl;
163  cout << "--> Pixel Format: " << info.pixel_format << endl;
164  cout << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << endl;
165  cout << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << endl;
166  cout << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << endl;
167  cout << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << endl;
168  cout << "--> Video Codec: " << info.vcodec << endl;
169  cout << "--> Video Length: " << info.video_length << " Frames" << endl;
170  cout << "--> Video Stream Index: " << info.video_stream_index << endl;
171  cout << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << endl;
172  cout << "--> Interlaced: " << info.interlaced_frame << endl;
173  cout << "--> Interlaced: Top Field First: " << info.top_field_first << endl;
174  cout << "----------------------------" << endl;
175  cout << "----- Audio Attributes -----" << endl;
176  cout << "----------------------------" << endl;
177  cout << "--> Audio Codec: " << info.acodec << endl;
178  cout << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << endl;
179  cout << "--> Sample Rate: " << info.sample_rate << " Hz" << endl;
180  cout << "--> # of Channels: " << info.channels << endl;
181  cout << "--> Channel Layout: " << info.channel_layout << endl;
182  cout << "--> Audio Stream Index: " << info.audio_stream_index << endl;
183  cout << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << endl;
184  cout << "----------------------------" << endl;
185 }
186 
187 // Generate JSON string of this object
189 
190  // Return formatted string
191  return JsonValue().toStyledString();
192 }
193 
194 // Generate Json::JsonValue for this object
195 Json::Value WriterBase::JsonValue() {
196 
197  // Create root json object
198  Json::Value root;
199  root["has_video"] = info.has_video;
200  root["has_audio"] = info.has_audio;
201  root["has_single_image"] = info.has_single_image;
202  root["duration"] = info.duration;
203  stringstream filesize_stream;
204  filesize_stream << info.file_size;
205  root["file_size"] = filesize_stream.str();
206  root["height"] = info.height;
207  root["width"] = info.width;
208  root["pixel_format"] = info.pixel_format;
209  root["fps"] = Json::Value(Json::objectValue);
210  root["fps"]["num"] = info.fps.num;
211  root["fps"]["den"] = info.fps.den;
212  root["video_bit_rate"] = info.video_bit_rate;
213  root["pixel_ratio"] = Json::Value(Json::objectValue);
214  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
215  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
216  root["display_ratio"] = Json::Value(Json::objectValue);
217  root["display_ratio"]["num"] = info.display_ratio.num;
218  root["display_ratio"]["den"] = info.display_ratio.den;
219  root["vcodec"] = info.vcodec;
220  stringstream video_length_stream;
221  video_length_stream << info.video_length;
222  root["video_length"] = video_length_stream.str();
223  root["video_stream_index"] = info.video_stream_index;
224  root["video_timebase"] = Json::Value(Json::objectValue);
225  root["video_timebase"]["num"] = info.video_timebase.num;
226  root["video_timebase"]["den"] = info.video_timebase.den;
227  root["interlaced_frame"] = info.interlaced_frame;
228  root["top_field_first"] = info.top_field_first;
229  root["acodec"] = info.acodec;
230  root["audio_bit_rate"] = info.audio_bit_rate;
231  root["sample_rate"] = info.sample_rate;
232  root["channels"] = info.channels;
233  root["channel_layout"] = info.channel_layout;
234  root["audio_stream_index"] = info.audio_stream_index;
235  root["audio_timebase"] = Json::Value(Json::objectValue);
236  root["audio_timebase"]["num"] = info.audio_timebase.num;
237  root["audio_timebase"]["den"] = info.audio_timebase.den;
238 
239  // return JsonValue
240  return root;
241 }
242 
243 // Load JSON string into this object
244 void WriterBase::SetJson(string value) throw(InvalidJSON) {
245 
246  // Parse JSON string into JSON objects
247  Json::Value root;
248  Json::Reader reader;
249  bool success = reader.parse( value, root );
250  if (!success)
251  // Raise exception
252  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
253 
254  try
255  {
256  // Set all values that match
257  SetJsonValue(root);
258  }
259  catch (exception e)
260  {
261  // Error parsing JSON (or missing keys)
262  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
263  }
264 }
265 
266 // Load Json::JsonValue into this object
267 void WriterBase::SetJsonValue(Json::Value root) {
268 
269  // Set data from Json (if key is found)
270  if (!root["has_video"].isNull())
271  info.has_video = root["has_video"].asBool();
272  if (!root["has_audio"].isNull())
273  info.has_audio = root["has_audio"].asBool();
274  if (!root["has_single_image"].isNull())
275  info.has_single_image = root["has_single_image"].asBool();
276  if (!root["duration"].isNull())
277  info.duration = root["duration"].asDouble();
278  if (!root["file_size"].isNull())
279  info.file_size = (long long) root["file_size"].asUInt();
280  if (!root["height"].isNull())
281  info.height = root["height"].asInt();
282  if (!root["width"].isNull())
283  info.width = root["width"].asInt();
284  if (!root["pixel_format"].isNull())
285  info.pixel_format = root["pixel_format"].asInt();
286  if (!root["fps"].isNull() && root["fps"].isObject()) {
287  if (!root["fps"]["num"].isNull())
288  info.fps.num = root["fps"]["num"].asInt();
289  if (!root["fps"]["den"].isNull())
290  info.fps.den = root["fps"]["den"].asInt();
291  }
292  if (!root["video_bit_rate"].isNull())
293  info.video_bit_rate = root["video_bit_rate"].asInt();
294  if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
295  if (!root["pixel_ratio"]["num"].isNull())
296  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
297  if (!root["pixel_ratio"]["den"].isNull())
298  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
299  }
300  if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
301  if (!root["display_ratio"]["num"].isNull())
302  info.display_ratio.num = root["display_ratio"]["num"].asInt();
303  if (!root["display_ratio"]["den"].isNull())
304  info.display_ratio.den = root["display_ratio"]["den"].asInt();
305  }
306  if (!root["vcodec"].isNull())
307  info.vcodec = root["vcodec"].asString();
308  if (!root["video_length"].isNull())
309  info.video_length = (long int) root["video_length"].asUInt();
310  if (!root["video_stream_index"].isNull())
311  info.video_stream_index = root["video_stream_index"].asInt();
312  if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
313  if (!root["video_timebase"]["num"].isNull())
314  info.video_timebase.num = root["video_timebase"]["num"].asInt();
315  if (!root["video_timebase"]["den"].isNull())
316  info.video_timebase.den = root["video_timebase"]["den"].asInt();
317  }
318  if (!root["interlaced_frame"].isNull())
319  info.interlaced_frame = root["interlaced_frame"].asBool();
320  if (!root["top_field_first"].isNull())
321  info.top_field_first = root["top_field_first"].asBool();
322  if (!root["acodec"].isNull())
323  info.acodec = root["acodec"].asString();
324 
325  if (!root["audio_bit_rate"].isNull())
326  info.audio_bit_rate = root["audio_bit_rate"].asInt();
327  if (!root["sample_rate"].isNull())
328  info.sample_rate = root["sample_rate"].asInt();
329  if (!root["channels"].isNull())
330  info.channels = root["channels"].asInt();
331  if (!root["channel_layout"].isNull())
332  info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
333  if (!root["audio_stream_index"].isNull())
334  info.audio_stream_index = root["audio_stream_index"].asInt();
335  if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
336  if (!root["audio_timebase"]["num"].isNull())
337  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
338  if (!root["audio_timebase"]["den"].isNull())
339  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
340  }
341 }
int channels
The number of audio channels used in the audio stream.
Definition: WriterBase.h:72
long long file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
void AppendDebugMethod(string method_name, string arg1_name, float arg1_value, string arg2_name, float arg2_value, string arg3_name, float arg3_value, string arg4_name, float arg4_value, string arg5_name, float arg5_value, string arg6_name, float arg6_value)
Append debug information as JSON.
Definition: WriterBase.cpp:67
int num
Numerator for the fraction.
Definition: Fraction.h:44
WriterInfo info
Information about the current media file.
Definition: WriterBase.h:106
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: WriterBase.cpp:267
void SetJson(string value)
Load JSON string into this object.
Definition: WriterBase.cpp:244
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's the info struct of a reader, and sets the writer with the same info...
Definition: WriterBase.cpp:113
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:148
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
void Log(string message)
Log message to all subscribers of this logger (if any)
Definition: ZmqLogger.cpp:97
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
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
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:120
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
static ZmqLogger * Instance()
Create or get an instance of this logger singleton (invoke the class with this method) ...
Definition: ZmqLogger.cpp:38
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:195
int audio_stream_index
The index of the audio stream.
Definition: WriterBase.h:74
Exception for invalid JSON.
Definition: Exceptions.h:152
int file_size
Size of file (in bytes)
Definition: WriterBase.h:55
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:188
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
long int video_length
The number of frames in the video stream.
Definition: WriterBase.h:64
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