BililiveRecorder/BililiveRecorder.Cli/Program.cs

112 lines
3.6 KiB
C#
Raw Normal View History

using System;
2020-06-21 21:26:51 +08:00
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using BililiveRecorder.Core;
2020-06-21 21:26:51 +08:00
using BililiveRecorder.Core.Config;
using BililiveRecorder.FlvProcessor;
using CommandLine;
2020-06-21 19:36:43 +08:00
using NLog;
namespace BililiveRecorder.Cli
{
class Program
{
private static IContainer Container { get; set; }
private static ILifetimeScope RootScope { get; set; }
private static IRecorder Recorder { get; set; }
2020-06-21 19:36:43 +08:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] _)
{
var builder = new ContainerBuilder();
builder.RegisterModule<FlvProcessorModule>();
builder.RegisterModule<CoreModule>();
2020-06-21 21:26:51 +08:00
builder.RegisterType<CommandConfigV1>().As<ConfigV1>().InstancePerMatchingLifetimeScope("recorder_root");
Container = builder.Build();
2020-06-21 21:26:51 +08:00
RootScope = Container.BeginLifetimeScope("recorder_root");
Recorder = RootScope.Resolve<IRecorder>();
if (!Recorder.Initialize(System.IO.Directory.GetCurrentDirectory()))
{
Console.WriteLine("Initialize Error");
return;
}
2020-06-21 21:26:51 +08:00
Parser.Default
2020-06-21 21:26:51 +08:00
.ParseArguments<CommandConfigV1>(() => (CommandConfigV1)Recorder.Config, Environment.GetCommandLineArgs())
.WithParsed(Run);
}
2020-06-21 21:26:51 +08:00
private static void Run(ConfigV1 option)
{
2020-06-21 21:26:51 +08:00
foreach (var room in option.RoomList)
{
2020-06-21 21:26:51 +08:00
if (Recorder.Where(r => r.RoomId == room.Roomid).Count() == 0)
2020-06-21 19:36:43 +08:00
{
2020-06-21 21:26:51 +08:00
Recorder.AddRoom(room.Roomid);
2020-06-21 19:36:43 +08:00
}
}
2020-06-21 19:36:43 +08:00
logger.Info("开始录播");
2020-06-21 19:36:43 +08:00
Task.WhenAll(Recorder.Select(x => Task.Run(() => x.Start()))).Wait();
Console.CancelKeyPress += (sender, e) =>
{
2020-06-21 19:36:43 +08:00
Task.WhenAll(Recorder.Select(x => Task.Run(() => x.StopRecord()))).Wait();
logger.Info("停止录播");
};
while (true)
{
Thread.Sleep(TimeSpan.FromSeconds(10));
}
}
}
2020-06-21 21:26:51 +08:00
class ConfigV1Metadata
{
[Option('o', "dir", Default = ".", HelpText = "Output directory", Required = false)]
[Utils.DoNotCopyProperty]
public object WorkDirectory { get; set; }
[Option("cookie", HelpText = "Provide custom cookies", Required = false)]
public object Cookie { get; set; }
[Option("avoidtxy", HelpText = "Avoid Tencent Cloud server", Required = false)]
public object AvoidTxy { get; set; }
[Option("live_api_host", HelpText = "Use custom api host", Required = false)]
public object LiveApiHost { get; set; }
[Option("record_filename_format", HelpText = "Recording name format", Required = false)]
public object RecordFilenameFormat { get; set; }
}
[MetadataType(typeof(ConfigV1Metadata))]
class CommandConfigV1 : ConfigV1
{
2020-06-21 21:26:51 +08:00
[Option('i', "id", HelpText = "room id", Required = true)]
[Utils.DoNotCopyProperty]
public string _RoomList
{
set
{
var roomids = value.Split(',');
RoomList.Clear();
foreach (var roomid in roomids)
{
var room = new RoomV1();
room.Roomid = Int32.Parse(roomid);
room.Enabled = false;
RoomList.Add(room);
}
}
}
}
}