private async void SanityCheckTwitch() { List <string> paths = new List <string>(); foreach (string path in paths) { foreach (string file in Directory.EnumerateFiles(path, "twitch_*.mp4", SearchOption.AllDirectories)) { try { string filepart = Path.GetFileNameWithoutExtension(file); long tmp; string id = filepart.Split('_').Where(x => x.StartsWith("v") && long.TryParse(x.Substring(1), out tmp)).First(); List <IVideoJob> jobs = new List <IVideoJob>(); foreach (var item in objectListViewDownloads.Objects) { IVideoJob job = item as IVideoJob; if (job != null) { if (job.VideoInfo.Service == StreamService.Twitch && job.VideoInfo.VideoId == id && job.JobStatus == VideoJobStatus.Finished) { try { // sanity check TimeSpan actualVideoLength = (await FFMpegUtil.Probe(file)).Duration; TimeSpan expectedVideoLength = job.VideoInfo.VideoLength; if (actualVideoLength.Subtract(expectedVideoLength).Duration() > TimeSpan.FromSeconds(5)) { // if difference is bigger than 5 seconds something is off, report job.Status = "Large time difference between expected (" + expectedVideoLength.ToString() + ") and actual (" + actualVideoLength.ToString() + "), reenqueueing. (" + file + ")"; job.JobStatus = VideoJobStatus.NotStarted; } else { job.Status = "Sane."; } } catch (Exception ex) { job.Status = "Exception while sanity checking: " + ex.ToString(); job.JobStatus = VideoJobStatus.NotStarted; } } } } } catch (Exception) { } } } }
private async Task GenDurationTwitch(StringBuilder sb, string path) { sb.AppendFormat("Scanning {0}...", path).AppendLine(); List <string> filesInDir = Directory.EnumerateFiles(path).ToList(); foreach (string filepath in filesInDir) { string filename = Path.GetFileName(filepath); foreach (IVideoJob job in Jobs) { if (job != null && job.VideoInfo.Service == StreamService.Twitch) { string filestart = "twitch_" + job.VideoInfo.Username + "_"; string filemid = "_v" + job.VideoInfo.VideoId + "_"; string fileend = ".mp4"; if (filename.StartsWith(filestart) && filename.Contains(filemid) && filename.EndsWith(fileend)) { Console.WriteLine("Probing {0}", filepath); TimeSpan actualVideoLength = (await FFMpegUtil.Probe(filepath)).Duration; TimeSpan expectedVideoLength = job.VideoInfo.VideoLength; if (actualVideoLength.Subtract(expectedVideoLength).Duration() > TimeSpan.FromSeconds(5)) { sb.AppendFormat("File {0} seems to have a large difference. ({1} seconds)", filepath, actualVideoLength.Subtract(expectedVideoLength).Duration().TotalSeconds); sb.AppendLine(); } } } } } try { foreach (string subdir in Directory.EnumerateDirectories(path)) { try { await GenDurationTwitch(sb, subdir); } catch (Exception) { } } } catch (Exception) { } }
public async static Task <List <IVideoInfo> > FetchReencodeableFiles(string path, string additionalOptions) { string chunked = "_chunked"; string postfix = "_x264crf23"; List <string> reencodeFiles = new List <string>(); foreach (string file in System.IO.Directory.EnumerateFiles(path)) { string name = System.IO.Path.GetFileNameWithoutExtension(file); string ext = Path.GetExtension(file); if (ext == ".mp4" && name.EndsWith(chunked)) { string newfile = Path.Combine(path, postfix, name.Substring(0, name.Length - chunked.Length) + postfix + ext); if (!File.Exists(newfile)) { reencodeFiles.Add(System.IO.Path.GetFullPath(file)); } } } List <IVideoInfo> rv = new List <IVideoInfo>(); foreach (string f in reencodeFiles) { FFProbeResult probe = await FFMpegUtil.Probe(f); int framerate; try { framerate = (int)Math.Round(probe.Streams.Where(x => x.Framerate > 0.0f).FirstOrDefault().Framerate); } catch (Exception) { framerate = 30; } if (framerate <= 0) { framerate = 30; } // super hacky... probably should improve this stuff List <string> ffmpegOptions; string postfixOld; string postfixNew; string outputformat; if (additionalOptions == "rescale720_30fps") { ffmpegOptions = new List <string>() { "-c:v", "libx264", "-preset", "slower", "-crf", "23", "-g", "300", "-x264-params", "\"min-keyint=30:b-adapt=2\"", "-sws_flags", "lanczos", "-vf", "\"scale=-2:720\"", "-c:a", "copy", "-max_muxing_queue_size", "100000", "-r", "30" }; postfixOld = "_chunked"; postfixNew = "_x264crf23scaled720p30"; outputformat = "mkv"; } else if (additionalOptions == "rescale480_30fps") { ffmpegOptions = new List <string>() { "-c:v", "libx264", "-preset", "slower", "-crf", "23", "-g", "300", "-x264-params", "\"min-keyint=30:b-adapt=2\"", "-sws_flags", "lanczos", "-vf", "\"scale=-2:480\"", "-c:a", "copy", "-max_muxing_queue_size", "100000", "-r", "30" }; postfixOld = "_chunked"; postfixNew = "_x264crf23scaled480p30"; outputformat = "mkv"; } else if (additionalOptions == "30fps") { ffmpegOptions = new List <string>() { "-c:v", "libx264", "-preset", "slower", "-crf", "23", "-g", "300", "-x264-params", "\"min-keyint=30:b-adapt=2\"", "-c:a", "copy", "-max_muxing_queue_size", "100000", "-r", "30" }; postfixOld = "_chunked"; postfixNew = "_x264crf23-30"; outputformat = "mkv"; } else if (additionalOptions == "rescale720") { ffmpegOptions = new List <string>() { "-c:v", "libx264", "-preset", "slower", "-crf", "23", "-g", (framerate * 10).ToString(), "-x264-params", "\"min-keyint=" + framerate.ToString() + ":b-adapt=2\"", "-sws_flags", "lanczos", "-vf", "\"scale=-2:720\"", "-c:a", "copy", "-max_muxing_queue_size", "100000" }; postfixOld = "_chunked"; postfixNew = "_x264crf23scaled720p"; outputformat = "mp4"; } else if (additionalOptions == "rescale480") { ffmpegOptions = new List <string>() { "-c:v", "libx264", "-preset", "slower", "-crf", "23", "-g", (framerate * 10).ToString(), "-x264-params", "\"min-keyint=" + framerate.ToString() + ":b-adapt=2\"", "-sws_flags", "lanczos", "-vf", "\"scale=-2:480\"", "-c:a", "copy", "-max_muxing_queue_size", "100000" }; postfixOld = "_chunked"; postfixNew = "_x264crf23scaled480p"; outputformat = "mp4"; } else { ffmpegOptions = new List <string>() { "-c:v", "libx264", "-preset", "slower", "-crf", "23", "-g", (framerate * 10).ToString(), "-x264-params", "\"min-keyint=" + framerate.ToString() + ":b-adapt=2\"", "-c:a", "copy", "-max_muxing_queue_size", "100000" }; postfixOld = "_chunked"; postfixNew = "_x264crf23"; outputformat = "mp4"; } IVideoInfo retval = new FFMpegReencodeJobVideoInfo(f, probe, ffmpegOptions, postfixOld, postfixNew, outputformat); rv.Add(retval); } return(rv); }