-
Notifications
You must be signed in to change notification settings - Fork 7
/
ConvertJob.cs
220 lines (182 loc) · 8.23 KB
/
ConvertJob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Newtonsoft.Json.Linq;
using youtube_dl_viewer.Util;
namespace youtube_dl_viewer.Jobs
{
public class ConvertJob : Job
{
public readonly string Destination;
public readonly string Temp;
public readonly int DataDirIndex;
public readonly string VideoUID;
private (int, int) _progress = (0, 1);
public override (int, int) Progress => _progress;
public ConvertJob(AbsJobManager man, string src, string dst, int ddindex, string viduid) : base(man, src)
{
Destination = dst;
DataDirIndex = ddindex;
VideoUID = viduid;
Temp = Path.Combine(Path.GetTempPath(), "yt_dl_v_" + Guid.NewGuid().ToString("B") + ".webm");
}
public override string Name => $"Convert::'{Path.GetFileName(Source)}'";
protected override void Run()
{
Process proc = null;
var start = DateTime.Now;
try
{
if (!Program.HasValidFFMPEG) throw new Exception("no ffmpeg");
var (ecode1, outputProbe) = FFMPEGUtil.RunCommand(Program.Args.FFPROBEExec, $" -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"{Source}\"", "prevgen-probe");
if (AbortRequest) { ChangeState(JobState.Aborted); return; }
if (ecode1 != 0)
{
Console.Error.WriteLine($"Job [{Name}] failed (non-zero exit code in ffprobe)");
ChangeState(JobState.Failed);
return;
}
var videolen = double.Parse(outputProbe.Trim(), CultureInfo.InvariantCulture);
var cmd = $" -i \"{Source}\" -f webm -vcodec libvpx-vp9 {Program.Args.ConvertFFMPEGParams} {Temp}";
proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Program.Args.FFMPEGExec,
Arguments = cmd,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
}
};
var builderOut = new StringBuilder();
proc.OutputDataReceived += (sender, args) =>
{
if (args.Data == null) return;
if (builderOut.Length == 0) builderOut.Append(args.Data);
else builderOut.Append("\n" + args.Data);
ParseFFMpegOutputLine(args, videolen);
};
proc.ErrorDataReceived += (sender, args) =>
{
if (args.Data == null) return;
if (builderOut.Length == 0) builderOut.Append(args.Data);
else builderOut.Append("\n" + args.Data);
ParseFFMpegOutputLine(args, videolen);
};
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
while (!File.Exists(Temp))
{
if (AbortRequest) return;
if (proc.HasExited && !File.Exists(Temp))
{
ChangeState(JobState.Failed);
return;
}
Thread.Sleep(0);
}
for (;;) // Wait for ffmpeg
{
if (AbortRequest) { ChangeState(JobState.Aborted); return; }
if (proc.HasExited) break;
Thread.Sleep(100);
}
if (Program.Args.FFMPEGDebugDir != null)
{
File.WriteAllText(Path.Combine(Program.Args.FFMPEGDebugDir, $"{start:yyyy-MM-dd_HH-mm-ss.fffffff}_[convert].log"), $"> {Program.Args.FFMPEGExec} {cmd}\nExitCode:{proc.ExitCode}\nStart:{start:yyyy-MM-dd HH:mm:ss}\nEnd:{DateTime.Now:yyyy-MM-dd HH:mm:ss}\n\n{builderOut}");
}
if (proc.ExitCode != 0)
{
Console.Error.WriteLine($"Job [{Name}] failed (non-zero exit code)");
ChangeState(JobState.Failed);
}
else
{
ChangeState(JobState.Finished);
_progress = (1, 1);
while (ProxyCount != 0) // Wait for proxies
{
if (AbortRequest) { ChangeState(JobState.Aborted); return; }
Thread.Sleep(100);
}
lock (SuperLock)
{
if (Destination != null)
{
for (var i = 0; i < 15; i++) // 15 retries
{
if (AbortRequest) { ChangeState(JobState.Aborted); return; }
try
{
File.Move(Temp, Destination);
if (File.Exists(Destination) && new FileInfo(Destination).Length == 0) File.Delete(Destination);
Program.PatchDataCache(DataDirIndex, VideoUID, new[]{"meta", "cached"}, true);
Program.PatchDataCache(DataDirIndex, VideoUID, new[]{"meta", "cache_file"}, Destination);
break;
}
catch (IOException)
{
Console.Error.WriteLine("Move of converted file to cache failed ... retry in 2 secs");
Thread.Sleep(2 * 1000);
}
}
}
}
ChangeState(JobState.Success);
}
}
finally
{
if (State == JobState.Running) ChangeState(JobState.Failed); // just to be sure
if (proc != null && !proc.HasExited)
{
proc.Kill(true);
Thread.Sleep(500);
}
for (var i = 0;; i++)
{
try
{
if (File.Exists(Temp)) File.Delete(Temp);
break;
}
catch (IOException)
{
Console.Error.WriteLine("Delete of converted file (temp dir) failed ... retry in 3 secs");
Thread.Sleep(3 * 1000);
}
if (i == 10) // 10 retries
{
Console.Error.WriteLine("Delete of converted file (temp dir) failed finally");
break;
}
}
}
}
private void ParseFFMpegOutputLine(DataReceivedEventArgs args, double videolen)
{
if (args.Data.StartsWith("frame="))
{
var match = Regex.Match(args.Data, @"time=(?<time>[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2})");
if (match.Success)
{
var time = TimeSpan.Parse(match.Groups["time"].Value);
_progress = ((int) Math.Floor((time.TotalSeconds / videolen) * 1000), 1000);
}
}
}
public override JObject AsJson(string managerName, string queue)
{
var obj = base.AsJson(managerName, queue);
obj.Add(new JProperty("Destination", Destination));
obj.Add(new JProperty("Temp", Temp));
return obj;
}
}
}