コード例 #1
0
        public void FFmpegFlvRecordTest()
        {
            var recorder = new BilibiliLiveRecorder(3);

            Assert.ThrowsExceptionAsync <ArgumentException>(async() => { await recorder.GetLiveUrl(); }).Wait();

            recorder.Refresh().Wait();

            var urls      = Task.Run(recorder.GetLiveUrl).Result.ToArray();
            var urlNumber = urls.Length;

            Assert.IsTrue(urlNumber > 1);

            var          url  = urls[0];
            const string path = @"D:\Downloads\test_ffmpeg.flv";


            var cts = new CancellationTokenSource();

            Task.Run(async() => { await MyTask.FFmpegRecordTask(url, path, cts); }, cts.Token);
            Thread.Sleep(10000);
            cts.Cancel();
        }
コード例 #2
0
        private async Task RecordTask(Rooms room, CancellationTokenSource cts)
        {
            var rootPath      = RecordDirectory.Text;
            var n             = GetStreamUrlIndex();//0,1,2,3
            var httpWay       = RecordWay2.Checked;
            var isConvert2Mp4 = AutoConvert.Checked;

            var dir = Path.Combine(rootPath, $@"{room.RealRoomID}");

            if (!Directory.Exists(dir))
            {
                var dirInfo = Directory.CreateDirectory(dir);
                if (!dirInfo.Exists)
                {
                    throw new Exception($@"{room.RealRoomID}:存储目录创建失败");
                }
            }

            var iEnumerableUrls = await room.GetLiveUrl();

            var urls = iEnumerableUrls.ToArray();

            if (urls.Length == 0)
            {
                throw new Exception($@"{room.RealRoomID}:直播流获取失败");
            }

            if (n >= urls.Length)
            {
                n = 0;
            }

            var url = urls[n];

            Logging.Info($@"{room.RealRoomID}:{url}");

            try
            {
                var isConnected = await room.TestHttpOk(url);

                if (!isConnected)
                {
                    Logging.Error($@"{room.RealRoomID}:直播流错误...");
                    return;
                }
            }
            catch (TaskCanceledException)
            {
                Logging.Error($@"{room.RealRoomID}:直播流检测超时...");
                return;
            }

            Logging.Info($@"{room.RealRoomID}:录制开始");

            var path = Path.Combine(dir, $@"{DateTime.Now:yyyyMMdd_HHmmss}.flv");

            room.IsRecording = true;
            if (httpWay)
            {
                await MyTask.HttpDownLoadRecordTask(url, path, cts);
            }
            else
            {
                await MyTask.FFmpegRecordTask(url, path, cts);
            }

            Logging.Info($@"{room.RealRoomID}:录制结束=>{path}");

            if (File.Exists(path))
            {
                var size = Util.GetFileSize(path);
                if (size < RecordFileMinSize)
                {
                    File.Delete(path);
                    Logging.Info($@"{room.RealRoomID}:因文件过小删除:{path}");
                }
                else if (isConvert2Mp4)
                {
                    Logging.Info($@"{room.RealRoomID}:开始转封装:{path}");
                    // 异步执行
                    SplitTaskFile(path, OutputVideoPath.Text, GetVideoConvertConfig(), false).Start();
                }
            }
        }