从FLV流在C#中提取音频 [英] Extract Audio from FLV stream in C#

查看:217
本文介绍了从FLV流在C#中提取音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C#中的FLV流中提取音频流。我在谷歌搜索,我发现 FLVExtract ,但它仅支持从FLV文件中提取,而不是从流。

I'd like to extract audio stream from a FLV stream in C#. I searched in Google and I found FLVExtract, but it supports only extracting from FLV files, and not from streams.

我怎样才能做到这一点?

How can I do this?

推荐答案

我没找到任何东西,所以我不得不把它写自己。这是非常快,它的伟大的工作。下面的代码:

I didn't find anything, so I had to write it myself. It is very fast and it's working great. Here's the code:

    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;
        }
    }

这篇关于从FLV流在C#中提取音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆