public void FinallizeFile()
        {
            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }
                using (var fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite))
                {
                    fs.Write(FlvStreamProcessor.FLV_HEADER_BYTES, 0, FlvStreamProcessor.FLV_HEADER_BYTES.Length);
                    fs.Write(new byte[] { 0, 0, 0, 0, }, 0, 4);

                    double clipDuration = (Tags[Tags.Count - 1].TimeStamp - Tags[0].TimeStamp) / 1000d;
                    Header["duration"]      = clipDuration;
                    Header["lasttimestamp"] = (double)(Tags[Tags.Count - 1].TimeStamp - Tags[0].TimeStamp);

                    var t = funcFlvTag();
                    t.TagType = TagType.DATA;

                    if (Header.ContainsKey("BililiveRecorder"))
                    {
                        // TODO: 更好的写法
                        (Header["BililiveRecorder"] as Dictionary <string, object>)["starttime"] = DateTime.UtcNow - TimeSpan.FromSeconds(clipDuration);
                    }
                    t.Data = Header.ToBytes();
                    t.WriteTo(fs);

                    int offset = Tags[0].TimeStamp;

                    HTags.ForEach(tag => tag.WriteTo(fs));
                    Tags.ForEach(tag => tag.WriteTo(fs, offset));

                    logger.Info("剪辑已保存:{0}", Path.GetFileName(path));

                    fs.Close();
                }
                Tags.Clear();
            }
            catch (IOException ex)
            {
                logger.Warn(ex, "保存剪辑文件时出错");
            }
            catch (Exception ex)
            {
                logger.Error(ex, "保存剪辑文件时出错");
            }
            ClipFinalized?.Invoke(this, new ClipFinalizedArgs()
            {
                ClipProcessor = this
            });
        }
Esempio n. 2
0
        public void FinallizeFile()
        {
            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }
                using (var fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite))
                {
                    fs.Write(FlvStreamProcessor.FLV_HEADER_BYTES, 0, FlvStreamProcessor.FLV_HEADER_BYTES.Length);
                    fs.Write(new byte[] { 0, 0, 0, 0, }, 0, 4);

                    Header.Meta["duration"]      = (Tags[Tags.Count - 1].TimeStamp - Tags[0].TimeStamp) / 1000d;
                    Header.Meta["lasttimestamp"] = (Tags[Tags.Count - 1].TimeStamp - Tags[0].TimeStamp);

                    var t = new FlvTag
                    {
                        TagType = TagType.DATA,
                        Data    = Header.ToBytes()
                    };
                    t.WriteTo(fs);

                    int offset = Tags[0].TimeStamp;

                    HTags.ForEach(tag => tag.WriteTo(fs));
                    Tags.ForEach(tag => tag.WriteTo(fs, offset));

                    logger.Info("剪辑已保存:{0}", Path.GetFileName(path));

                    fs.Close();
                }
                Tags.Clear();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "保存剪辑文件时出错");
            }
            ClipFinalized?.Invoke(this, new ClipFinalizedArgs()
            {
                ClipProcessor = this
            });
        }