Loading...
Searching...
No Matches
ChunkWriter.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "ChunkWriter.h"
#include "Exceptions.h"
#include "Frame.h"
using namespace openshot;
ChunkWriter::ChunkWriter(std::string path, ReaderBase *reader) :
local_reader(reader), path(path), chunk_size(24*3), chunk_count(1), frame_count(1), is_writing(false),
default_extension(".webm"), default_vcodec("libvpx"), default_acodec("libvorbis"), last_frame_needed(false), is_open(false)
{
// Change codecs to default
info.vcodec = default_vcodec;
info.acodec = default_acodec;
// Copy info struct from the source reader
CopyReaderInfo(local_reader);
// Create folder (if it does not exist)
create_folder(path);
// Write JSON meta data file
write_json_meta_data();
// Open reader
local_reader->Open();
}
// get a formatted path of a specific chunk
std::string ChunkWriter::get_chunk_path(int64_t chunk_number, std::string folder, std::string extension)
{
// Create path of new chunk video
std::stringstream chunk_count_string;
chunk_count_string << chunk_number;
QString padded_count = "%1"; //chunk_count_string.str().c_str();
padded_count = padded_count.arg(chunk_count_string.str().c_str(), 6, '0');
if (folder.length() != 0 && extension.length() != 0)
// Return path with FOLDER and EXTENSION name
return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str() + QDir::separator() + padded_count + extension.c_str()).toStdString();
else if (folder.length() == 0 && extension.length() != 0)
// Return path with NO FOLDER and EXTENSION name
return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + padded_count + extension.c_str()).toStdString();
else if (folder.length() != 0 && extension.length() == 0)
// Return path with FOLDER and NO EXTENSION
return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str()).toStdString();
else
return "";
}
// Add a frame to the queue waiting to be encoded.
void ChunkWriter::WriteFrame(std::shared_ptr<openshot::Frame> frame)
{
// Check for open reader (or throw exception)
if (!is_open)
throw WriterClosed("The ChunkWriter is closed. Call Open() before calling this method.", path);
// Check if currently writing chunks?
if (!is_writing)
{
// Save thumbnail of chunk start frame
frame->Save(get_chunk_path(chunk_count, "", ".jpeg"), 1.0);
// Create FFmpegWriter (FINAL quality)
create_folder(get_chunk_path(chunk_count, "final", ""));
writer_final = new FFmpegWriter(get_chunk_path(chunk_count, "final", default_extension));
writer_final->SetAudioOptions(true, default_acodec, info.sample_rate, info.channels, info.channel_layout, 128000);
writer_final->SetVideoOptions(true, default_vcodec, info.fps, info.width, info.height, info.pixel_ratio, false, false, info.video_bit_rate);
// Create FFmpegWriter (PREVIEW quality)
create_folder(get_chunk_path(chunk_count, "preview", ""));
writer_preview = new FFmpegWriter(get_chunk_path(chunk_count, "preview", default_extension));
writer_preview->SetAudioOptions(true, default_acodec, info.sample_rate, info.channels, info.channel_layout, 128000);
writer_preview->SetVideoOptions(true, default_vcodec, info.fps, info.width * 0.5, info.height * 0.5, info.pixel_ratio, false, false, info.video_bit_rate * 0.5);
// Create FFmpegWriter (LOW quality)
create_folder(get_chunk_path(chunk_count, "thumb", ""));
writer_thumb = new FFmpegWriter(get_chunk_path(chunk_count, "thumb", default_extension));
writer_thumb->SetAudioOptions(true, default_acodec, info.sample_rate, info.channels, info.channel_layout, 128000);
writer_thumb->SetVideoOptions(true, default_vcodec, info.fps, info.width * 0.25, info.height * 0.25, info.pixel_ratio, false, false, info.video_bit_rate * 0.25);
// Prepare Streams
writer_final->PrepareStreams();
writer_preview->PrepareStreams();
writer_thumb->PrepareStreams();
// Write header
writer_final->WriteHeader();
writer_preview->WriteHeader();
writer_thumb->WriteHeader();
// Keep track that a chunk is being written
is_writing = true;
last_frame_needed = true;
}
// If this is not the 1st chunk, always start frame 1 with the last frame from the previous
// chunk. This helps to prevent audio resampling issues (because it "stokes" the sample array)
if (last_frame_needed)
{
if (last_frame)
{
// Write the previous chunks LAST FRAME to the current chunk
writer_final->WriteFrame(last_frame);
writer_preview->WriteFrame(last_frame);
writer_thumb->WriteFrame(last_frame);
} else {
// Write the 1st frame (of the 1st chunk)... since no previous chunk is available
auto blank_frame = std::make_shared<Frame>(
1, info.width, info.height, "#000000",
info.sample_rate, info.channels);
blank_frame->AddColor(info.width, info.height, "#000000");
writer_final->WriteFrame(blank_frame);
writer_preview->WriteFrame(blank_frame);
writer_thumb->WriteFrame(blank_frame);
}
// disable last frame
last_frame_needed = false;
}
// WRITE THE CURRENT FRAME TO THE CURRENT CHUNK
writer_final->WriteFrame(frame);
writer_preview->WriteFrame(frame);
writer_thumb->WriteFrame(frame);
// Write the frames once it reaches the correct chunk size
if (frame_count % chunk_size == 0 && frame_count >= chunk_size)
{
// Pad an additional 12 frames
for (int z = 0; z<12; z++)
{
// Repeat frame
writer_final->WriteFrame(frame);
writer_preview->WriteFrame(frame);
writer_thumb->WriteFrame(frame);
}
// Write Footer
writer_final->WriteTrailer();
writer_preview->WriteTrailer();
writer_thumb->WriteTrailer();
// Close writer & reader
writer_final->Close();
writer_preview->Close();
writer_thumb->Close();
// Increment chunk count
chunk_count++;
// Stop writing chunk
is_writing = false;
}
// Increment frame counter
frame_count++;
// Keep track of the last frame added
last_frame = frame;
}
// Write a block of frames from a reader
void ChunkWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t 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);
}
}
// Write a block of frames from the local cached reader
void ChunkWriter::WriteFrame(int64_t start, int64_t length)
{
// Loop through each frame (and encoded it)
for (int64_t number = start; number <= length; number++)
{
// Get the frame
std::shared_ptr<Frame> f = local_reader->GetFrame(number);
// Encode frame
WriteFrame(f);
}
}
// Close the writer
void ChunkWriter::Close()
{
// Write the frames once it reaches the correct chunk size
if (is_writing)
{
// Pad an additional 12 frames
for (int z = 0; z<12; z++)
{
// Repeat frame
writer_final->WriteFrame(last_frame);
writer_preview->WriteFrame(last_frame);
writer_thumb->WriteFrame(last_frame);
}
// Write Footer
writer_final->WriteTrailer();
writer_preview->WriteTrailer();
writer_thumb->WriteTrailer();
// Close writer & reader
writer_final->Close();
writer_preview->Close();
writer_thumb->Close();
// Increment chunk count
chunk_count++;
// Stop writing chunk
is_writing = false;
}
// close writer
is_open = false;
// Reset frame counters
chunk_count = 0;
frame_count = 0;
// Open reader
local_reader->Close();
}
// write JSON meta data
void ChunkWriter::write_json_meta_data()
{
// Load path of chunk folder
std::string json_path = QDir::cleanPath(QString(path.c_str()) + QDir::separator() + "info.json").toStdString();
// Write JSON file
std::ofstream myfile;
myfile.open (json_path.c_str());
myfile << local_reader->Json() << std::endl;
myfile.close();
}
// check for chunk folder
void ChunkWriter::create_folder(std::string path)
{
QDir dir(path.c_str());
if (!dir.exists()) {
dir.mkpath(".");
}
}
// check for valid chunk json
bool ChunkWriter::is_chunk_valid()
{
return true;
}
// Open the writer
void ChunkWriter::Open()
{
is_open = true;
}
ChunkWriter.h
Header file for ChunkWriter class.
Exceptions.h
Header file for all Exception classes.
path
path
Definition FFmpegWriter.cpp:1515
Frame.h
Header file for Frame class.
openshot::ChunkWriter::Close
void Close()
Close the writer.
Definition ChunkWriter.cpp:209
openshot::ChunkWriter::ChunkWriter
ChunkWriter(std::string path, openshot::ReaderBase *reader)
Constructor for ChunkWriter. Throws one of the following exceptions.
Definition ChunkWriter.cpp:19
openshot::ChunkWriter::Open
void Open()
Open writer.
Definition ChunkWriter.cpp:280
openshot::ChunkWriter::WriteFrame
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition ChunkWriter.cpp:64
openshot::FFmpegWriter
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition FFmpegWriter.h:116
openshot::FFmpegWriter::Close
void Close()
Close the writer.
Definition FFmpegWriter.cpp:1038
openshot::FFmpegWriter::SetAudioOptions
void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate)
Set audio export options.
Definition FFmpegWriter.cpp:286
openshot::FFmpegWriter::PrepareStreams
void PrepareStreams()
Prepare & initialize streams and open codecs. This method is called automatically by the Open() metho...
Definition FFmpegWriter.cpp:615
openshot::FFmpegWriter::SetVideoOptions
void SetVideoOptions(bool has_video, std::string codec, openshot::Fraction fps, int width, int height, openshot::Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate)
Set video export options.
Definition FFmpegWriter.cpp:163
openshot::FFmpegWriter::WriteHeader
void WriteHeader()
Write the file header (after the options are set). This method is called automatically by the Open() ...
Definition FFmpegWriter.cpp:632
openshot::FFmpegWriter::WriteFrame
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition FFmpegWriter.cpp:677
openshot::FFmpegWriter::WriteTrailer
void WriteTrailer()
Write the file trailer (after all frames are written). This is called automatically by the Close() me...
Definition FFmpegWriter.cpp:822
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot::ReaderBase::Json
virtual std::string Json() const =0
Generate JSON string of this object.
openshot::ReaderBase::Open
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
openshot::ReaderBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
openshot::ReaderBase::Close
virtual void Close()=0
Close the reader (and any resources it was consuming)
openshot::WriterBase::CopyReaderInfo
void CopyReaderInfo(openshot::ReaderBase *reader)
This method copy's the info struct of a reader, and sets the writer with the same info.
Definition WriterBase.cpp:56
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
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::WriterInfo::height
int height
The height of the video (in pixels)
Definition WriterBase.h:39
openshot::WriterInfo::video_bit_rate
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition WriterBase.h:43
openshot::WriterInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition WriterBase.h:55
openshot::WriterInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition WriterBase.h:46
openshot::WriterInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition WriterBase.h:42
openshot::WriterInfo::acodec
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition WriterBase.h:52
openshot::WriterInfo::channel_layout
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition WriterBase.h:56
openshot::WriterInfo::width
int width
The width of the video (in pixels)
Definition WriterBase.h:40
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::WriterInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition WriterBase.h:54