OpenShot Library | libopenshot  0.3.3
ImageWriter.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 //Require ImageMagick support
14 #ifdef USE_IMAGEMAGICK
15 
16 #include "MagickUtilities.h"
17 #include "QtUtilities.h"
18 
19 #include "ImageWriter.h"
20 #include "Exceptions.h"
21 #include "Frame.h"
22 #include "ReaderBase.h"
23 #include "ZmqLogger.h"
24 
25 using namespace openshot;
26 
28  path(path), cache_size(8), write_video_count(0), image_quality(75), number_of_loops(1),
29  combine_frames(true), is_open(false)
30 {
31  info.has_audio = false;
32  info.has_video = true;
33 }
34 
35 // Set video export options
37  std::string format, Fraction fps, int width, int height,
38  int quality, int loops, bool combine)
39 {
40  // Set frames per second (if provided)
41  info.fps = fps;
42 
43  // Set image magic properties
44  image_quality = quality;
45  number_of_loops = loops;
46  combine_frames = combine;
47  info.vcodec = format;
48 
49  // Set the timebase (inverse of fps)
51 
52  info.width = std::max(1, width);
53  info.height = std::max(1, height);
54 
55  info.video_bit_rate = quality;
56 
57  // Calculate the DAR (display aspect ratio)
58  Fraction size(
61 
62  // Reduce size fraction
63  size.Reduce();
64 
65  // Set the ratio based on the reduced fraction
66  info.display_ratio = size;
67 
69  "ImageWriter::SetVideoOptions (" + format + ")",
70  "width", width,
71  "height", height,
72  "size.num", size.num,
73  "size.den", size.den,
74  "fps.num", fps.num,
75  "fps.den", fps.den);
76 }
77 
78 // Open the writer
80 {
81  is_open = true;
82 }
83 
84 // Add a frame to the queue waiting to be encoded.
85 void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
86 {
87  // Check for open reader (or throw exception)
88  if (!is_open) {
89  throw WriterClosed(
90  "The ImageWriter is closed. "
91  "Call Open() before calling this method.", path);
92  }
93 
94  // Copy and resize image
95  auto qimage = frame->GetImage();
96  auto frame_image = openshot::QImage2Magick(qimage);
97  frame_image->magick( info.vcodec );
98  frame_image->backgroundColor(Magick::Color("none"));
99  MAGICK_IMAGE_ALPHA(frame_image, true);
100  frame_image->quality(image_quality);
101  frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
102  frame_image->animationIterations(number_of_loops);
103 
104  // Calculate correct DAR (display aspect ratio)
105  int new_height = info.height * frame->GetPixelRatio().Reciprocal().ToDouble();
106 
107  // Resize image
108  Magick::Geometry new_size(info.width, new_height);
109  new_size.aspect(true);
110  frame_image->resize(new_size);
111 
112 
113  // Put resized frame in vector (waiting to be written)
114  frames.push_back(*frame_image.get());
115 
116  // Keep track of the last frame added
117  last_frame = frame;
118 }
119 
120 // Write a block of frames from a reader
121 void ImageWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t length)
122 {
124  "ImageWriter::WriteFrame (from Reader)",
125  "start", start,
126  "length", length);
127 
128  // Loop through each frame (and encoded it)
129  for (int64_t number = start; number <= length; number++)
130  {
131  // Get the frame
132  std::shared_ptr<Frame> f = reader->GetFrame(number);
133 
134  // Encode frame
135  WriteFrame(f);
136  }
137 }
138 
139 // Close the writer and encode/output final image to the disk.
141 {
142  // Write frame images to file
143  Magick::writeImages(frames.begin(), frames.end(), path, combine_frames);
144 
145  // Clear frames vector & counters, close writer
146  frames.clear();
147  write_video_count = 0;
148  is_open = false;
149 
150  ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close");
151 }
152 
153 #endif //USE_IMAGEMAGICK
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
openshot::WriterInfo::video_bit_rate
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: WriterBase.h:43
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::WriterClosed
Exception when a writer is closed, and a frame is requested.
Definition: Exceptions.h:415
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:42
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::ImageWriter::Open
void Open()
Open writer.
Definition: ImageWriter.cpp:79
openshot::ImageWriter::WriteFrame
void WriteFrame(std::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ImageWriter.cpp:85
QtUtilities.h
Header file for QtUtilities (compatibiity overlay)
openshot::WriterInfo::width
int width
The width of the video (in pixels)
Definition: WriterBase.h:40
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::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
ZmqLogger.h
Header file for ZeroMQ-based Logger class.
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:65
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:78
openshot::QImage2Magick
std::shared_ptr< Magick::Image > QImage2Magick(std::shared_ptr< QImage >)
Convert QImage to Magick::Image.
Definition: MagickUtilities.cpp:21
path
path
Definition: FFmpegWriter.cpp:1479
Frame.h
Header file for Frame class.
openshot::ZmqLogger::Instance
static ZmqLogger * Instance()
Create or get an instance of this logger singleton (invoke the class with this method)
Definition: ZmqLogger.cpp:35
MAGICK_IMAGE_ALPHA
#define MAGICK_IMAGE_ALPHA(im, a)
Definition: MagickUtilities.h:44
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:178
openshot::WriterInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: WriterBase.h:34
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:140
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::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:36
MagickUtilities.h
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::WriterInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: WriterBase.h:46
ImageWriter.h
Header file for ImageWriter class.
openshot::WriterBase::info
WriterInfo info
Information about the current media file.
Definition: WriterBase.h:76
openshot::ImageWriter::ImageWriter
ImageWriter(std::string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition: ImageWriter.cpp:27
Exceptions.h
Header file for all Exception classes.