OpenShot Library | libopenshot  0.1.9
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef OPENSHOT_CHUNK_WRITER_H
29 #define OPENSHOT_CHUNK_WRITER_H
30 
31 #include "ReaderBase.h"
32 #include "WriterBase.h"
33 #include "FFmpegWriter.h"
34 
35 #include <cmath>
36 #include <ctime>
37 #include <iostream>
38 #include <fstream>
39 #include <omp.h>
40 #include <QtCore/qdir.h>
41 #include <stdio.h>
42 #include <sstream>
43 #include <unistd.h>
44 #include "CacheMemory.h"
45 #include "Exceptions.h"
46 #include "Json.h"
47 
48 
49 using namespace std;
50 
51 namespace 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  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  ReaderBase *local_reader;
90  FFmpegWriter *writer_thumb;
91  FFmpegWriter *writer_preview;
92  FFmpegWriter *writer_final;
93  std::shared_ptr<Frame> last_frame;
94  bool last_frame_needed;
95  string default_extension;
96  string default_vcodec;
97  string default_acodec;
98 
99  /// check for chunk folder
100  void create_folder(string path);
101 
102  /// get a formatted path of a specific chunk
103  string get_chunk_path(int64_t chunk_number, string folder, 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(string path, 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<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(ReaderBase* reader, int64_t start, int64_t length);
148 
149  };
150 
151 }
152 
153 #endif
Header file for ReaderBase class.
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:80
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
Header file for FFmpegWriter class.
Header file for CacheMemory class.
bool IsOpen()
Determine if writer is open or closed.
Definition: ChunkWriter.h:125
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:85
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:132
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:122