2009-11-07 8 views
4

Je voudrais extraire le flux audio d'un flux FLV en C#. J'ai cherché dans Google et j'ai trouvé FLVExtract, mais il ne supporte que l'extraction de fichiers FLV, et non de flux.Extrait audio du flux FLV en C#

Comment est-ce que je peux faire ceci?

+0

Quel est le format de l'audio? mp3? – Charles

Répondre

18

Je n'ai rien trouvé, donc j'ai dû l'écrire moi-même. C'est très rapide et ça fonctionne très bien. Voici le 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; 
     } 
    } 
+0

J'ai une vidéo FLV qui a un flux audio mais ça ne marche pas pour ça. – greatmajestics

+0

Peux-tu me parler de ça ... Alon Gubkin. Le code fourni ci-dessus ne fonctionne pas. – greatmajestics