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,39 +155,51 @@ 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");
this.lastScriptBody = this.nextScriptTag.ScriptData; if (!this.allowMissingHeader)
var value = this.lastScriptBody.GetMetadataValue();
if (value is not null)
{ {
value["duration"] = (ScriptDataNumber)0; if (this.nextVideoHeaderTag is null)
throw new InvalidOperationException("No video header tag availible");
if (!this.disableKeyframes) if (this.nextAudioHeaderTag is null)
{ throw new InvalidOperationException("No audio header tag availible");
var kfv = new KeyframesScriptDataValue();
value["keyframes"] = kfv;
this.keyframesScriptDataValue = kfv;
}
} }
this.BeforeScriptTagWrite?.Invoke(this.lastScriptBody); // Open File
await this.tagWriter.CreateNewFile().ConfigureAwait(false);
await this.tagWriter.WriteTag(this.nextScriptTag).ConfigureAwait(false); // Write Script Tag
{
this.lastScriptBody = this.nextScriptTag.ScriptData;
this.state = WriterState.BeforeHeader; var value = this.lastScriptBody.GetMetadataValue();
} if (value is not null)
{
value["duration"] = (ScriptDataNumber)0;
private async Task WriteHeaderTagsImpl() if (!this.disableKeyframes)
{ {
if (this.allowMissingHeader) var kfv = new KeyframesScriptDataValue();
value["keyframes"] = kfv;
this.keyframesScriptDataValue = kfv;
}
}
this.BeforeScriptTagWrite?.Invoke(this.lastScriptBody);
await this.tagWriter.WriteTag(this.nextScriptTag).ConfigureAwait(false);
}
// Write Header Tag
{ {
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;