28 #include "../include/ChunkWriter.h"
30 using namespace openshot;
33 local_reader(reader), path(path), chunk_size(24*3), chunk_count(1), frame_count(1), is_writing(
false),
34 default_extension(
".webm"), default_vcodec(
"libvpx"), default_acodec(
"libvorbis"), last_frame_needed(
false), is_open(
false)
37 info.vcodec = default_vcodec;
38 info.acodec = default_acodec;
41 CopyReaderInfo(local_reader);
47 write_json_meta_data();
54 string ChunkWriter::get_chunk_path(
int chunk_number,
string folder,
string extension)
57 stringstream chunk_count_string;
58 chunk_count_string << chunk_number;
59 QString padded_count =
"%1";
60 padded_count = padded_count.arg(chunk_count_string.str().c_str(), 6,
'0');
61 if (folder.length() != 0 && extension.length() != 0)
63 return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str() + QDir::separator() + padded_count + extension.c_str()).toStdString();
65 else if (folder.length() == 0 && extension.length() != 0)
67 return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + padded_count + extension.c_str()).toStdString();
69 else if (folder.length() != 0 && extension.length() == 0)
71 return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str()).toStdString();
81 throw WriterClosed(
"The ChunkWriter is closed. Call Open() before calling this method.", path);
87 frame->Save(get_chunk_path(chunk_count,
"",
".jpeg"), 1.0);
90 create_folder(get_chunk_path(chunk_count,
"final",
""));
91 writer_final =
new FFmpegWriter(get_chunk_path(chunk_count,
"final", default_extension));
92 writer_final->SetAudioOptions(
true, default_acodec, info.sample_rate, info.channels, info.channel_layout, 128000);
93 writer_final->SetVideoOptions(
true, default_vcodec, info.fps, info.width, info.height, info.pixel_ratio,
false,
false, info.video_bit_rate);
96 create_folder(get_chunk_path(chunk_count,
"preview",
""));
97 writer_preview =
new FFmpegWriter(get_chunk_path(chunk_count,
"preview", default_extension));
98 writer_preview->SetAudioOptions(
true, default_acodec, info.sample_rate, info.channels, info.channel_layout, 128000);
99 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);
102 create_folder(get_chunk_path(chunk_count,
"thumb",
""));
103 writer_thumb =
new FFmpegWriter(get_chunk_path(chunk_count,
"thumb", default_extension));
104 writer_thumb->SetAudioOptions(
true, default_acodec, info.sample_rate, info.channels, info.channel_layout, 128000);
105 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);
108 writer_final->PrepareStreams();
109 writer_preview->PrepareStreams();
110 writer_thumb->PrepareStreams();
113 writer_final->WriteHeader();
114 writer_preview->WriteHeader();
115 writer_thumb->WriteHeader();
119 last_frame_needed =
true;
124 if (last_frame_needed)
129 writer_final->WriteFrame(last_frame);
130 writer_preview->WriteFrame(last_frame);
131 writer_thumb->WriteFrame(last_frame);
134 tr1::shared_ptr<Frame> blank_frame(
new Frame(1, info.width, info.height,
"#000000", info.sample_rate, info.channels));
135 blank_frame->AddColor(info.width, info.height,
"#000000");
136 writer_final->WriteFrame(blank_frame);
137 writer_preview->WriteFrame(blank_frame);
138 writer_thumb->WriteFrame(blank_frame);
142 last_frame_needed =
false;
148 writer_final->WriteFrame(frame);
149 writer_preview->WriteFrame(frame);
150 writer_thumb->WriteFrame(frame);
155 if (frame_count % chunk_size == 0 && frame_count >= chunk_size)
157 cout <<
"Done with chunk" << endl;
158 cout <<
"frame_count: " << frame_count << endl;
159 cout <<
"chunk_size: " << chunk_size << endl;
162 for (
int z = 0; z<12; z++)
165 writer_final->WriteFrame(frame);
166 writer_preview->WriteFrame(frame);
167 writer_thumb->WriteFrame(frame);
171 writer_final->WriteTrailer();
172 writer_preview->WriteTrailer();
173 writer_thumb->WriteTrailer();
176 writer_final->Close();
177 writer_preview->Close();
178 writer_thumb->Close();
199 for (
int number = start; number <= length; number++)
202 tr1::shared_ptr<Frame> f = reader->GetFrame(number);
213 for (
int number = start; number <= length; number++)
216 tr1::shared_ptr<Frame> f = local_reader->GetFrame(number);
229 cout <<
"Final chunk" << endl;
230 cout <<
"frame_count: " << frame_count << endl;
231 cout <<
"chunk_size: " << chunk_size << endl;
234 for (
int z = 0; z<12; z++)
248 writer_final->
Close();
249 writer_preview->
Close();
250 writer_thumb->
Close();
267 local_reader->
Close();
271 void ChunkWriter::write_json_meta_data()
274 string json_path = QDir::cleanPath(QString(path.c_str()) + QDir::separator() +
"info.json").toStdString();
278 myfile.open (json_path.c_str());
279 myfile << local_reader->
Json() << endl;
284 void ChunkWriter::create_folder(
string path)
286 QDir dir(path.c_str());
293 bool ChunkWriter::is_chunk_valid()
ChunkWriter(string path, ReaderBase *reader)
Constructor for ChunkWriter. Throws one of the following exceptions.
This class represents a single frame of video (i.e. image & audio data)
This class uses the FFmpeg libraries, to write and encode video files and audio files.
virtual void Close()=0
Close the reader (and any resources it was consuming)
This abstract class is the base class, used by all readers in libopenshot.
void WriteFrame(tr1::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Exception when no valid codec is found for a file.
Exception when memory could not be allocated.
Exception when invalid encoding options are used.
Exception for files that can not be found or opened.
void WriteFrame(tr1::shared_ptr< 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...
void Close()
Close the writer.
virtual string Json()=0
Get and Set JSON methods.
Exception when a writer is closed, and a frame is requested.
void Close()
Close the writer.