OpenShot Library | libopenshot  0.2.4
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 
38 #include <cmath>
39 #include <ctime>
40 #include <iostream>
41 #include <fstream>
42 #include <omp.h>
43 #include <QtCore/qdir.h>
44 #include <stdio.h>
45 #include <sstream>
46 #include <unistd.h>
47 #include "CacheMemory.h"
48 #include "Exceptions.h"
49 #include "Json.h"
50 
51 
52 namespace openshot
53 {
54  /**
55  * @brief This class takes any reader and generates a special type of video file, built with
56  * chunks of small video and audio data.
57  *
58  * These chunks can easily be passed around in a distributed
59  * computing environment, without needing to share the entire video file. They also allow a
60  * chunk to be frame accurate, since seeking inaccuracies are removed.
61  *
62  * @code
63  * // This example demonstrates how to feed a reader into a ChunkWriter
64  * FFmpegReader *r = new FFmpegReader("MyAwesomeVideo.mp4"); // Get a reader
65  * r.Open(); // Open the reader
66  *
67  * // Create a ChunkWriter (and a folder location on your computer)
68  * ChunkWriter w("/folder_path_to_hold_chunks/", r);
69  *
70  * // Open the writer
71  * w.Open();
72  *
73  * // Write a block of frames to the ChunkWriter (from frame 1 to the end)
74  * w.WriteFrame(r, 1, r->info.video_length);
75  *
76  * // Close the reader & writer
77  * w.Close();
78  * r.Close();
79  * @endcode
80  */
81  class ChunkWriter : public WriterBase
82  {
83  private:
84  std::string path;
85  int64_t chunk_count;
86  int64_t chunk_size;
87  int64_t frame_count;
88  bool is_open;
89  bool is_writing;
90  openshot::ReaderBase *local_reader;
91  openshot::FFmpegWriter *writer_thumb;
92  openshot::FFmpegWriter *writer_preview;
93  openshot::FFmpegWriter *writer_final;
94  std::shared_ptr<Frame> last_frame;
95  bool last_frame_needed;
96  std::string default_extension;
97  std::string default_vcodec;
98  std::string default_acodec;
99 
100  /// check for chunk folder
101  void create_folder(std::string path);
102 
103  /// get a formatted path of a specific chunk
104  std::string get_chunk_path(int64_t chunk_number, std::string folder, std::string extension);
105 
106  /// check for valid chunk json
107  bool is_chunk_valid();
108 
109  /// write json meta data
110  void write_json_meta_data();
111 
112  public:
113 
114  /// @brief Constructor for ChunkWriter. Throws one of the following exceptions.
115  /// @param path The folder path of the chunk file to be created
116  /// @param reader The initial reader to base this chunk file's meta data on (such as fps, height, width, etc...)
117  ChunkWriter(std::string path, openshot::ReaderBase *reader);
118 
119  /// Close the writer
120  void Close();
121 
122  /// Get the chunk size (number of frames to write in each chunk)
123  int64_t GetChunkSize() { return chunk_size; };
124 
125  /// Determine if writer is open or closed
126  bool IsOpen() { return is_open; };
127 
128  /// Open writer
129  void Open();
130 
131  /// @brief Set the chunk size (number of frames to write in each chunk)
132  /// @param new_size The number of frames to write in this chunk file
133  void SetChunkSize(int64_t new_size) { chunk_size = new_size; };
134 
135  /// @brief Add a frame to the stack waiting to be encoded.
136  /// @param frame The openshot::Frame object that needs to be written to this chunk file.
137  void WriteFrame(std::shared_ptr<openshot::Frame> frame);
138 
139  /// @brief Write a block of frames from a reader
140  /// @param start The starting frame number to write (of the reader passed into the constructor)
141  /// @param length The number of frames to write (of the reader passed into the constructor)
142  void WriteFrame(int64_t start, int64_t length);
143 
144  /// @brief Write a block of frames from a reader
145  /// @param reader The reader containing the frames you need
146  /// @param start The starting frame number to write
147  /// @param length The number of frames to write
148  void WriteFrame(openshot::ReaderBase* reader, int64_t start, int64_t length);
149 
150  };
151 
152 }
153 
154 #endif
Header file for ReaderBase class.
ChunkWriter(std::string path, openshot::ReaderBase *reader)
Constructor for ChunkWriter. Throws one of the following exceptions.
Definition: ChunkWriter.cpp:35
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition: FFmpegWriter.h:143
This class takes any reader and generates a special type of video file, built with chunks of small vi...
Definition: ChunkWriter.h:81
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ChunkWriter.cpp:80
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
Header file for FFmpegWriter class.
Header file for CacheMemory class.
bool IsOpen()
Determine if writer is open or closed.
Definition: ChunkWriter.h:126
Header file for all Exception classes.
Header file for WriterBase class.
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:87
Header file for JSON class.
void SetChunkSize(int64_t new_size)
Set the chunk size (number of frames to write in each chunk)
Definition: ChunkWriter.h:133
This namespace is the default namespace for all code in the openshot library.
int64_t GetChunkSize()
Get the chunk size (number of frames to write in each chunk)
Definition: ChunkWriter.h:123
void Open()
Open writer.
void Close()
Close the writer.