Introduction
In this article, we’ll learn how to add multiple FLV movies in a single SWF file and then extract all or any of those movies using FLVExtractor class.
Explanation
FLVExtractor class allows you to extract FLV movies from an SWF file using Extract method. We need to pass the video stream id in order to extract a particular movie. This id is assigned while adding the FLV movie in the SWF file. To have a better understanding of the whole process, we’ll see the code snippets for adding FLV videos in the SWF file and extracting those videos.
Adding multiple FLV movies
In order to add an FLV movie in an SWF file, we’ll use FLVUnpacker class. This class provides a method named Unpack, which is used to unpack an FLV to a flash container. While adding a new movie to the SWF file, we need to provide two parameters i.e. video stream id and depth. So, we can add multiple FLV movies with different IDs and at different depth. However, the Unpack overload without any parameters would add the movie with default ID i.e. 1.
[C#]
//create flash container to hold movies
FlashContainer flash = null;
//create unpacker object to add movies
IUnpacker unpacker = null;
//Open input stream with FLV data
using (Stream inputStream = new FileStream("simpleMovie.flv", FileMode.Open))
{
//create FLVUnpacker object
unpacker = new FLVUnpacker(inputStream);
//unpack FLV to flash container
flash = unpacker.Unpack(); //use default stream id =1
}
//load second FLV movie
using (Stream inputStream2 = new FileStream("longMovie.flv", FileMode.Open))
{
//create FLVUnpackager object
unpacker = new FLVUnpacker(inputStream2);
//unpack FLV to flash contianer; set different stream id and depth
flash.AddFlash(unpacker.Unpack(2, 2));
}
//load third FLV movie
using (Stream inputStream3 = new FileStream("simpleMovie2.flv", FileMode.Open))
{
//create FLVUnpackager object
unpacker = new FLVUnpacker(inputStream3);
//unpack FLV to flash contianer; set different stream id and depth
flash.AddFlash(unpacker.Unpack(3, 3));
}
//write SWF output file
flash.Write("SWFContainingMultipleFLV.swf");
[VB.NET]
'create flash container to hold movies
Dim flash As FlashContainer = Nothing
'create unpacker object to add movies
Dim unpacker As IUnpacker = Nothing
'Open input stream with FLV data
Using inputStream As Stream = New FileStream("simpleMovie.flv", FileMode.Open)
'create FLVUnpacker object
unpacker = New FLVUnpacker(inputStream)
'unpack FLV to flash container
flash = unpacker.Unpack() 'use default stream id =1
End Using
'load second FLV movie
Using inputStream2 As Stream = New FileStream("longMovie.flv", FileMode.Open)
'create FLVUnpackager object
unpacker = New FLVUnpacker(inputStream2)
'unpack FLV to flash contianer; set different stream id and depth
flash.AddFlash(unpacker.Unpack(2, 2))
End Using
'load third FLV movie
Using inputStream3 As Stream = New FileStream("simpleMovie2.flv", FileMode.Open)
'create FLVUnpackager object
unpacker = New FLVUnpacker(inputStream3)
'unpack FLV to flash contianer; set different stream id and depth
flash.AddFlash(unpacker.Unpack(3, 3))
End Using
'write SWF output file
flash.Write("SWFContainingMultipleFLV.swf")
Extracting Multiple FLV movies
Once the movies are added in the SWF file, we can extract these movies using FLVExtractor. We can use Extract method to achieve this goal. This method requires two parameters i.e. output stream and video stream id.
[C#]
//open SWF flash file and create flash container
FlashContainer flash = new FlashContainer("SWFContainingMultipleFLV.swf");
//create FLVExtractor object to extract FLV movies
FLVExtractor packer = new FLVExtractor(flash);
//open output file stream for first movie
using (Stream outputStream = new FileStream("FLVExtractedFromSWF.flv", FileMode.Create))
{
//created FLV movie and write it to the stream; get video by id
packer.Extract(outputStream, 1);
}
//open output file stream for second movie
using (Stream outputStream = new FileStream("FLVExtractedFromSWF2.flv", FileMode.Create))
{
//created FLV movie and write it to the stream; get video by id
packer.Extract(outputStream, 2);
}
//open output file stream for third movie
using (Stream outputStream = new FileStream("FLVExtractedFromSWF3.flv", FileMode.Create))
{
//created FLV movie and write it to the stream; get video by id
packer.Extract(outputStream, 3);
}
[VB.NET]
'open SWF flash file and create flash container
Dim flash As New FlashContainer("SWFContainingMultipleFLV.swf")
'create FLVExtractor object to extract FLV movies
Dim packer As New FLVExtractor(flash)
'open output file stream for first movie
Using outputStream As Stream = New FileStream("FLVExtractedFromSWF.flv", FileMode.Create)
'created FLV movie and write it to the stream; get video by id
packer.Extract(outputStream, 1)
End Using
'open output file stream for second movie
Using outputStream As Stream = New FileStream("FLVExtractedFromSWF2.flv", FileMode.Create)
'created FLV movie and write it to the stream; get video by id
packer.Extract(outputStream, 2)
End Using
'open output file stream for third movie
Using outputStream As Stream = New FileStream("FLVExtractedFromSWF3.flv", FileMode.Create)
'created FLV movie and write it to the stream; get video by id
packer.Extract(outputStream, 3)
End Using
Conclusion
In this article, we have seen the use of FLVExtractor class to extract multiple FLV movies. We have also learned to add multiple FLV movies in an SWF file.