OpenShot Library | libopenshot  0.2.7
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 "ImageWriter.h"
38 #include "Exceptions.h"
39 
40 using namespace openshot;
41 
43  path(path), cache_size(8), write_video_count(0), image_quality(75), number_of_loops(1),
44  combine_frames(true), is_open(false)
45 {
46  // Disable audio & video (so they can be independently enabled)
47  info.has_audio = false;
48  info.has_video = true;
49 }
50 
51 // Set video export options
52 void ImageWriter::SetVideoOptions(std::string format, Fraction fps, int width, int height,
53  int quality, int loops, bool combine)
54 {
55  // Set frames per second (if provided)
56  info.fps.num = fps.num;
57  info.fps.den = fps.den;
58 
59  // Set image magic properties
60  image_quality = quality;
61  number_of_loops = loops;
62  combine_frames = combine;
63  info.vcodec = format;
64 
65  // Set the timebase (inverse of fps)
68 
69  if (width >= 1)
70  info.width = width;
71  if (height >= 1)
72  info.height = height;
73 
74  info.video_bit_rate = quality;
75 
76  // Calculate the DAR (display aspect ratio)
78 
79  // Reduce size fraction
80  size.Reduce();
81 
82  // Set the ratio based on the reduced fraction
83  info.display_ratio.num = size.num;
84  info.display_ratio.den = size.den;
85 
86  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);
87 }
88 
89 // Open the writer
91 {
92  is_open = true;
93 }
94 
95 // Add a frame to the queue waiting to be encoded.
96 void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
97 {
98  // Check for open reader (or throw exception)
99  if (!is_open)
100  throw WriterClosed("The ImageWriter is closed. Call Open() before calling this method.", path);
101 
102 
103  // Copy and resize image
104  std::shared_ptr<Magick::Image> frame_image = frame->GetMagickImage();
105  frame_image->magick( info.vcodec );
106  frame_image->backgroundColor(Magick::Color("none"));
107  MAGICK_IMAGE_ALPHA(frame_image, true);
108  frame_image->quality(image_quality);
109  frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
110  frame_image->animationIterations(number_of_loops);
111 
112  // Calculate correct DAR (display aspect ratio)
113  int new_width = info.width;
114  int new_height = info.height * frame->GetPixelRatio().Reciprocal().ToDouble();
115 
116  // Resize image
117  Magick::Geometry new_size(new_width, new_height);
118  new_size.aspect(true);
119  frame_image->resize(new_size);
120 
121 
122  // Put resized frame in vector (waiting to be written)
123  frames.push_back(*frame_image.get());
124 
125  // Keep track of the last frame added
126  last_frame = frame;
127 }
128 
129 // Write a block of frames from a reader
130 void ImageWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t length)
131 {
132  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::WriteFrame (from Reader)", "start", start, "length", length);
133 
134  // Loop through each frame (and encoded it)
135  for (int64_t number = start; number <= length; number++)
136  {
137  // Get the frame
138  std::shared_ptr<Frame> f = reader->GetFrame(number);
139 
140  // Encode frame
141  WriteFrame(f);
142  }
143 }
144 
145 // Close the writer and encode/output final image to the disk.
147 {
148  // Write frame's image to file
149  Magick::writeImages(frames.begin(), frames.end(), path, combine_frames);
150 
151  // Clear frames vector
152  frames.clear();
153 
154  // Reset frame counters
155  write_video_count = 0;
156 
157  // Close writer
158  is_open = false;
159 
160  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close");
161 }
162 
163 #endif //USE_IMAGEMAGICK
int num
Numerator for the fraction.
Definition: Fraction.h:50
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
#define MAGICK_IMAGE_ALPHA(im, a)
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:54
void WriteFrame(std::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ImageWriter.cpp:96
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
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:84
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
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: WriterBase.h:67
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
void WriteFrame(ReaderBase *reader, int64_t start, int64_t length)
Write a block of frames from a reader.
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:190
void Close()
Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick...
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
Header file for all Exception classes.
This class represents a fraction.
Definition: Fraction.h:48
ImageWriter(std::string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition: ImageWriter.cpp:42
bool has_video
Determines if this file has a video stream.
Definition: WriterBase.h:52
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:52
static ZmqLogger * Instance()
Create or get an instance of this logger singleton (invoke the class with this method) ...
Definition: ZmqLogger.cpp:52
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
bool has_audio
Determines if this file has an audio stream.
Definition: WriterBase.h:53
Header file for ImageWriter class.
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: WriterBase.h:64
Exception when too many seek attempts happen.
Definition: Exceptions.h:390
int height
The height of the video (in pixels)
Definition: WriterBase.h:57
int den
Denominator for the fraction.
Definition: Fraction.h:51
void Open()
Open writer.
Definition: ImageWriter.cpp:90
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: WriterBase.h:60