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