Пример #1
0
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();

            while (true)
            {
                stopwatch.Start();

                FlvUtil.FlvMerge(new string[] { "1.flv", "1.flv", "1.flv", "1.flv" }, "o.flv");

                stopwatch.Stop();

                FlvFile.Tag metadataTag;
                using (FlvFile flvFile = new FlvFile("o.flv"))
                {
                    metadataTag = flvFile.ReadTag();
                }
                Console.WriteLine("Metadata: \r\n");
                Console.WriteLine(metadataTag);
                Console.WriteLine("\r\n");
                Console.WriteLine("Timer: {0} ms", stopwatch.ElapsedMilliseconds);
                stopwatch.Reset();
                Console.ReadLine();
            }
        }
Пример #2
0
        public static void FlvMerge(string[] inputs, string output)
        {
            MetadataTagCreater.Metadata metadata = new MetadataTagCreater.Metadata(creator, matadatacreator);
            uint timestampOffset = 0;

            foreach (string input in inputs)
            {
                using (FlvFile flv = new FlvFile(input))
                {
                    while (true)
                    {
                        FlvFile.Tag tag = flv.ReadTag();
                        if (tag == null)
                        {
                            break;
                        }
                        if (tag.Type == FlvFile.Tag.TagType.Script)
                        {
                            FlvFile.Tag.ScriptTag        scriptTag = (FlvFile.Tag.ScriptTag)tag;
                            FlvFile.Tag.ScriptTag.String tagName   = (FlvFile.Tag.ScriptTag.String)scriptTag.Name;
                            if (tagName.Value == "onMetaData")
                            {
                                FlvFile.Tag.ScriptTag.EcmaArray tagValue = (FlvFile.Tag.ScriptTag.EcmaArray)scriptTag.Value;
                                metadata.hasKeyframes   |= ((FlvFile.Tag.ScriptTag.Boolean)tagValue.Items["hasKeyframes"]).Value;
                                metadata.hasVideo       |= ((FlvFile.Tag.ScriptTag.Boolean)tagValue.Items["hasVideo"]).Value;
                                metadata.hasAudio       |= ((FlvFile.Tag.ScriptTag.Boolean)tagValue.Items["hasAudio"]).Value;
                                metadata.hasMetadata    |= ((FlvFile.Tag.ScriptTag.Boolean)tagValue.Items["hasMetadata"]).Value;
                                metadata.canSeekToEnd   |= ((FlvFile.Tag.ScriptTag.Boolean)tagValue.Items["canSeekToEnd"]).Value;
                                metadata.duration       += ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["duration"]).Value;
                                metadata.videocodecid    = ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["videocodecid"]).Value;
                                metadata.width           = ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["width"]).Value;
                                metadata.height          = ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["height"]).Value;
                                metadata.audiocodecid    = ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["audiocodecid"]).Value;
                                metadata.audiosamplerate = ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["audiosamplerate"]).Value;
                                metadata.audiosamplesize = ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["audiosamplesize"]).Value;
                                metadata.stereo         |= ((FlvFile.Tag.ScriptTag.Boolean)tagValue.Items["stereo"]).Value;
                            }
                        }
                        else
                        {
                            metadata.datasize     += tag.TagLengthWithPts;
                            metadata.lasttimestamp = tag.Header.Timestamp + timestampOffset;
                            if (tag.Type == FlvFile.Tag.TagType.Video)
                            {
                                FlvFile.Tag.VideoTag videoTag = (FlvFile.Tag.VideoTag)tag;
                                metadata.videosize += videoTag.TagLength;
                                metadata.framerate++;
                                metadata.videodatarate += videoTag.BodyLength;
                                if (videoTag.FrameType == FlvFile.Tag.VideoTag.FrameTypes.KeyFrame)
                                {
                                    metadata.lastkeyframetimestamp = videoTag.Header.Timestamp + timestampOffset;
                                    metadata.lastkeyframelocation  = (uint)metadata.filesize;
                                    metadata.keyframesFilepositions.Add((uint)metadata.filesize);
                                    metadata.keyframesTimes.Add((double)(videoTag.Header.Timestamp + timestampOffset) / 1000);
                                }
                            }
                            else if (tag.Type == FlvFile.Tag.TagType.Audio)
                            {
                                FlvFile.Tag.AudioTag audioTag = (FlvFile.Tag.AudioTag)tag;
                                metadata.audiosize     += audioTag.TagLength;
                                metadata.audiodatarate += audioTag.BodyLength;
                            }
                            metadata.filesize += tag.TagLengthWithPts;
                        }
                    }
                }
                timestampOffset = (uint)(metadata.duration * 1000);
            }
            metadata.framerate             /= metadata.duration;
            metadata.videodatarate         /= metadata.duration * 1024 / 8;
            metadata.audiodatarate         /= metadata.duration * 1024 / 8;
            metadata.lasttimestamp         /= 1000;
            metadata.lastkeyframetimestamp /= 1000;

            FlvFile.Tag.ScriptTag metadataTag = MetadataTagCreater.CreatMetadataTag(metadata);

            FlvFile.FlvHeader flvHeader = new FlvFile.FlvHeader(FlvFile.FlvHeader.StreamFlag.VideoAndAudio);

            timestampOffset = 0;
            using (FileStream fileStream = new FileStream(output, FileMode.Create))
            {
                fileStream.Write(flvHeader.HeaderBytes, 0, flvHeader.HeaderBytes.Length);
                byte[] metadataBytes = metadataTag.TagBytesWithPts;
                fileStream.Write(metadataBytes, 0, metadataBytes.Length);
                double duration = 0;
                foreach (string input in inputs)
                {
                    using (FlvFile flv = new FlvFile(input))
                    {
                        while (true)
                        {
                            FlvFile.Tag tag = flv.ReadTag();
                            if (tag == null)
                            {
                                break;
                            }
                            if (tag.Type == FlvFile.Tag.TagType.Script)
                            {
                                FlvFile.Tag.ScriptTag        scriptTag = (FlvFile.Tag.ScriptTag)tag;
                                FlvFile.Tag.ScriptTag.String tagName   = (FlvFile.Tag.ScriptTag.String)scriptTag.Name;
                                if (tagName.Value == "onMetaData")
                                {
                                    FlvFile.Tag.ScriptTag.EcmaArray tagValue = (FlvFile.Tag.ScriptTag.EcmaArray)scriptTag.Value;
                                    duration += ((FlvFile.Tag.ScriptTag.Number)tagValue.Items["duration"]).Value;
                                }
                            }
                            else
                            {
                                tag.Header.Timestamp += timestampOffset;
                                byte[] buffer = tag.TagBytesWithPts;
                                fileStream.Write(buffer, 0, buffer.Length);
                            }
                        }
                    }
                    timestampOffset = (uint)(duration * 1000);
                }
            }
        }