forked from btnkij/qsv2flv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranscoder.cs
182 lines (169 loc) · 6.05 KB
/
Transcoder.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace QSV2FLV
{
public class Transcoder
{
private FileStream temp, qsv;
private FlvWriter flv;
private string qsvPath, outputPath, outputName;
/// <summary>
/// Transcode
/// </summary>
//Constructed Code
public Transcoder(string _qsvPath, string _outputPath)
{
qsvPath = _qsvPath;
outputPath = _outputPath + "\\";
qsv = new FileStream(qsvPath, FileMode.Open);
FileInfo file = new FileInfo(qsvPath);
outputName = file.Name.Substring(0, file.Name.LastIndexOf("."));
file = new FileInfo(outputPath + outputName + ".temp");
if (!Directory.Exists(file.DirectoryName))
Directory.CreateDirectory(file.DirectoryName);
if (file.Exists)
file.Delete();
temp = new FileStream(outputPath + outputName + ".temp",FileMode.CreateNew);
}
public void Dispose()
{
flv.Dispose();
FileInfo file = new FileInfo(outputPath + outputName + ".temp");
file.Delete();
}
public void Transcode()
{
SeekBegin();
SkipMeta();
while (true)
{
try
{
if (ReadNext())
{
byte[] buffer = GetTag();
temp.Write(buffer, 0, buffer.GetLength(0));
}
else
{
SkipMeta();
SeekNextTag();
SeekNextTag();
}
}
catch (Exception e)
{
if (e is EndOfStreamException) break;
else throw;
}
}
qsv.Close();
qsv.Dispose();
temp.Close();
temp.Dispose();
flv = new FlvWriter(outputPath + outputName + ".flv", outputPath + outputName + ".temp");
flv.Parse();
flv.Output();
}
/// <summary>
/// Transcode Tools
/// </summary>
private bool CheckTAG()
{
byte[] buffer = new byte[10];
byte[] tag = Encoding.ASCII.GetBytes("QIYI VIDEO");
qsv.Seek(0L, SeekOrigin.Begin);
qsv.Read(buffer, 0, 10);
return buffer == tag ? true : false;
}
private bool CheckVersion()
{
byte[] buffer = new byte[4];
qsv.Seek(10L, SeekOrigin.Begin);
qsv.Read(buffer, 0, 4);
return BytesToInt(buffer, 0) == 2 ? true : false;
}
private void SeekBegin()
{
long offset = 0L;
int size = 0;
byte[] buffer = new byte[12];
qsv.Seek(74L, SeekOrigin.Begin);
qsv.Read(buffer, 0, 12);
offset = BytesToLong(buffer, 0);
size = BytesToInt(buffer, 8);
qsv.Seek(offset + size, SeekOrigin.Begin);
}
private void SkipMeta()
{
qsv.Position += 0xD;
int len = 0;
while (true)
{
int bit = qsv.ReadByte();
++len;
if (bit == 0x9)
{
byte[] buffer = new byte[4];
qsv.Read(buffer, 0, 4);
int size =
(0xFF & buffer[0]) << 24 | (0xFF & buffer[1]) << 16 | (0xFF & buffer[2]) << 8 | (0xFF & buffer[3]);
if (len == size) break;
else qsv.Position -= 4;
}
}
}
private void SeekNextTag()
{
int dataSize = 0;
byte[] buffer = new byte[4];
qsv.Read(buffer, 0, 4);
dataSize = (0xFF & buffer[1]) << 16 | (0xFF & buffer[2]) << 8 | (0xFF & buffer[3]);
qsv.Seek(dataSize + 11, SeekOrigin.Current);
}
private byte[] GetTag()
{
int dataSize = 0;
byte[] buffer = new byte[4];
qsv.Read(buffer, 0, 4);
qsv.Position -= 4;
dataSize = (0xFF & buffer[1]) << 16 | (0xFF & buffer[2]) << 8 | (0xFF & buffer[3]);
byte[] tag = new byte[dataSize + 15];
qsv.Read(tag, 0, dataSize + 15);
return tag;
}
private bool ReadNext()
{
if (qsv.Position >= qsv.Length - 11)
{
throw new EndOfStreamException();
}
byte[] buffer = new byte[11];
qsv.Read(buffer, 0, 11);
qsv.Position -= 11;
if ((buffer[8] | buffer[9] | buffer[10]) == 0 && (buffer[0] == 0x8 || buffer[0] == 0x9))
{
return true;
}
else
{
return false;
}
}
private int BytesToInt(byte[] paramArrayOfByte, int paramInt)
{
return 0xFF & paramArrayOfByte[paramInt] | (0xFF & paramArrayOfByte[(paramInt + 1)]) << 8 | (0xFF & paramArrayOfByte[(paramInt + 2)]) << 16 | (0xFF & paramArrayOfByte[(paramInt + 3)]) << 24;
}
private long BytesToLong(byte[] paramArrayOfByte, int paramInt)
{
int i = 0xFF & paramArrayOfByte[paramInt] | (0xFF & paramArrayOfByte[(paramInt + 1)]) << 8 | (0xFF & paramArrayOfByte[(paramInt + 2)]) << 16 | (0xFF & paramArrayOfByte[(paramInt + 3)]) << 24;
int j = 0xFF & paramArrayOfByte[(paramInt + 4)] | (0xFF & paramArrayOfByte[(paramInt + 5)]) << 8 | (0xFF & paramArrayOfByte[(paramInt + 6)]) << 16 | (0xFF & paramArrayOfByte[(paramInt + 7)]) << 24;
return i + j;
}
}
}