This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathFlvFile.cs
More file actions
256 lines (207 loc) · 7.3 KB
/
FlvFile.cs
File metadata and controls
256 lines (207 loc) · 7.3 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// ****************************************************************************
//
// FLV Extract
// Copyright (C) 2006-2012 J.D. Purcell (moitah@yahoo.com)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// ****************************************************************************
using System;
using System.IO;
namespace YoutubeExtractor
{
internal class FlvFile : IDisposable
{
private readonly long fileLength;
private readonly string inputPath;
private readonly string outputPath;
private IAudioExtractor audioExtractor;
private long fileOffset;
private FileStream fileStream;
/// <summary>
/// Initializes a new instance of the <see cref="FlvFile"/> class.
/// </summary>
/// <param name="inputPath">The path of the input.</param>
/// <param name="outputPath">The path of the output without extension.</param>
public FlvFile(string inputPath, string outputPath)
{
this.inputPath = inputPath;
this.outputPath = outputPath;
this.fileStream = new FileStream(this.inputPath, FileMode.Open, FileAccess.Read, FileShare.Read, 64 * 1024);
this.fileOffset = 0;
this.fileLength = fileStream.Length;
}
public event EventHandler<ProgressEventArgs> ConversionProgressChanged;
public bool ExtractedAudio { get; private set; }
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <exception cref="AudioExtractionException">The input file is not an FLV file.</exception>
public void ExtractStreams()
{
this.Seek(0);
if (this.ReadUInt32() != 0x464C5601)
{
// not a FLV file
throw new AudioExtractionException("Invalid input file. Impossible to extract audio track.");
}
this.ReadUInt8();
uint dataOffset = this.ReadUInt32();
this.Seek(dataOffset);
this.ReadUInt32();
while (fileOffset < fileLength)
{
if (!ReadTag())
{
break;
}
if (fileLength - fileOffset < 4)
{
break;
}
this.ReadUInt32();
double progress = (this.fileOffset * 1.0 / this.fileLength) * 100;
if (this.ConversionProgressChanged != null)
{
this.ConversionProgressChanged(this, new ProgressEventArgs((int)this.fileOffset, progress));
}
}
this.CloseOutput(false);
}
private void CloseOutput(bool disposing)
{
if (this.audioExtractor != null)
{
if (disposing && this.audioExtractor.VideoPath != null)
{
try
{
File.Delete(this.audioExtractor.VideoPath);
}
catch { }
}
this.audioExtractor.Dispose();
this.audioExtractor = null;
}
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (this.fileStream != null)
{
this.fileStream.Close();
this.fileStream = null;
}
this.CloseOutput(true);
}
}
private IAudioExtractor GetAudioWriter(uint mediaInfo)
{
uint format = mediaInfo >> 4;
switch (format)
{
case 14:
case 2:
return new Mp3AudioExtractor(this.outputPath);
case 10:
return new AacAudioExtractor(this.outputPath);
}
string typeStr;
switch (format)
{
case 1:
typeStr = "ADPCM";
break;
case 6:
case 5:
case 4:
typeStr = "Nellymoser";
break;
default:
typeStr = "format=" + format;
break;
}
throw new AudioExtractionException("Unable to extract audio (" + typeStr + " is unsupported).");
}
private byte[] ReadBytes(int length)
{
var buff = new byte[length];
this.fileStream.Read(buff, 0, length);
this.fileOffset += length;
return buff;
}
private bool ReadTag()
{
if (this.fileLength - this.fileOffset < 11)
return false;
// Read tag header
uint tagType = ReadUInt8();
uint dataSize = ReadUInt24();
uint timeStamp = ReadUInt24();
timeStamp |= this.ReadUInt8() << 24;
this.ReadUInt24();
// Read tag data
if (dataSize == 0)
return true;
if (this.fileLength - this.fileOffset < dataSize)
return false;
uint mediaInfo = this.ReadUInt8();
dataSize -= 1;
byte[] data = this.ReadBytes((int)dataSize);
if (tagType == 0x8)
{
// If we have no audio writer, create one
if (this.audioExtractor == null)
{
this.audioExtractor = this.GetAudioWriter(mediaInfo);
this.ExtractedAudio = this.audioExtractor != null;
}
if (this.audioExtractor == null)
{
throw new InvalidOperationException("No supported audio writer found.");
}
this.audioExtractor.WriteChunk(data, timeStamp);
}
return true;
}
private uint ReadUInt24()
{
var x = new byte[4];
this.fileStream.Read(x, 1, 3);
this.fileOffset += 3;
return BigEndianBitConverter.ToUInt32(x, 0);
}
private uint ReadUInt32()
{
var x = new byte[4];
this.fileStream.Read(x, 0, 4);
this.fileOffset += 4;
return BigEndianBitConverter.ToUInt32(x, 0);
}
private uint ReadUInt8()
{
this.fileOffset += 1;
return (uint)this.fileStream.ReadByte();
}
private void Seek(long offset)
{
this.fileStream.Seek(offset, SeekOrigin.Begin);
this.fileOffset = offset;
}
}
}