Merge file creation into one step

This commit is contained in:
Genteure 2021-05-21 21:30:46 +08:00
parent ba847cf28d
commit 2ceb916a8d
2 changed files with 34 additions and 59 deletions

View File

@ -223,7 +223,7 @@ namespace BililiveRecorder.Core.Recording
{ {
["RecordedBy"] = (ScriptDataString)"BililiveRecorder B站录播姬", ["RecordedBy"] = (ScriptDataString)"BililiveRecorder B站录播姬",
["RecordedFrom"] = (ScriptDataString)(this.streamHost ?? string.Empty), ["RecordedFrom"] = (ScriptDataString)(this.streamHost ?? string.Empty),
["RecorderVersion"] = (ScriptDataString)GitVersionInformation.FullSemVer, ["RecorderVersion"] = (ScriptDataString)GitVersionInformation.InformationalVersion,
["StartTime"] = (ScriptDataDate)now, ["StartTime"] = (ScriptDataDate)now,
["RoomId"] = (ScriptDataString)this.room.RoomConfig.RoomId.ToString(), ["RoomId"] = (ScriptDataString)this.room.RoomConfig.RoomId.ToString(),
["ShortId"] = (ScriptDataString)this.room.ShortId.ToString(), ["ShortId"] = (ScriptDataString)this.room.ShortId.ToString(),

View File

@ -138,15 +138,6 @@ namespace BililiveRecorder.Flv.Writer
} }
} }
private async Task OpenNewFileImpl()
{
this.CloseCurrentFileImpl();
await this.tagWriter.CreateNewFile().ConfigureAwait(false);
this.state = WriterState.BeforeScript;
}
private async Task RewriteScriptTagImpl(double duration, bool updateKeyframes, double keyframeTime, double filePosition) private async Task RewriteScriptTagImpl(double duration, bool updateKeyframes, double keyframeTime, double filePosition)
{ {
if (this.lastScriptBody is null) if (this.lastScriptBody is null)
@ -164,14 +155,30 @@ namespace BililiveRecorder.Flv.Writer
await this.tagWriter.OverwriteMetadata(this.lastScriptBody).ConfigureAwait(false); await this.tagWriter.OverwriteMetadata(this.lastScriptBody).ConfigureAwait(false);
} }
private async Task WriteScriptTagImpl() private async Task OpenNewFileThenWriteHeadersImpl()
{ {
this.CloseCurrentFileImpl();
if (this.nextScriptTag is null) if (this.nextScriptTag is null)
throw new InvalidOperationException("No script tag availible"); throw new InvalidOperationException("No script tag availible");
if (this.nextScriptTag.ScriptData is null) if (this.nextScriptTag.ScriptData is null)
throw new InvalidOperationException("ScriptData is null"); throw new InvalidOperationException("ScriptData is null");
if (!this.allowMissingHeader)
{
if (this.nextVideoHeaderTag is null)
throw new InvalidOperationException("No video header tag availible");
if (this.nextAudioHeaderTag is null)
throw new InvalidOperationException("No audio header tag availible");
}
// Open File
await this.tagWriter.CreateNewFile().ConfigureAwait(false);
// Write Script Tag
{
this.lastScriptBody = this.nextScriptTag.ScriptData; this.lastScriptBody = this.nextScriptTag.ScriptData;
var value = this.lastScriptBody.GetMetadataValue(); var value = this.lastScriptBody.GetMetadataValue();
@ -190,13 +197,9 @@ namespace BililiveRecorder.Flv.Writer
this.BeforeScriptTagWrite?.Invoke(this.lastScriptBody); this.BeforeScriptTagWrite?.Invoke(this.lastScriptBody);
await this.tagWriter.WriteTag(this.nextScriptTag).ConfigureAwait(false); await this.tagWriter.WriteTag(this.nextScriptTag).ConfigureAwait(false);
this.state = WriterState.BeforeHeader;
} }
private async Task WriteHeaderTagsImpl() // Write Header Tag
{
if (this.allowMissingHeader)
{ {
if (this.nextVideoHeaderTag is not null) if (this.nextVideoHeaderTag is not null)
await this.tagWriter.WriteTag(this.nextVideoHeaderTag).ConfigureAwait(false); await this.tagWriter.WriteTag(this.nextVideoHeaderTag).ConfigureAwait(false);
@ -204,17 +207,7 @@ namespace BililiveRecorder.Flv.Writer
if (this.nextAudioHeaderTag is not null) if (this.nextAudioHeaderTag is not null)
await this.tagWriter.WriteTag(this.nextAudioHeaderTag).ConfigureAwait(false); await this.tagWriter.WriteTag(this.nextAudioHeaderTag).ConfigureAwait(false);
} }
else
{
if (this.nextVideoHeaderTag is null)
throw new InvalidOperationException("No video header tag availible");
if (this.nextAudioHeaderTag is null)
throw new InvalidOperationException("No audio header tag availible");
await this.tagWriter.WriteTag(this.nextVideoHeaderTag).ConfigureAwait(false);
await this.tagWriter.WriteTag(this.nextAudioHeaderTag).ConfigureAwait(false);
}
this.state = WriterState.Writing; this.state = WriterState.Writing;
} }
@ -223,16 +216,7 @@ namespace BililiveRecorder.Flv.Writer
switch (this.state) switch (this.state)
{ {
case WriterState.EmptyFileOrNotOpen: case WriterState.EmptyFileOrNotOpen:
await this.OpenNewFileImpl().ConfigureAwait(false); await this.OpenNewFileThenWriteHeadersImpl().ConfigureAwait(false);
await this.WriteScriptTagImpl().ConfigureAwait(false);
await this.WriteHeaderTagsImpl().ConfigureAwait(false);
break;
case WriterState.BeforeScript:
await this.WriteScriptTagImpl().ConfigureAwait(false);
await this.WriteHeaderTagsImpl().ConfigureAwait(false);
break;
case WriterState.BeforeHeader:
await this.WriteHeaderTagsImpl().ConfigureAwait(false);
break; break;
case WriterState.Writing: case WriterState.Writing:
break; break;
@ -257,16 +241,7 @@ namespace BililiveRecorder.Flv.Writer
switch (this.state) switch (this.state)
{ {
case WriterState.EmptyFileOrNotOpen: case WriterState.EmptyFileOrNotOpen:
await this.OpenNewFileImpl().ConfigureAwait(false); await this.OpenNewFileThenWriteHeadersImpl().ConfigureAwait(false);
await this.WriteScriptTagImpl().ConfigureAwait(false);
await this.WriteHeaderTagsImpl().ConfigureAwait(false);
break;
case WriterState.BeforeScript:
await this.WriteScriptTagImpl().ConfigureAwait(false);
await this.WriteHeaderTagsImpl().ConfigureAwait(false);
break;
case WriterState.BeforeHeader:
await this.WriteHeaderTagsImpl().ConfigureAwait(false);
break; break;
case WriterState.Writing: case WriterState.Writing:
break; break;