forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathVideoFileReader.h
242 lines (217 loc) · 6.16 KB
/
VideoFileReader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// AForge FFMPEG Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2009-2011
// contacts@aforgenet.com
//
#pragma once
using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;
using namespace AForge::Video;
namespace AForge { namespace Video { namespace FFMPEG
{
ref struct ReaderPrivateData;
/// <summary>
/// Class for reading video files utilizing FFmpeg library.
/// </summary>
///
/// <remarks><para>The class allows to read video files using <a href="http://www.ffmpeg.org/">FFmpeg</a> library.</para>
///
/// <para><note>Make sure you have <b>FFmpeg</b> binaries (DLLs) in the output folder of your application in order
/// to use this class successfully. <b>FFmpeg</b> binaries can be found in Externals folder provided with AForge.NET
/// framework's distribution.</note></para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create instance of video reader
/// VideoFileReader reader = new VideoFileReader( );
/// // open video file
/// reader.Open( "test.avi" );
/// // check some of its attributes
/// Console.WriteLine( "width: " + reader.Width );
/// Console.WriteLine( "height: " + reader.Height );
/// Console.WriteLine( "fps: " + reader.FrameRate );
/// Console.WriteLine( "codec: " + reader.CodecName );
/// // read 100 video frames out of it
/// for ( int i = 0; i < 100; i++ )
/// {
/// Bitmap videoFrame = reader.ReadVideoFrame( );
/// // process the frame somehow
/// // ...
///
/// // dispose the frame when it is no longer required
/// videoFrame.Dispose( );
/// }
/// reader.Close( );
/// </code>
/// </remarks>
///
public ref class VideoFileReader : IDisposable
{
public:
/// <summary>
/// Frame width of the opened video file.
/// </summary>
///
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
///
property int Width
{
int get( )
{
CheckIfVideoFileIsOpen( );
return m_width;
}
}
/// <summary>
/// Frame height of the opened video file.
/// </summary>
///
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
///
property int Height
{
int get( )
{
CheckIfVideoFileIsOpen( );
return m_height;
}
}
/// <summary>
/// Frame rate of the opened video file.
/// </summary>
///
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
///
property int FrameRate
{
int get( )
{
CheckIfVideoFileIsOpen( );
return m_frameRate;
}
}
/// <summary>
/// Number of video frames in the opened video file.
/// </summary>
///
/// <remarks><para><note><b>Warning</b>: some video file formats may report different value
/// from the actual number of video frames in the file (subject to fix/investigate).</note></para>
/// </remarks>
///
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
///
property Int64 FrameCount
{
Int64 get( )
{
CheckIfVideoFileIsOpen( );
return m_framesCount;
}
}
/// <summary>
/// Name of codec used for encoding the opened video file.
/// </summary>
///
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
///
property String^ CodecName
{
String^ get( )
{
CheckIfVideoFileIsOpen( );
return m_codecName;
}
}
/// <summary>
/// The property specifies if a video file is opened or not by this instance of the class.
/// </summary>
property bool IsOpen
{
bool get ( )
{
return ( data != nullptr );
}
}
protected:
/// <summary>
/// Object's finalizer.
/// </summary>
///
!VideoFileReader( )
{
Close( );
}
public:
/// <summary>
/// Initializes a new instance of the <see cref="VideoFileReader"/> class.
/// </summary>
///
VideoFileReader( void );
/// <summary>
/// Disposes the object and frees its resources.
/// </summary>
///
~VideoFileReader( )
{
this->!VideoFileReader( );
disposed = true;
}
/// <summary>
/// Open video file with the specified name.
/// </summary>
///
/// <param name="fileName">Video file name to open.</param>
///
/// <exception cref="System::IO::IOException">Cannot open video file with the specified name.</exception>
/// <exception cref="VideoException">A error occurred while opening the video file. See exception message.</exception>
///
void Open( String^ fileName );
/// <summary>
/// Read next video frame of the currently opened video file.
/// </summary>
///
/// <returns>Returns next video frame of the opened file or <see langword="null"/> if end of
/// file was reached. The returned video frame has 24 bpp color format.</returns>
///
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
/// <exception cref="VideoException">A error occurred while reading next video frame. See exception message.</exception>
///
Bitmap^ ReadVideoFrame( );
/// <summary>
/// Close currently opened video file if any.
/// </summary>
///
void Close( );
private:
int m_width;
int m_height;
int m_frameRate;
String^ m_codecName;
Int64 m_framesCount;
private:
Bitmap^ DecodeVideoFrame( );
// Checks if video file was opened
void CheckIfVideoFileIsOpen( )
{
if ( data == nullptr )
{
throw gcnew System::IO::IOException( "Video file is not open, so can not access its properties." );
}
}
// Check if the object was already disposed
void CheckIfDisposed( )
{
if ( disposed )
{
throw gcnew System::ObjectDisposedException( "The object was already disposed." );
}
}
private:
// private data of the class
ReaderPrivateData^ data;
bool disposed;
};
} } }