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