-
Notifications
You must be signed in to change notification settings - Fork 1
/
EditableVideoStream.cs
119 lines (98 loc) · 4.9 KB
/
EditableVideoStream.cs
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
/* This class has been written by
* Corinna John (Hannover, Germany)
*
* You may do with this code whatever you like,
* except selling it or claiming any rights/ownership.
*
* Please send me a little feedback about what you're
* using this code for and what changes you'd like to
* see in later versions. (And please excuse my bad english.)
*
* WARNING: This is experimental code.
* Please do not expect "Release Quality".
* */
#region Using directives
using System;
using System.Text;
using System.Runtime.InteropServices;
#endregion
namespace AviFile {
public class EditableVideoStream : VideoStream {
private IntPtr editableStream = IntPtr.Zero;
/// <summary>Pointer to the unmanaged AVI stream</summary>
internal override IntPtr StreamPointer {
get { return editableStream; }
}
/// <summary>Create an editable stream from an uneditable stream</summary>
/// <param name="stream">uneditable stream</param>
public EditableVideoStream(VideoStream stream) : base(stream.FrameSize, stream.FrameRate, stream.Width, stream.Height, stream.CountBitsPerPixel, stream.CountFrames, stream.CompressOptions, stream.WriteCompressed) {
Avi.AVIFileInit();
int result = Avi.CreateEditableStream(ref editableStream, stream.StreamPointer);
if (result != 0) {
throw new Exception("Exception in CreateEditableStream: " + result.ToString());
}
SetInfo(stream.StreamInfo);
}
/// <summary>Close the stream</summary>
public override void Close() {
base.Close();
Avi.AVIFileExit();
}
/// <summary>Copy a number of frames into a temporary stream</summary>
/// <param name="start">First frame to copy</param>
/// <param name="length">Count of frames to copy</param>
/// <returns>Pointer to the unmanaged temporary stream</returns>
public IntPtr Copy(int start, int length) {
IntPtr copyPointer = IntPtr.Zero;
int result = Avi.EditStreamCopy(editableStream, ref start, ref length, ref copyPointer);
if (result != 0) {
throw new Exception("Exception in Copy: " + result.ToString());
}
return copyPointer;
}
/// <summary>Move a number of frames into a temporary stream</summary>
/// <param name="start">First frame to cut</param>
/// <param name="length">Count of frames to cut</param>
/// <returns>Pointer to the unmanaged temporary stream</returns>
public IntPtr Cut(int start, int length) {
IntPtr copyPointer = IntPtr.Zero;
int result = Avi.EditStreamCut(editableStream, ref start, ref length, ref copyPointer);
if (result != 0) {
throw new Exception("Exception in Cut: " + result.ToString());
}
countFrames -= length;
return copyPointer;
}
/// <summary>Paste a number of frames from another video stream into this stream</summary>
/// <param name="sourceStream">Stream to copy from</param>
/// <param name="copyPosition">Index of the first frame to copy</param>
/// <param name="pastePosition">Where to paste the copied frames</param>
/// <param name="length">Count of frames to paste</param>
public void Paste(VideoStream sourceStream, int copyPosition, int pastePosition, int length) {
Paste(sourceStream.StreamPointer, copyPosition, pastePosition, length);
}
/// <summary>Paste a number of frames from another video stream into this stream</summary>
/// <param name="sourceStream">Pointer to the unmanaged stream to copy from</param>
/// <param name="copyPosition">Index of the first frame to copy</param>
/// <param name="pastePosition">Where to paste the copied frames</param>
/// <param name="length">Count of frames to paste</param>
public void Paste(IntPtr sourceStream, int copyPosition, int pastePosition, int length) {
int pastedLength = 0;
int result = Avi.EditStreamPaste(editableStream, ref pastePosition, ref pastedLength, sourceStream, copyPosition, length);
if (result != 0) {
throw new Exception("Exception in Paste: " + result.ToString());
}
countFrames += pastedLength;
}
/// <summary>Change the AviStreamInfo values and update the frame rate</summary>
/// <param name="info"></param>
public void SetInfo(Avi.AVISTREAMINFO info) {
int result = Avi.EditStreamSetInfo(editableStream, ref info, Marshal.SizeOf(info));
if (result != 0) {
throw new Exception("Exception in SetInfo: " + result.ToString());
}
frameRate = info.dwRate / info.dwScale;
}
}
}