OpenShot Library | libopenshot  0.2.5
ImageWriter.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for ImageWriter class
4  * @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
12  * (http://www.openshotstudios.com). This file is part of
13  * OpenShot Library (http://www.openshot.org), an open-source project
14  * dedicated to delivering high quality video editing and animation solutions
15  * to the world.
16  *
17  * This file is originally based on the Libavformat API example, and then modified
18  * by the libopenshot project.
19  *
20  * OpenShot Library (libopenshot) is free software: you can redistribute it
21  * and/or modify it under the terms of the GNU Lesser General Public License
22  * as published by the Free Software Foundation, either version 3 of the
23  * License, or (at your option) any later version.
24  *
25  * OpenShot Library (libopenshot) is distributed in the hope that it will be
26  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public License
31  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
32  */
33 
34 //Require ImageMagick support
35 #ifdef USE_IMAGEMAGICK
36 
37 #include "../include/ImageWriter.h"
38 
39 using namespace openshot;
40 
42  path(path), cache_size(8), is_writing(false), write_video_count(0), image_quality(75), number_of_loops(1),
43  combine_frames(true), is_open(false)
44 {
45  // Disable audio & video (so they can be independently enabled)
46  info.has_audio = false;
47  info.has_video = true;
48 }
49 
50 // Set video export options
51 void ImageWriter::SetVideoOptions(std::string format, Fraction fps, int width, int height,
52  int quality, int loops, bool combine)
53 {
54  // Set frames per second (if provided)
55  info.fps.num = fps.num;
56  info.fps.den = fps.den;
57 
58  // Set image magic properties
59  image_quality = quality;
60  number_of_loops = loops;
61  combine_frames = combine;
62  info.vcodec = format;
63 
64  // Set the timebase (inverse of fps)
67 
68  if (width >= 1)
69  info.width = width;
70  if (height >= 1)
71  info.height = height;
72 
73  info.video_bit_rate = quality;
74 
75  // Calculate the DAR (display aspect ratio)
77 
78  // Reduce size fraction
79  size.Reduce();
80 
81  // Set the ratio based on the reduced fraction
82  info.display_ratio.num = size.num;
83  info.display_ratio.den = size.den;
84 
85  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::SetVideoOptions (" + format + ")", "width", width, "height", height, "size.num", size.num, "size.den", size.den, "fps.num", fps.num, "fps.den", fps.den);
86 }
87 
88 // Open the writer
90 {
91  is_open = true;
92 }
93 
94 // Add a frame to the queue waiting to be encoded.
95 void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
96 {
97  // Check for open reader (or throw exception)
98  if (!is_open)
99  throw WriterClosed("The ImageWriter is closed. Call Open() before calling this method.", path);
100 
101 
102  // Copy and resize image
103  std::shared_ptr<Magick::Image> frame_image = frame->GetMagickImage();
104  frame_image->magick( info.vcodec );
105  frame_image->backgroundColor(Magick::Color("none"));
106  MAGICK_IMAGE_ALPHA(frame_image, true);
107  frame_image->quality(image_quality);
108  frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
109  frame_image->animationIterations(number_of_loops);
110 
111  // Calculate correct DAR (display aspect ratio)
112  int new_width = info.width;
113  int new_height = info.height * frame->GetPixelRatio().Reciprocal().ToDouble();
114 
115  // Resize image
116  Magick::Geometry new_size(new_width, new_height);
117  new_size.aspect(true);
118  frame_image->resize(new_size);
119 
120 
121  // Put resized frame in vector (waiting to be written)
122  frames.push_back(*frame_image.get());
123 
124  // Keep track of the last frame added
125  last_frame = frame;
126 }
127 
128 // Write a block of frames from a reader
129 void ImageWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t length)
130 {
131  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::WriteFrame (from Reader)", "start", start, "length", length);
132 
133  // Loop through each frame (and encoded it)
134  for (int64_t number = start; number <= length; number++)
135  {
136  // Get the frame
137  std::shared_ptr<Frame> f = reader->GetFrame(number);
138 
139  // Encode frame
140  WriteFrame(f);
141  }
142 }
143 
144 // Close the writer and encode/output final image to the disk.
146 {
147  // Write frame's image to file
148  Magick::writeImages(frames.begin(), frames.end(), path, combine_frames);
149 
150  // Clear frames vector
151  frames.clear();
152 
153  // Reset frame counters
154  write_video_count = 0;
155 
156  // Close writer
157  is_open = false;
158 
159  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close");
160 }
161 
162 #endif //USE_IMAGEMAGICK
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:44
openshot::WriterInfo::video_bit_rate
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: WriterBase.h:61
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:63
openshot::WriterClosed
Exception when a writer is closed, and a frame is requested.
Definition: Exceptions.h:386
openshot::ReaderBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
openshot::WriterInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: WriterBase.h:60
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:39
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:45
openshot::Fraction::Reciprocal
Fraction Reciprocal()
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:84
openshot::ImageWriter::Open
void Open()
Open writer.
Definition: ImageWriter.cpp:89
openshot::Frame::GetPixelRatio
openshot::Fraction GetPixelRatio()
Set Pixel Aspect Ratio.
Definition: Frame.h:235
openshot::ImageWriter::WriteFrame
void WriteFrame(std::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ImageWriter.cpp:95
openshot::Frame::GetMagickImage
std::shared_ptr< Magick::Image > GetMagickImage()
Get pointer to ImageMagick image object.
Definition: Frame.cpp:925
openshot::WriterInfo::width
int width
The width of the video (in pixels)
Definition: WriterBase.h:58
openshot::WriterInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: WriterBase.h:67
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:62
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:47
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:48
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:74
path
path
Definition: FFmpegWriter.cpp:1410
openshot::ZmqLogger::Instance
static ZmqLogger * Instance()
Create or get an instance of this logger singleton (invoke the class with this method)
Definition: ZmqLogger.cpp:45
MAGICK_IMAGE_ALPHA
#define MAGICK_IMAGE_ALPHA(im, a)
Definition: MagickUtilities.h:48
openshot::ZmqLogger::AppendDebugMethod
void AppendDebugMethod(std::string method_name, std::string arg1_name="", float arg1_value=-1.0, std::string arg2_name="", float arg2_value=-1.0, std::string arg3_name="", float arg3_value=-1.0, std::string arg4_name="", float arg4_value=-1.0, std::string arg5_name="", float arg5_value=-1.0, std::string arg6_name="", float arg6_value=-1.0)
Append debug information.
Definition: ZmqLogger.cpp:179
openshot::WriterInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: WriterBase.h:52
openshot::ImageWriter::Close
void Close()
Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick,...
Definition: ImageWriter.cpp:145
openshot::WriterInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: WriterBase.h:53
openshot::WriterInfo::height
int height
The height of the video (in pixels)
Definition: WriterBase.h:57
openshot::ImageWriter::SetVideoOptions
void SetVideoOptions(std::string format, Fraction fps, int width, int height, int quality, int loops, bool combine)
Set the video export options.
Definition: ImageWriter.cpp:51
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
openshot::Fraction::ToDouble
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:49
openshot::WriterInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: WriterBase.h:64
openshot::WriterBase::info
WriterInfo info
Information about the current media file.
Definition: WriterBase.h:94
openshot::ImageWriter::ImageWriter
ImageWriter(std::string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition: ImageWriter.cpp:41