BililiveRecorder/BililiveRecorder.Cli/Program.cs

123 lines
4.2 KiB
C#
Raw Normal View History

2020-12-21 19:08:44 +08:00
using System;
2021-01-04 16:24:36 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Autofac;
using BililiveRecorder.Core;
2021-01-04 16:24:36 +08:00
using BililiveRecorder.Core.Config.V2;
using BililiveRecorder.FlvProcessor;
using CommandLine;
namespace BililiveRecorder.Cli
{
2020-12-21 19:08:44 +08:00
internal class Program
{
2021-01-04 16:24:36 +08:00
private static int Main()
=> Parser.Default
.ParseArguments<CmdVerbConfigMode, CmdVerbPortableMode>(Environment.GetCommandLineArgs())
.MapResult<CmdVerbConfigMode, CmdVerbPortableMode, int>(RunConfigMode, RunPortableMode, err => 1);
2021-01-04 16:24:36 +08:00
private static int RunConfigMode(CmdVerbConfigMode opts)
{
2021-01-04 16:24:36 +08:00
var container = CreateBuilder().Build();
var rootScope = container.BeginLifetimeScope("recorder_root");
var semaphore = new SemaphoreSlim(0, 1);
var recorder = rootScope.Resolve<IRecorder>();
2021-01-01 14:52:53 +08:00
2021-01-04 16:24:36 +08:00
ConsoleCancelEventHandler p = null!;
p = (sender, e) =>
{
Console.CancelKeyPress -= p;
e.Cancel = true;
recorder.Dispose();
semaphore.Release();
};
Console.CancelKeyPress += p;
2020-06-21 21:26:51 +08:00
2021-01-04 16:24:36 +08:00
if (!recorder.Initialize(opts.WorkDirectory))
{
Console.WriteLine("Initialize Error");
2021-01-04 16:24:36 +08:00
return -1;
}
2020-06-21 21:26:51 +08:00
2021-01-04 16:24:36 +08:00
semaphore.Wait();
return 0;
}
2021-01-04 16:24:36 +08:00
private static int RunPortableMode(CmdVerbPortableMode opts)
2020-06-21 21:26:51 +08:00
{
2021-01-04 16:24:36 +08:00
var container = CreateBuilder().Build();
var rootScope = container.BeginLifetimeScope("recorder_root");
var semaphore = new SemaphoreSlim(0, 1);
var recorder = rootScope.Resolve<IRecorder>();
var config = new ConfigV2()
2020-06-21 21:26:51 +08:00
{
2021-01-04 16:24:36 +08:00
DisableConfigSave = true,
};
if (!string.IsNullOrWhiteSpace(opts.Cookie))
config.Global.Cookie = opts.Cookie;
if (!string.IsNullOrWhiteSpace(opts.LiveApiHost))
config.Global.LiveApiHost = opts.LiveApiHost;
if (!string.IsNullOrWhiteSpace(opts.RecordFilenameFormat))
config.Global.RecordFilenameFormat = opts.RecordFilenameFormat;
config.Global.WorkDirectory = opts.OutputDirectory;
config.Rooms = opts.RoomIds.Select(x => new RoomConfig { RoomId = x, AutoRecord = true }).ToList();
ConsoleCancelEventHandler p = null!;
p = (sender, e) =>
{
Console.CancelKeyPress -= p;
e.Cancel = true;
recorder.Dispose();
semaphore.Release();
};
Console.CancelKeyPress += p;
if (!((Recorder)recorder).InitializeWithConfig(config))
{
Console.WriteLine("Initialize Error");
return -1;
2020-06-21 21:26:51 +08:00
}
2021-01-04 16:24:36 +08:00
semaphore.Wait();
return 0;
2020-06-21 21:26:51 +08:00
}
2020-12-21 04:13:49 +08:00
2021-01-04 16:24:36 +08:00
private static ContainerBuilder CreateBuilder()
2020-12-21 04:13:49 +08:00
{
2021-01-04 16:24:36 +08:00
var builder = new ContainerBuilder();
builder.RegisterModule<FlvProcessorModule>();
builder.RegisterModule<CoreModule>();
return builder;
2020-12-21 04:13:49 +08:00
}
2021-01-04 16:24:36 +08:00
}
[Verb("portable", HelpText = "Run recorder. Ignore config file in output directory")]
public class CmdVerbPortableMode
{
[Option('o', "dir", Default = ".", HelpText = "Output directory", Required = false)]
public string OutputDirectory { get; set; } = ".";
2020-12-21 04:13:49 +08:00
[Option("cookie", HelpText = "Provide custom cookies", Required = false)]
2021-01-04 16:24:36 +08:00
public string? Cookie { get; set; }
2020-12-21 04:13:49 +08:00
[Option("live_api_host", HelpText = "Use custom api host", Required = false)]
2021-01-04 16:24:36 +08:00
public string? LiveApiHost { get; set; }
2020-12-21 04:13:49 +08:00
[Option("record_filename_format", HelpText = "Recording name format", Required = false)]
2021-01-04 16:24:36 +08:00
public string? RecordFilenameFormat { get; set; }
[Value(0, Min = 1, Required = true, HelpText = "List of room id")]
public IEnumerable<int> RoomIds { get; set; } = Enumerable.Empty<int>();
}
[Verb("run", HelpText = "Run recorder with config file")]
public class CmdVerbConfigMode
{
[Value(0, HelpText = "Target directory", Required = true)]
public string WorkDirectory { get; set; } = string.Empty;
}
2020-12-21 19:08:44 +08:00
}