OpenShot Library | libopenshot 0.2.7
ChunkWriter.h
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Header file for ChunkWriter class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 *
6 * @ref License
7 */
8
9/* LICENSE
10 *
11 * Copyright (c) 2008-2019 OpenShot Studios, LLC
12 * <http://www.openshotstudios.com/>. This file is part of
13 * OpenShot Library (libopenshot), an open-source project dedicated to
14 * delivering high quality video editing and animation solutions to the
15 * world. For more information visit <http://www.openshot.org/>.
16 *
17 * OpenShot Library (libopenshot) is free software: you can redistribute it
18 * and/or modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation, either version 3 of the
20 * License, or (at your option) any later version.
21 *
22 * OpenShot Library (libopenshot) is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31#ifndef OPENSHOT_CHUNK_WRITER_H
32#define OPENSHOT_CHUNK_WRITER_H
33
34#include "ReaderBase.h"
35#include "WriterBase.h"
36#include "FFmpegWriter.h"
37#include "CacheMemory.h"
38#include "Json.h"
39
40#include <cmath>
41#include <ctime>
42#include <iostream>
43#include <fstream>
44#include <cstdio>
45#include <sstream>
46#include <unistd.h>
47#include <omp.h>
48#include <QtCore/QDir>
49
50
51namespace openshot
52{
53 /**
54 * @brief This class takes any reader and generates a special type of video file, built with
55 * chunks of small video and audio data.
56 *
57 * These chunks can easily be passed around in a distributed
58 * computing environment, without needing to share the entire video file. They also allow a
59 * chunk to be frame accurate, since seeking inaccuracies are removed.
60 *
61 * @code
62 * // This example demonstrates how to feed a reader into a ChunkWriter
63 * FFmpegReader *r = new FFmpegReader("MyAwesomeVideo.mp4"); // Get a reader
64 * r.Open(); // Open the reader
65 *
66 * // Create a ChunkWriter (and a folder location on your computer)
67 * ChunkWriter w("/folder_path_to_hold_chunks/", r);
68 *
69 * // Open the writer
70 * w.Open();
71 *
72 * // Write a block of frames to the ChunkWriter (from frame 1 to the end)
73 * w.WriteFrame(r, 1, r->info.video_length);
74 *
75 * // Close the reader & writer
76 * w.Close();
77 * r.Close();
78 * @endcode
79 */
80 class ChunkWriter : public WriterBase
81 {
82 private:
83 std::string path;
84 int64_t chunk_count;
85 int64_t chunk_size;
86 int64_t frame_count;
87 bool is_open;
88 bool is_writing;
89 openshot::ReaderBase *local_reader;
90 openshot::FFmpegWriter *writer_thumb;
91 openshot::FFmpegWriter *writer_preview;
92 openshot::FFmpegWriter *writer_final;
93 std::shared_ptr<Frame> last_frame;
94 bool last_frame_needed;
95 std::string default_extension;
96 std::string default_vcodec;
97 std::string default_acodec;
98
99 /// check for chunk folder
100 void create_folder(std::string path);
101
102 /// get a formatted path of a specific chunk
103 std::string get_chunk_path(int64_t chunk_number, std::string folder, std::string extension);
104
105 /// check for valid chunk json
106 bool is_chunk_valid();
107
108 /// write json meta data
109 void write_json_meta_data();
110
111 public:
112
113 /// @brief Constructor for ChunkWriter. Throws one of the following exceptions.
114 /// @param path The folder path of the chunk file to be created
115 /// @param reader The initial reader to base this chunk file's meta data on (such as fps, height, width, etc...)
116 ChunkWriter(std::string path, openshot::ReaderBase *reader);
117
118 /// Close the writer
119 void Close();
120
121 /// Get the chunk size (number of frames to write in each chunk)
122 int64_t GetChunkSize() { return chunk_size; };
123
124 /// Determine if writer is open or closed
125 bool IsOpen() { return is_open; };
126
127 /// Open writer
128 void Open();
129
130 /// @brief Set the chunk size (number of frames to write in each chunk)
131 /// @param new_size The number of frames to write in this chunk file
132 void SetChunkSize(int64_t new_size) { chunk_size = new_size; };
133
134 /// @brief Add a frame to the stack waiting to be encoded.
135 /// @param frame The openshot::Frame object that needs to be written to this chunk file.
136 void WriteFrame(std::shared_ptr<openshot::Frame> frame);
137
138 /// @brief Write a block of frames from a reader
139 /// @param start The starting frame number to write (of the reader passed into the constructor)
140 /// @param length The number of frames to write (of the reader passed into the constructor)
141 void WriteFrame(int64_t start, int64_t length);
142
143 /// @brief Write a block of frames from a reader
144 /// @param reader The reader containing the frames you need
145 /// @param start The starting frame number to write
146 /// @param length The number of frames to write
147 void WriteFrame(openshot::ReaderBase* reader, int64_t start, int64_t length);
148
149 };
150
151}
152
153#endif
Header file for CacheMemory class.
Header file for FFmpegWriter class.
Header file for JSON class.
Header file for ReaderBase class.
Header file for WriterBase class.
This class takes any reader and generates a special type of video file, built with chunks of small vi...
Definition: ChunkWriter.h:81
void Close()
Close the writer.
bool IsOpen()
Determine if writer is open or closed.
Definition: ChunkWriter.h:125
void SetChunkSize(int64_t new_size)
Set the chunk size (number of frames to write in each chunk)
Definition: ChunkWriter.h:132
int64_t GetChunkSize()
Get the chunk size (number of frames to write in each chunk)
Definition: ChunkWriter.h:122
ChunkWriter(std::string path, openshot::ReaderBase *reader)
Constructor for ChunkWriter. Throws one of the following exceptions.
Definition: ChunkWriter.cpp:36
void Open()
Open writer.
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ChunkWriter.cpp:81
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition: FFmpegWriter.h:148
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:88
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47