Loading...
Searching...
No Matches
ImageWriter.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
//
// SPDX-License-Identifier: LGPL-3.0-or-later
//Require ImageMagick support
#ifdef USE_IMAGEMAGICK
#include "MagickUtilities.h"
#include "QtUtilities.h"
#include "ImageWriter.h"
#include "Exceptions.h"
#include "Frame.h"
#include "ReaderBase.h"
#include "ZmqLogger.h"
using namespace openshot;
ImageWriter::ImageWriter(std::string path) :
path(path), cache_size(8), write_video_count(0), image_quality(75), number_of_loops(1),
combine_frames(true), is_open(false)
{
info.has_audio = false;
info.has_video = true;
}
// Set video export options
void ImageWriter::SetVideoOptions(
std::string format, Fraction fps, int width, int height,
int quality, int loops, bool combine)
{
// Set frames per second (if provided)
info.fps = fps;
// Set image magic properties
image_quality = quality;
number_of_loops = loops;
combine_frames = combine;
info.vcodec = format;
// Set the timebase (inverse of fps)
info.video_timebase = fps.Reciprocal();
info.width = std::max(1, width);
info.height = std::max(1, height);
info.video_bit_rate = quality;
// Calculate the DAR (display aspect ratio)
Fraction size(
info.width * info.pixel_ratio.num,
info.height * info.pixel_ratio.den);
// Reduce size fraction
size.Reduce();
// Set the ratio based on the reduced fraction
info.display_ratio = size;
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);
}
// Open the writer
void ImageWriter::Open()
{
is_open = true;
}
// Add a frame to the queue waiting to be encoded.
void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
{
// Check for open reader (or throw exception)
if (!is_open) {
throw WriterClosed(
"The ImageWriter is closed. "
"Call Open() before calling this method.", path);
}
// Copy and resize image
auto qimage = frame->GetImage();
auto frame_image = openshot::QImage2Magick(qimage);
frame_image->magick( info.vcodec );
frame_image->backgroundColor(Magick::Color("none"));
MAGICK_IMAGE_ALPHA(frame_image, true);
frame_image->quality(image_quality);
frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
frame_image->animationIterations(number_of_loops);
// Calculate correct DAR (display aspect ratio)
int new_height = info.height * frame->GetPixelRatio().Reciprocal().ToDouble();
// Resize image
Magick::Geometry new_size(info.width, new_height);
new_size.aspect(true);
frame_image->resize(new_size);
// Put resized frame in vector (waiting to be written)
frames.push_back(*frame_image.get());
// Keep track of the last frame added
last_frame = frame;
}
// Write a block of frames from a reader
void ImageWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t length)
{
ZmqLogger::Instance()->AppendDebugMethod(
"ImageWriter::WriteFrame (from Reader)",
"start", start,
"length", length);
// Loop through each frame (and encoded it)
for (int64_t number = start; number <= length; number++)
{
// Get the frame
std::shared_ptr<Frame> f = reader->GetFrame(number);
// Encode frame
WriteFrame(f);
}
}
// Close the writer and encode/output final image to the disk.
void ImageWriter::Close()
{
// Write frame images to file
Magick::writeImages(frames.begin(), frames.end(), path, combine_frames);
// Clear frames vector & counters, close writer
frames.clear();
write_video_count = 0;
is_open = false;
ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close");
}
#endif //USE_IMAGEMAGICK
Exceptions.h
Header file for all Exception classes.
Frame.h
Header file for Frame class.
ImageWriter.h
Header file for ImageWriter class.
MagickUtilities.h
Header file for MagickUtilities (IM6/IM7 compatibility overlay).
MAGICK_IMAGE_ALPHA
#define MAGICK_IMAGE_ALPHA(im, a)
Definition MagickUtilities.h:44
QtUtilities.h
Header file for QtUtilities (compatibiity overlay).
ReaderBase.h
Header file for ReaderBase class.
ZmqLogger.h
Header file for ZeroMQ-based Logger class.
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::Fraction::num
int num
Numerator for the fraction.
Definition Fraction.h:32
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::Fraction::den
int den
Denominator for the fraction.
Definition Fraction.h:33
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::ImageWriter::ImageWriter
ImageWriter(std::string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition ImageWriter.cpp:27
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
openshot::ImageWriter::WriteFrame
void WriteFrame(std::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition ImageWriter.cpp:85
openshot::ImageWriter::Open
void Open()
Open writer.
Definition ImageWriter.cpp:79
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot::ReaderBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
openshot::WriterBase::info
WriterInfo info
Information about the current media file.
Definition WriterBase.h:76
openshot::WriterClosed
Exception when a writer is closed, and a frame is requested.
Definition Exceptions.h:416
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::ZmqLogger::Instance
static ZmqLogger * Instance()
Create or get an instance of this logger singleton (invoke the class with this method).
Definition ZmqLogger.cpp:35
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::QImage2Magick
std::shared_ptr< Magick::Image > QImage2Magick(std::shared_ptr< QImage >)
Convert QImage to Magick::Image.
Definition MagickUtilities.cpp:21