gpt4 book ai didi

c# - 在 C# 中从 FLV 流中提取音频

转载 作者:太空狗 更新时间:2023-10-29 23:59:32 26 4
gpt4 key购买 nike

我想在 C# 中从 FLV 流中提取音频流。我在 Google 中搜索并找到了 FLVExtract , 但它只支持从 FLV 文件中提取,而不支持从流中提取。

我该怎么做?

最佳答案

没找到,只好自己写了。它速度非常快,而且效果很好。这是代码:

    protected byte[] ExtractAudio(Stream stream)
{
var reader = new BinaryReader(stream);

// Is stream a Flash Video stream
if (reader.ReadChar() != 'F' || reader.ReadChar() != 'L' || reader.ReadChar() != 'V')
throw new IOException("The file is not a FLV file.");

// Is audio stream exists in the video stream
var version = reader.ReadByte();
var exists = reader.ReadByte();

if ((exists != 5) && (exists != 4))
throw new IOException("No Audio Stream");

reader.ReadInt32(); // data offset of header. ignoring

var output = new List<byte>();

while (true)
{
try
{
reader.ReadInt32(); // PreviousTagSize0 skipping

var tagType = reader.ReadByte();

while (tagType != 8)
{
var skip = ReadNext3Bytes(reader) + 11;
reader.BaseStream.Position += skip;

tagType = reader.ReadByte();
}

var DataSize = ReadNext3Bytes(reader);

reader.ReadInt32(); //skip timestamps
ReadNext3Bytes(reader); // skip streamID
reader.ReadByte(); // skip audio header

for (int i = 0; i < DataSize - 1; i++)
output.Add(reader.ReadByte());
}
catch
{
break;
}
}

return output.ToArray();
}

private long ReadNext3Bytes(BinaryReader reader)
{
try
{
return Math.Abs((reader.ReadByte() & 0xFF) * 256 * 256 + (reader.ReadByte() & 0xFF)
* 256 + (reader.ReadByte() & 0xFF));
}
catch
{
return 0;
}
}

关于c# - 在 C# 中从 FLV 流中提取音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1693656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com