2021-05-06 22:39:20 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-08-22 19:56:24 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
2019-03-26 21:56:05 +00:00
|
|
|
using System.Threading.Tasks;
|
2018-12-14 23:48:06 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.MediaEncoding
|
|
|
|
{
|
|
|
|
public class JobLogger
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
public JobLogger(ILogger logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2023-08-20 18:06:57 +00:00
|
|
|
public async Task StartStreamingLog(EncodingJobInfo state, StreamReader reader, Stream target)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-03-26 21:56:05 +00:00
|
|
|
using (target)
|
2023-08-20 18:06:57 +00:00
|
|
|
using (reader)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2019-03-15 16:56:19 +00:00
|
|
|
while (!reader.EndOfStream && reader.BaseStream.CanRead)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
var line = await reader.ReadLineAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
ParseLogLine(line, state);
|
|
|
|
|
|
|
|
var bytes = Encoding.UTF8.GetBytes(Environment.NewLine + line);
|
|
|
|
|
2019-02-24 21:04:30 +00:00
|
|
|
// If ffmpeg process is closed, the state is disposed, so don't write to target in that case
|
|
|
|
if (!target.CanWrite)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
await target.WriteAsync(bytes).ConfigureAwait(false);
|
2019-03-15 16:56:19 +00:00
|
|
|
|
|
|
|
// Check again, the stream could have been closed
|
|
|
|
if (!target.CanWrite)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
await target.FlushAsync().ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogError(ex, "Error reading ffmpeg log");
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ParseLogLine(string line, EncodingJobInfo state)
|
|
|
|
{
|
|
|
|
float? framerate = null;
|
|
|
|
double? percent = null;
|
|
|
|
TimeSpan? transcodingPosition = null;
|
|
|
|
long? bytesTranscoded = null;
|
|
|
|
int? bitRate = null;
|
|
|
|
|
|
|
|
var parts = line.Split(' ');
|
|
|
|
|
|
|
|
var totalMs = state.RunTimeTicks.HasValue
|
|
|
|
? TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalMilliseconds
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
var startMs = state.BaseRequest.StartTimeTicks.HasValue
|
|
|
|
? TimeSpan.FromTicks(state.BaseRequest.StartTimeTicks.Value).TotalMilliseconds
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
for (var i = 0; i < parts.Length; i++)
|
|
|
|
{
|
|
|
|
var part = parts[i];
|
|
|
|
|
|
|
|
if (string.Equals(part, "fps=", StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
(i + 1 < parts.Length))
|
|
|
|
{
|
|
|
|
var rate = parts[i + 1];
|
|
|
|
|
2023-02-19 15:52:29 +00:00
|
|
|
if (float.TryParse(rate, CultureInfo.InvariantCulture, out var val))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
framerate = val;
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 08:46:56 +00:00
|
|
|
else if (part.StartsWith("fps=", StringComparison.OrdinalIgnoreCase))
|
2020-02-02 08:27:02 +00:00
|
|
|
{
|
2020-11-14 14:47:34 +00:00
|
|
|
var rate = part.Split('=', 2)[^1];
|
2020-02-02 08:27:02 +00:00
|
|
|
|
2023-02-19 15:52:29 +00:00
|
|
|
if (float.TryParse(rate, CultureInfo.InvariantCulture, out var val))
|
2020-02-02 08:27:02 +00:00
|
|
|
{
|
|
|
|
framerate = val;
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 23:27:57 +00:00
|
|
|
else if (state.RunTimeTicks.HasValue &&
|
|
|
|
part.StartsWith("time=", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2020-11-14 14:47:34 +00:00
|
|
|
var time = part.Split('=', 2)[^1];
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2021-09-26 14:14:36 +00:00
|
|
|
if (TimeSpan.TryParse(time, CultureInfo.InvariantCulture, out var val))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
var currentMs = startMs + val.TotalMilliseconds;
|
|
|
|
|
2019-03-27 15:26:33 +00:00
|
|
|
percent = 100.0 * currentMs / totalMs;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2022-06-27 00:53:03 +00:00
|
|
|
transcodingPosition = TimeSpan.FromMilliseconds(currentMs);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (part.StartsWith("size=", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2020-11-14 14:47:34 +00:00
|
|
|
var size = part.Split('=', 2)[^1];
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
int? scale = null;
|
2021-09-19 18:53:31 +00:00
|
|
|
if (size.Contains("kb", StringComparison.OrdinalIgnoreCase))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
scale = 1024;
|
|
|
|
size = size.Replace("kb", string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scale.HasValue)
|
|
|
|
{
|
2023-02-19 15:52:29 +00:00
|
|
|
if (long.TryParse(size, CultureInfo.InvariantCulture, out var val))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
bytesTranscoded = val * scale.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (part.StartsWith("bitrate=", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2020-11-14 14:47:34 +00:00
|
|
|
var rate = part.Split('=', 2)[^1];
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
int? scale = null;
|
2021-09-19 18:53:31 +00:00
|
|
|
if (rate.Contains("kbits/s", StringComparison.OrdinalIgnoreCase))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
scale = 1024;
|
|
|
|
rate = rate.Replace("kbits/s", string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scale.HasValue)
|
|
|
|
{
|
2023-02-19 15:52:29 +00:00
|
|
|
if (float.TryParse(rate, CultureInfo.InvariantCulture, out var val))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
bitRate = (int)Math.Ceiling(val * scale.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (framerate.HasValue || percent.HasValue)
|
|
|
|
{
|
2020-02-02 07:58:29 +00:00
|
|
|
state.ReportTranscodingProgress(transcodingPosition, framerate, percent, bytesTranscoded, bitRate);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|