OpenShot Library | libopenshot  0.1.9
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2013 OpenShot Studios, LLC, Fabrice Bellard
9  * (http://www.openshotstudios.com). This file is part of
10  * OpenShot Library (http://www.openshot.org), an open-source project
11  * dedicated to delivering high quality video editing and animation solutions
12  * to the world.
13  *
14  * This file is originally based on the Libavformat API example, and then modified
15  * by the libopenshot project.
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/ImageWriter.h"
32 
33 using namespace openshot;
34 
36  path(path), cache_size(8), is_writing(false), write_video_count(0), image_quality(75), number_of_loops(1),
37  combine_frames(true), is_open(false)
38 {
39  // Disable audio & video (so they can be independently enabled)
40  info.has_audio = false;
41  info.has_video = true;
42 }
43 
44 // Set video export options
45 void ImageWriter::SetVideoOptions(string format, Fraction fps, int width, int height,
46  int quality, int loops, bool combine)
47 {
48  // Set frames per second (if provided)
49  info.fps.num = fps.num;
50  info.fps.den = fps.den;
51 
52  // Set image magic properties
53  image_quality = quality;
54  number_of_loops = loops;
55  combine_frames = combine;
56  info.vcodec = format;
57 
58  // Set the timebase (inverse of fps)
61 
62  if (width >= 1)
63  info.width = width;
64  if (height >= 1)
65  info.height = height;
66 
67  info.video_bit_rate = quality;
68 
69  // Calculate the DAR (display aspect ratio)
71 
72  // Reduce size fraction
73  size.Reduce();
74 
75  // Set the ratio based on the reduced fraction
76  info.display_ratio.num = size.num;
77  info.display_ratio.den = size.den;
78 
79  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);
80 }
81 
82 // Open the writer
84 {
85  is_open = true;
86 }
87 
88 // Add a frame to the queue waiting to be encoded.
89 void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
90 {
91  // Check for open reader (or throw exception)
92  if (!is_open)
93  throw WriterClosed("The ImageWriter is closed. Call Open() before calling this method.", path);
94 
95 
96  // Copy and resize image
97  std::shared_ptr<Magick::Image> frame_image = frame->GetMagickImage();
98  frame_image->magick( info.vcodec );
99  frame_image->backgroundColor(Magick::Color("none"));
100  frame_image->matte(true);
101  frame_image->quality(image_quality);
102  frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
103  frame_image->animationIterations(number_of_loops);
104 
105  // Calculate correct DAR (display aspect ratio)
106  int new_width = info.width;
107  int new_height = info.height * frame->GetPixelRatio().Reciprocal().ToDouble();
108 
109  // Resize image
110  Magick::Geometry new_size(new_width, new_height);
111  new_size.aspect(true);
112  frame_image->resize(new_size);
113 
114 
115  // Put resized frame in vector (waiting to be written)
116  frames.push_back(*frame_image.get());
117 
118  // Keep track of the last frame added
119  last_frame = frame;
120 }
121 
122 // Write a block of frames from a reader
123 void ImageWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t length)
124 {
125  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::WriteFrame (from Reader)", "start", start, "length", length, "", -1, "", -1, "", -1, "", -1);
126 
127  // Loop through each frame (and encoded it)
128  for (int64_t number = start; number <= length; number++)
129  {
130  // Get the frame
131  std::shared_ptr<Frame> f = reader->GetFrame(number);
132 
133  // Encode frame
134  WriteFrame(f);
135  }
136 }
137 
138 // Close the writer and encode/output final image to the disk.
140 {
141  // Write frame's image to file
142  Magick::writeImages(frames.begin(), frames.end(), path, combine_frames);
143 
144  // Clear frames vector
145  frames.clear();
146 
147  // Reset frame counters
148  write_video_count = 0;
149 
150  // Close writer
151  is_open = false;
152 
153  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close", "", -1, "", -1, "", -1, "", -1, "", -1, "", -1);
154 }
155 
ImageWriter(string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition: ImageWriter.cpp:35
int num
Numerator for the fraction.
Definition: Fraction.h:44
WriterInfo info
Information about the current media file.
Definition: WriterBase.h:92
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: WriterBase.h:60
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: WriterBase.h:61
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:41
void WriteFrame(std::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ImageWriter.cpp:89
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: WriterBase.h:63
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:71
void SetVideoOptions(string format, Fraction fps, int width, int height, int quality, int loops, bool combine)
Set the video export options.
Definition: ImageWriter.cpp:45
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
int width
The width of the video (in pixels)
Definition: WriterBase.h:57
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: WriterBase.h:66
void Close()
Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick...
virtual std::shared_ptr< Frame > GetFrame(int64_t number)=0
void AppendDebugMethod(string method_name, string arg1_name, float arg1_value, string arg2_name, float arg2_value, string arg3_name, float arg3_value, string arg4_name, float arg4_value, string arg5_name, float arg5_value, string arg6_name, float arg6_value)
Append debug information.
Definition: ZmqLogger.cpp:162
This class represents a fraction.
Definition: Fraction.h:42
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:62
bool has_video
Determines if this file has a video stream.
Definition: WriterBase.h:51
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: WriterBase.h:59
static ZmqLogger * Instance()
Create or get an instance of this logger singleton (invoke the class with this method) ...
Definition: ZmqLogger.cpp:38
This namespace is the default namespace for all code in the openshot library.
bool has_audio
Determines if this file has an audio stream.
Definition: WriterBase.h:52
Exception when a writer is closed, and a frame is requested.
Definition: Exceptions.h:264
int height
The height of the video (in pixels)
Definition: WriterBase.h:56
int den
Denominator for the fraction.
Definition: Fraction.h:45
void Open()
Open writer.
Definition: ImageWriter.cpp:83