Skip to content

Commit

Permalink
several fixes, fixed loop check
Browse files Browse the repository at this point in the history
  • Loading branch information
Venipa committed Feb 22, 2021
1 parent 236536d commit 52303d9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
55 changes: 47 additions & 8 deletions CoubDownload-Bridge/Commands/DownloadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public string Execute(DownloadArgs args)
Debug.WriteLine(this.ffmpegPath);
var cl = new Coub();
var data = cl.getCoubById(CoubId).Result;
var audio = data.FileVersions.Html5.Audio?.High?.Url ?? data.FileVersions.Html5.Audio?.Med?.Url;
var audio = data.AudioVersions?.Template ?? data.FileVersions.Html5.Audio?.High?.Url ?? data.FileVersions.Html5.Audio?.Med?.Url;
var video = data.FileVersions.Html5.Video.High?.Url ?? data.FileVersions.Html5.Video.Med.Url;
var wc = new WebClient();
var videoInput = Path.Combine(tempPath, $"video_{data.Id}-({Guid.NewGuid().ToString()}).temp");
var audioInput = Path.Combine(tempPath, $"audio_{data.Id}-({Guid.NewGuid().ToString()}).temp");
var videoInput = Path.Combine(tempPath, $"video_{data.Id}-({Guid.NewGuid().ToString()}).tmp");
var audioInput = Path.Combine(tempPath, $"audio_{data.Id}-({Guid.NewGuid().ToString()}).tmp");

var dataCommunity = $"[{(App.Config.addCommunityPrefix ? $"{data.Communities?.Where(x => x.Visible != false)?.FirstOrDefault()?.Title ?? ""}" : "")}]";
var dataCategory = $"[{(App.Config.addCategoryPrefix ? $"{data.Categories?.Where(x => x.Visible != false && (App.Config.addCommunityPrefix ? (x.Title != dataCommunity) : true))?.FirstOrDefault()?.Title ?? "General"}" : "")}]";
Expand All @@ -122,7 +122,8 @@ public string Execute(DownloadArgs args)
Directory.CreateDirectory(outputPath);
}
}
} else
}
else
{
outputPath = Path.Combine(outputPath, "nsfw");
if (!Directory.Exists(outputPath))
Expand Down Expand Up @@ -209,6 +210,40 @@ public string Execute(DownloadArgs args)
var genMode = App.Config.useSinglePalletePerFrame ? "single" : "full";
var genWidth = App.Config.gifWidth > 50 && App.Config.gifWidth < 500 ? App.Config.gifWidth : 320;
ffmpeg.ExecuteAsync($"-y -i \"{videoInput}\" -filter_complex \"[0:v] scale=w={genWidth}:h=-1,split [a][b];[a] palettegen=stats_mode={genMode} [p];[b][p] paletteuse=new=1\" \"{resultGif}\"").Wait();
if (App.Config.AdditionalGIFConversions?.Count > 0)
{
string currentConvert = "Additional GIF Converts";
withPercentage = new ProgressBar(PbStyle.SingleLine, 100, currentConvert.Length, '█');
withPercentage.Refresh(0, currentConvert);
try
{
var tasks = App.Config.AdditionalGIFConversions.Select((x, i) =>
{
var tasksDone = 0;
var xHeight = x.Height != null ? x.Height : -1;
var xWidth = x.Width != null ? x.Width : (xHeight == -1 ? 100 : -1);
if (x.Height == null && x.Width == null)
{
throw new Exception("Error, Additional Gif Converter needs atleast 1 dimension");
}
var additionalOutputPath = Path.Combine(outputPath, $"{resultOutputPrefix}{CoubId} ({(xHeight == -1 ? "auto" : xHeight.ToString())}x{(x.MatchAspectRatio ? "auto" : xWidth.ToString())}).gif");
return ffmpeg.ExecuteAsync($"-y -i \"{videoInput}\" -filter_complex \"[0:v] scale=w={(x.MatchAspectRatio ? -1 : xWidth)}:h={(x.MatchAspectRatio ? -1 : xHeight)},{(x.Fps != null ? $"fps={x.Fps}," : "")}split [a][b];[a] palettegen=stats_mode={genMode} [p];[b][p] paletteuse=new=1\" \"{additionalOutputPath}\"").ContinueWith(r =>
{
withPercentage.Refresh(++tasksDone / App.Config.AdditionalGIFConversions.Count, currentConvert);
});
});
Task.WaitAll(tasks.ToArray());
withPercentage.Refresh(withPercentage.Max, currentConvert);
}
catch (Exception ex)
{
if (Debugger.IsAttached)
{
throw ex;
}
return ex.Message;
}
}
try
{
File.Delete(videoInput);
Expand All @@ -231,9 +266,12 @@ public string Execute(DownloadArgs args)
{
var vid = new MediaFile(videoInput);
var aud = new MediaFile(audioInput);
if (vid.FileInfo.Length >= aud.FileInfo.Length)
var audioDuration = ffmpeg.GetMetaDataAsync(aud).Result?.Duration;
var videoDuration = ffmpeg.GetMetaDataAsync(vid).Result?.Duration;
if (audioDuration.HasValue && videoDuration.HasValue && audioDuration.Value.TotalMilliseconds/1.5 < videoDuration.Value.TotalMilliseconds)
{
ffmpeg.ExecuteAsync($"-i \"{videoInput}\" -stream_loop -1 -i {audioInput} -c:v copy -shortest -map 0:v:0 -map 1:a:0 -y \"{resultOutput}\"").Wait();
Console.WriteLine($"Unable to loop video, audio is shorter than video.");
Task.Delay(5000).Wait();
}
else
{
Expand All @@ -243,7 +281,8 @@ public string Execute(DownloadArgs args)
else if (audio != null)
{
ffmpeg.ExecuteAsync($"-i \"{videoInput}\" -i {audioInput} -codec copy -shortest \"{resultOutput}\"").Wait();
} else
}
else
{
ffmpeg.ExecuteAsync($"-i \"{videoInput}\" -codec copy -shortest \"{resultOutput}\"").Wait();
}
Expand All @@ -255,7 +294,7 @@ public string Execute(DownloadArgs args)
}
catch { }
overallProgress.Next("Overall Progress");
if (App.Config.copyFileToClipboard)
if (File.Exists(resultOutput) && App.Config.copyFileToClipboard)
{
Dispatcher.CurrentDispatcher.Invoke(() =>
{
Expand Down
22 changes: 16 additions & 6 deletions CoubDownload-Bridge/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ public class GifGenerationConfig {
const int MIN_QUALITY = 10;
const int MAX_FPS = 60;
const int MIN_FPS = 10;
private int _Fps;
private int? _Fps;
[JsonProperty("fps")]
public int Fps
public int? Fps
{
get { return _Fps; }
set { _Fps = value < MIN_FPS ? MIN_FPS :
value > MAX_FPS ? MAX_FPS : value; }
set
{
if (value == null)
{
_Fps = null;
return;
}
_Fps = value < MIN_FPS ? MIN_FPS :
value > MAX_FPS ? MAX_FPS : value;
}
}

public int MaxFps { get; set; } = 100;
Expand All @@ -43,9 +51,9 @@ public int Quality
value > MAX_QUALITY ? MAX_QUALITY : value; }
}
[JsonProperty("width")]
public int Width { get; set; } = 100;
public int? Width { get; set; }
[JsonProperty("height")]
public int Height { get; set; } = 200;
public int? Height { get; set; }
[JsonProperty("match_aspect_ratio")]
public bool MatchAspectRatio { get; set; } = true;
}
Expand All @@ -66,6 +74,8 @@ public Config() { }
public bool copyFileToClipboard { get; private set; } = false;
[JsonProperty("spanVideoToAudio")]
public bool spanVideoToAudio { get; private set; } = false;
[JsonProperty("loopCountFallback")]
public int? LoopCountFallback { get; set; } = 5;
[JsonProperty("useSinglePalletePerFrame")]
public bool useSinglePalletePerFrame { get; private set; } = false;
[JsonProperty("addCategoryPrefix")]
Expand Down
6 changes: 6 additions & 0 deletions CoubDownload-Bridge/Tests/DownloadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public void ExecuteDownloadTest()
Assert.AreEqual(true, File.Exists(result));
}
[Test]
public void LoopTest()
{
var result = new DownloadCommand().Execute(new Args.DownloadArgs() { download = "2pd8gk", full = true });
Assert.AreEqual(true, File.Exists(result));
}
[Test]
public void GifTest()
{
var result = new DownloadCommand().Execute(new Args.DownloadArgs() { download = "1wjjeh", gif = true });
Expand Down

0 comments on commit 52303d9

Please sign in to comment.