31 #include "../include/ChunkWriter.h"
36 local_reader(reader), path(path), chunk_size(24*3), chunk_count(1), frame_count(1), is_writing(false),
37 default_extension(
".webm"), default_vcodec(
"libvpx"), default_acodec(
"libvorbis"), last_frame_needed(false), is_open(false)
50 write_json_meta_data();
57 std::string ChunkWriter::get_chunk_path(int64_t chunk_number, std::string folder, std::string extension)
60 std::stringstream chunk_count_string;
61 chunk_count_string << chunk_number;
62 QString padded_count =
"%1";
63 padded_count = padded_count.arg(chunk_count_string.str().c_str(), 6,
'0');
64 if (folder.length() != 0 && extension.length() != 0)
66 return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str() + QDir::separator() + padded_count + extension.c_str()).toStdString();
68 else if (folder.length() == 0 && extension.length() != 0)
70 return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + padded_count + extension.c_str()).toStdString();
72 else if (folder.length() != 0 && extension.length() == 0)
74 return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str()).toStdString();
84 throw WriterClosed(
"The ChunkWriter is closed. Call Open() before calling this method.", path);
90 frame->Save(get_chunk_path(chunk_count,
"",
".jpeg"), 1.0);
93 create_folder(get_chunk_path(chunk_count,
"final",
""));
94 writer_final =
new FFmpegWriter(get_chunk_path(chunk_count,
"final", default_extension));
99 create_folder(get_chunk_path(chunk_count,
"preview",
""));
100 writer_preview =
new FFmpegWriter(get_chunk_path(chunk_count,
"preview", default_extension));
105 create_folder(get_chunk_path(chunk_count,
"thumb",
""));
106 writer_thumb =
new FFmpegWriter(get_chunk_path(chunk_count,
"thumb", default_extension));
122 last_frame_needed =
true;
127 if (last_frame_needed)
145 last_frame_needed =
false;
158 if (frame_count % chunk_size == 0 && frame_count >= chunk_size)
160 std::cout <<
"Done with chunk" << std::endl;
161 std::cout <<
"frame_count: " << frame_count << std::endl;
162 std::cout <<
"chunk_size: " << chunk_size << std::endl;
165 for (
int z = 0; z<12; z++)
179 writer_final->
Close();
180 writer_preview->
Close();
181 writer_thumb->
Close();
202 for (int64_t number = start; number <= length; number++)
205 std::shared_ptr<Frame> f = reader->
GetFrame(number);
216 for (int64_t number = start; number <= length; number++)
219 std::shared_ptr<Frame> f = local_reader->
GetFrame(number);
232 std::cout <<
"Final chunk" << std::endl;
233 std::cout <<
"frame_count: " << frame_count << std::endl;
234 std::cout <<
"chunk_size: " << chunk_size << std::endl;
237 for (
int z = 0; z<12; z++)
251 writer_final->
Close();
252 writer_preview->
Close();
253 writer_thumb->
Close();
270 local_reader->
Close();
274 void ChunkWriter::write_json_meta_data()
277 std::string json_path = QDir::cleanPath(QString(path.c_str()) + QDir::separator() +
"info.json").toStdString();
280 std::ofstream myfile;
281 myfile.open (json_path.c_str());
282 myfile << local_reader->
Json() << std::endl;
287 void ChunkWriter::create_folder(std::string path)
289 QDir dir(path.c_str());
296 bool ChunkWriter::is_chunk_valid()
void Close()
Close the writer.
ChunkWriter(std::string path, openshot::ReaderBase *reader)
Constructor for ChunkWriter. Throws one of the following exceptions.
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
This class uses the FFmpeg libraries, to write and encode video files and audio files.
void Close()
Close the writer.
void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate)
Set audio export options.
void PrepareStreams()
Prepare & initialize streams and open codecs. This method is called automatically by the Open() metho...
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.
void WriteHeader()
Write the file header (after the options are set). This method is called automatically by the Open() ...
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
void WriteTrailer()
Write the file trailer (after all frames are written). This is called automatically by the Close() me...
This class represents a single frame of video (i.e. image & audio data)
This abstract class is the base class, used by all readers in libopenshot.
virtual std::string Json() const =0
Get and Set JSON methods.
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
virtual void Close()=0
Close the reader (and any resources it was consuming)
void CopyReaderInfo(openshot::ReaderBase *reader)
This method copy's the info struct of a reader, and sets the writer with the same info.
WriterInfo info
Information about the current media file.
Exception when a writer is closed, and a frame is requested.
This namespace is the default namespace for all code in the openshot library.
int height
The height of the video (in pixels)
int video_bit_rate
The bit rate of the video stream (in bytes)
int channels
The number of audio channels used in the audio stream.
std::string vcodec
The name of the video codec used to encode / decode the video stream.
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
std::string acodec
The name of the audio codec used to encode / decode the video stream.
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
int width
The width of the video (in pixels)
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
int sample_rate
The number of audio samples per second (44100 is a common sample rate)