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