Skip to content

Commit

Permalink
Test & Template update
Browse files Browse the repository at this point in the history
  • Loading branch information
tqk2811 committed Dec 24, 2021
1 parent 35def9a commit 0767dd4
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 20 deletions.
29 changes: 18 additions & 11 deletions FFmpegArgs.Template/BlurredBackgroundTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FFmpegArgs.Cores.Maps;
using FFmpegArgs.Filters.Enums;
using FFmpegArgs.Filters.MultimediaFilters;
using FFmpegArgs.Filters.VideoFilters;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,7 +10,7 @@ namespace FFmpegArgs.Template
public static class BlurredBackgroundTemplate
{
public static ImageMap MakeBlurredBackgroundTemplate(this ImageMap image,
int width, int height, string lumaRadius = "100")
int width, int height, int fps = 24, string lumaRadius = "100")
{
List<ImageMap> inputs = new List<ImageMap>();
if (image.IsInput)
Expand All @@ -23,18 +24,24 @@ public static ImageMap MakeBlurredBackgroundTemplate(this ImageMap image,
}

var blurred = inputs.First()
.ScaleFilter($"{width}", $"{height}").MapOut
.SetSarFilter("1/1").MapOut
.FormatFilter(PixFmt.rgba).MapOut
.BoxBlurFilter().LumaRadius($"{lumaRadius}").MapOut
.SetSarFilter("1/1").MapOut;
.ScaleFilter($"{width}", $"{height}").MapOut
.SetSarFilter("1/1").MapOut
.FpsFilter(fps).MapOut
.FormatFilter(PixFmt.rgba).MapOut
.BoxBlurFilter().LumaRadius($"{lumaRadius}").MapOut
.SetSarFilter("1/1").MapOut;

return inputs.Last()
.ScaleFilter($"if(gte(iw/ih,{width}/{height}),min(iw,{width}),-1)",
var raw = inputs.Last()
.ScaleFilter($"if(gte(iw/ih,{width}/{height}),min(iw,{width}),-1)",
$"if(gte(iw/ih,{width}/{height}),-1,min(ih,{height}))").MapOut
.ScaleFilter("trunc(iw/2)*2", "trunc(ih/2)*2").MapOut
.OverlayFilterOn(blurred, "(main_w - overlay_w)/2", "(main_h-overlay_h)/2").MapOut//center
.FormatFilter(PixFmt.rgba).MapOut;
.ScaleFilter("trunc(iw/2)*2", "trunc(ih/2)*2").MapOut
.SetSarFilter("1/1").MapOut
.FpsFilter(fps).MapOut
.FormatFilter(PixFmt.rgba).MapOut;

return raw
.OverlayFilterOn(blurred, "(main_w - overlay_w)/2", "(main_h-overlay_h)/2").MapOut//center
.SetPtsFilter("PTS-STARTPTS").MapOut;
}
}
}
172 changes: 172 additions & 0 deletions FFmpegArgs.Test/TanersenerSlideShow/FadeInOne.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using FFmpegArgs.Cores.Maps;
using FFmpegArgs.Executes;
using FFmpegArgs.Filters.Enums;
using FFmpegArgs.Filters.MultimediaFilters;
using FFmpegArgs.Filters.VideoFilters;
using FFmpegArgs.Inputs;
using FFmpegArgs.Outputs;
using FFmpegArgs.Template;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;

namespace FFmpegArgs.Test.TanersenerSlideShow
{
[TestClass]
public class FadeInOne
{
/// <summary>
/// Same as BlurredBackground
/// </summary>
[TestMethod]
public void FadeInOneTest()
{
DirectoryInfo directoryInfo = new DirectoryInfo(@"D:\temp\ffmpeg_encode_test\ImgsTest");
var files = directoryInfo.GetFiles("*.jpg");
FFmpegArg ffmpegArg = new FFmpegArg().OverWriteOutput();
var inputs = files.Select(x => ffmpegArg.AddImageInput(new ImageFileInput(x.Name).SetOption("-loop", 1))).ToList();

int WIDTH = 1280;
int HEIGHT = 720;
int FPS = 30;
double TRANSITION_DURATION = 1;
double IMAGE_DURATION = 2;
ScreenMode SCREEN_MODE = ScreenMode.Blur;
Color BACKGROUND_COLOR = Color.Black;

int IMAGE_COUNT = inputs.Count;
double TRANSITION_FRAME_COUNT = TRANSITION_DURATION * FPS;
double IMAGE_FRAME_COUNT = IMAGE_DURATION * FPS;
double TOTAL_DURATION = (IMAGE_DURATION + TRANSITION_DURATION) * IMAGE_COUNT - TRANSITION_DURATION;
double TOTAL_FRAME_COUNT = TOTAL_DURATION * FPS;

List<IEnumerable<ImageMap>> prepareInputs = new List<IEnumerable<ImageMap>>();
switch (SCREEN_MODE)
{
case ScreenMode.Center:
prepareInputs.AddRange(inputs.Select(x => x
.SetPtsFilter("PTS-STARTPTS").MapOut
.ScaleFilter(
$"if(gte(iw/ih,{WIDTH}/{HEIGHT}),min(iw,{WIDTH}),-1)",
$"if(gte(iw/ih,{WIDTH}/{HEIGHT}),-1,min(ih,{HEIGHT}))").MapOut
.ScaleFilter("trunc(iw/2)*2", "trunc(ih/2)*2").MapOut
.SetSarFilter("1/1").MapOut
.FpsFilter($"{FPS}").MapOut
.FormatFilter(PixFmt.rgba).MapOut
.SplitFilter(2).MapsOut));
break;

case ScreenMode.Crop:
prepareInputs.AddRange(inputs.Select(x => x
.SetPtsFilter("PTS-STARTPTS").MapOut
.ScaleFilter(
$"if(gte(iw/ih,{WIDTH}/{HEIGHT}),-1,{WIDTH})",
$"if(gte(iw/ih,{WIDTH}/{HEIGHT}),{HEIGHT},-1)").MapOut
.CropFilter().WH($"{WIDTH}",$"{HEIGHT}").MapOut
.SetSarFilter("1/1").MapOut
.FpsFilter($"{FPS}").MapOut
.FormatFilter(PixFmt.rgba).MapOut
.SplitFilter(2).MapsOut));
break;

case ScreenMode.Scale:
prepareInputs.AddRange(inputs.Select(x => x
.ScaleFilter($"{WIDTH}", $"{HEIGHT}").MapOut
.SetSarFilter("1/1").MapOut
.FpsFilter($"{FPS}").MapOut
.FormatFilter(PixFmt.rgba).MapOut
.SplitFilter(2).MapsOut));
break;

case ScreenMode.Blur:
prepareInputs.AddRange(inputs.Select(x => x
.MakeBlurredBackgroundTemplate(WIDTH, HEIGHT, FPS, "100")
.SplitFilter(2).MapsOut));
break;
}

List<ImageMap> overlaids = new List<ImageMap>();
for (int i = 0; i < prepareInputs.Count; i++)
{
overlaids.Add(prepareInputs[i].First()
.PadFilter($"{WIDTH}", $"{HEIGHT}").Position($"({WIDTH} - iw)/2", $"({HEIGHT} - ih)/2").MapOut
.TrimFilter().Duration(TimeSpan.FromSeconds(IMAGE_DURATION)).MapOut
.SelectFilter($"lte(n,{IMAGE_FRAME_COUNT})").MapOut);
}


List<ImageMap> startings = new List<ImageMap>();
List<ImageMap> endings = new List<ImageMap>();
for (int i = 0; i < prepareInputs.Count; i++)
{
//first create ed only (if only 1 image -> create ed)
//mid: split to ed and op
//last create op

var res = prepareInputs[i].Last()
.PadFilter($"{WIDTH}", $"{HEIGHT}").Position($"({WIDTH}-iw)/2", $"({HEIGHT}-ih)/2").MapOut
.TrimFilter().Duration(TimeSpan.FromSeconds(TRANSITION_DURATION)).MapOut
.SelectFilter($"lte(n,{TRANSITION_FRAME_COUNT})").MapOut;

if (i == 0)//first
{
if (overlaids.Count > 1)
{
endings.Add(res);
}
}
else if (i == overlaids.Count - 1)//last
{
startings.Add(res);
}
else//mid
{
var splits = res.SplitFilter(2).MapsOut;
startings.Add(splits.First());
endings.Add(splits.Last());
}
}

//CREATE TRANSITION FRAMES
List<ImageMap> blendeds = new List<ImageMap>();

for (int i = 0; i < startings.Count; i++)
{
blendeds.Add(startings[i]
.BlendFilterOn(endings[i]).All_Expr(
$"A*(if( gte(T,{TRANSITION_DURATION}),{TRANSITION_DURATION},T/{TRANSITION_DURATION})) + " +
$"B*(1-(if(gte(T,{TRANSITION_DURATION}),{TRANSITION_DURATION},T/{TRANSITION_DURATION})))").MapOut
.SelectFilter($"lte(n,{TRANSITION_FRAME_COUNT})").MapOut);
}

//CONCAT
List<ConcatGroup> concatGroups = new List<ConcatGroup>();
for (int i = 0; i < overlaids.Count; i++)
{
concatGroups.Add(new ConcatGroup(overlaids[i]));
if (i < overlaids.Count - 1) concatGroups.Add(new ConcatGroup(blendeds[i]));
}
ConcatFilter concatFilter = new ConcatFilter(concatGroups);
var mapout = concatFilter.ImageMapsOut.First()
.FormatFilter(PixFmt.yuv420p).MapOut;



var output = new ImageFileOutput($"{nameof(FadeInOneTest)}.mp4", mapout);
ffmpegArg.AddOutput(output);

FFmpegRender fFmpegRender = ffmpegArg.Render(new FFmpegRenderConfig()
{
WorkingDirectory = directoryInfo.FullName
});
FFmpegRenderResult result = fFmpegRender.Execute();
Assert.IsTrue(result.ExitCode == 0);
Process.Start("ffplay", Path.Combine(directoryInfo.FullName, $"{nameof(FadeInOneTest)}.mp4"));

}
}
}
111 changes: 111 additions & 0 deletions FFmpegArgs.Test/TanersenerSlideShow/FadeInTwo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using FFmpegArgs.Cores.Maps;
using FFmpegArgs.Executes;
using FFmpegArgs.Filters.Enums;
using FFmpegArgs.Filters.MultimediaFilters;
using FFmpegArgs.Filters.VideoFilters;
using FFmpegArgs.Inputs;
using FFmpegArgs.Outputs;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;

namespace FFmpegArgs.Test.TanersenerSlideShow
{
[TestClass]
public class FadeInTwo
{
[TestMethod]
public void FadeInTwoTest()
{
int WIDTH = 1366;
int HEIGHT = 768;
int TRANSITION_DURATION = 3;
int IMAGE_DURATION = 2;
int FPS = 24;
double TRANSITION_FRAME_COUNT = TRANSITION_DURATION * FPS;
double IMAGE_FRAME_COUNT = FPS * IMAGE_DURATION;
Color backGroundColor = Color.FromArgb(0, 0, 0, 0);
DirectoryInfo directoryInfo = new DirectoryInfo(@"D:\temp\ffmpeg_encode_test\ImgsTest");
var files = directoryInfo.GetFiles("*.jpg");

FFmpegArg ffmpegArg = new FFmpegArg().OverWriteOutput();

var inputs = files.Select(x => ffmpegArg.AddImageInput(new ImageFileInput(x.Name).SetOption("-loop", 1))).ToList();


List<IEnumerable<ImageMap>> splitInputs = new List<IEnumerable<ImageMap>>();
splitInputs.AddRange(inputs.Select(x => x
.SetSarFilter("1/1").MapOut
.ScaleFilter($"if(gte(iw/ih,{WIDTH}/{HEIGHT}),min(iw,{WIDTH}),-1)",
$"if(gte(iw/ih,{WIDTH}/{HEIGHT}),-1,min(ih,{HEIGHT}))").MapOut
.PadFilter($"{WIDTH}", $"{HEIGHT}").Position($"({HEIGHT} - ow)/2", $"({HEIGHT} - oh)/2").Color(backGroundColor).MapOut
.FpsFilter($"{FPS}").MapOut
.SetPtsFilter("PTS-STARTPTS").MapOut
.SplitFilter(2).MapsOut));

List<ImageMap> startings = new List<ImageMap>();
List<ImageMap> endings = new List<ImageMap>();
for (int i = 0; i < splitInputs.Count; i++)
{
var res = splitInputs[i].Last()
.TrimFilter().Duration(TimeSpan.FromSeconds(TRANSITION_DURATION)).MapOut
.SelectFilter($"lte(n,{TRANSITION_FRAME_COUNT})").MapOut;

if (i == 0)
{
endings.Add(res);
}
else if (i == splitInputs.Count - 1)
{
startings.Add(res);
}
else
{
var splits = res.SplitFilter(2).MapsOut;
startings.Add(splits.First());
endings.Add(splits.Last());
}
}

//CREATE TRANSITION FRAMES
List<ImageMap> blendeds = new List<ImageMap>();
for (int i = 0; i < startings.Count; i++)
{
blendeds.Add(startings[i]
.BlendFilterOn(endings[i]).All_Expr(
$"A*(if(gte(T,{TRANSITION_DURATION}),{TRANSITION_DURATION},T/{TRANSITION_DURATION})) + " +
$"B*(1-(if(gte(T,{TRANSITION_DURATION}),{TRANSITION_DURATION},T/{TRANSITION_DURATION})))").MapOut);
}

//concat
List<ConcatGroup> concatGroups = new List<ConcatGroup>();
for (int i = 0; i < splitInputs.Count; i++)
{
concatGroups.Add(new ConcatGroup(splitInputs[i].First()
.TrimFilter().Duration(TimeSpan.FromSeconds(IMAGE_DURATION)).MapOut
.SelectFilter($"lte(n,{IMAGE_FRAME_COUNT})").MapOut
.SetSarFilter("1/1").MapOut));

if (i < splitInputs.Count - 1) concatGroups.Add(new ConcatGroup(blendeds[i].SetSarFilter("1/1").MapOut));
}
ConcatFilter concatFilter = new ConcatFilter(concatGroups);
var mapout = concatFilter.ImageMapsOut.First()
.FormatFilter(PixFmt.yuv420p).MapOut;

var output = new ImageFileOutput($"{nameof(FadeInTwoTest)}.mp4", mapout);
ffmpegArg.AddOutput(output);

FFmpegRender fFmpegRender = ffmpegArg.Render(new FFmpegRenderConfig()
{
WorkingDirectory = directoryInfo.FullName
});
FFmpegRenderResult result = fFmpegRender.Execute();
Assert.IsTrue(result.ExitCode == 0);
Process.Start("ffplay", Path.Combine(directoryInfo.FullName, $"{nameof(FadeInTwoTest)}.mp4"));
}
}
}
4 changes: 2 additions & 2 deletions FFmpegArgs.Test/TanersenerSlideShow/PushHorizontalFilm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void PushHorizontalFilmTest()
{
int WIDTH = 1280;
int HEIGHT = 720;
double FPS = 60;
int FPS = 60;
double TRANSITION_DURATION = 3;
ScreenMode screenMode = ScreenMode.Blur;///# 1=CENTER, 2=CROP, 3=SCALE, 4=BLUR
Color BACKGROUND_COLOR = Color.FromArgb(0, 0, 0, 0);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void PushHorizontalFilmTest()
case ScreenMode.Blur:
{
images_Prepare.Add(images_inputmap[i]
.MakeBlurredBackgroundTemplate(WIDTH, HEIGHT, "100")
.MakeBlurredBackgroundTemplate(WIDTH, HEIGHT, FPS, "100")
.FormatFilter(PixFmt.rgba).MapOut
.SetSarFilter("1/1").MapOut);
break;
Expand Down
4 changes: 2 additions & 2 deletions FFmpegArgs.Test/TanersenerSlideShow/PushVerticalFilm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void PushVerticalFilmTest()
{
int WIDTH = 1280;
int HEIGHT = 720;
double FPS = 60;
int FPS = 60;
double TRANSITION_DURATION = 3;
ScreenMode screenMode = ScreenMode.Blur;
Color BACKGROUND_COLOR = Color.FromArgb(0, 0, 0, 0);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void PushVerticalFilmTest()
case ScreenMode.Blur:
{
images_Prepare.Add(images_inputmap[i]
.MakeBlurredBackgroundTemplate(WIDTH, HEIGHT, "100")
.MakeBlurredBackgroundTemplate(WIDTH, HEIGHT, FPS, "100")
.FormatFilter(PixFmt.rgba).MapOut
.SetSarFilter("1/1").MapOut);
break;
Expand Down
Loading

0 comments on commit 0767dd4

Please sign in to comment.