Skip to content

Commit

Permalink
high quality rescale
Browse files Browse the repository at this point in the history
  • Loading branch information
Anime4000 committed May 1, 2020
1 parent a7ba8c4 commit ccb7cb9
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 215 deletions.
43 changes: 27 additions & 16 deletions IFME.FFmpeg/MediaInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public MediaInfo(string path)
string lang = "und";
try { lang = stream.tags.language; }
catch (Exception ex) { Console.WriteLine(ex.Message); }
if (string.IsNullOrEmpty(lang)) lang = "und";

string codec = "unknown";
try { codec = stream.codec_name; }
Expand Down Expand Up @@ -142,6 +143,11 @@ public MediaInfo(string path)
try { id = stream.index; }
catch (Exception ex) { Console.WriteLine(ex.Message); }

string lang = "und";
try { lang = stream.tags.language; }
catch (Exception ex) { Console.WriteLine(ex.Message); }
if (string.IsNullOrEmpty(lang)) lang = "und";

string codec = "unknown";
try { codec = stream.codec_name; }
catch (Exception ex) { Console.WriteLine(ex.Message); }
Expand All @@ -158,10 +164,6 @@ public MediaInfo(string path)
try { channel = stream.channels; }
catch(Exception ex) { Console.WriteLine(ex.Message); }

string lang = "und";
try { lang = stream.tags.language; }
catch (Exception ex) { Console.WriteLine(ex.Message); }

if (bitdepth == 0) bitdepth = 16;
else if (bitdepth >= 32) bitdepth = 24;

Expand All @@ -183,13 +185,14 @@ public MediaInfo(string path)
try { id = stream.index; }
catch (Exception ex) { Console.WriteLine(ex.Message); }

string codec = "unknown";
try { codec = stream.codec_name; }
catch (Exception ex) { Console.WriteLine(ex.Message); }

string lang = "und";
try { lang = stream.tags.language; }
catch (Exception ex) { Console.WriteLine(ex.Message); }
if (string.IsNullOrEmpty(lang)) lang = "und";

string codec = "unknown";
try { codec = stream.codec_name; }
catch (Exception ex) { Console.WriteLine(ex.Message); }

Subtitle.Add(new StreamSubtitle
{
Expand Down Expand Up @@ -232,10 +235,12 @@ public static string Print(MediaInfo value)
$"Duration : {value.Duration} seconds{Environment.NewLine}" +
$"Overall bit rate : {value.BitRate} bps{Environment.NewLine}";

var video = $"{Environment.NewLine}Video{Environment.NewLine}";
var v = 0;
var video = string.Empty;
foreach (var item in value.Video)
{
video += $"ID : {item.Id}{Environment.NewLine}" +
video += $"{Environment.NewLine}Video #{v++}{Environment.NewLine}" +
$"ID : {item.Id}{Environment.NewLine}" +
$"Format : {item.Codec}{Environment.NewLine}" +
$"Width : {item.Width}{Environment.NewLine}" +
$"Height : {item.Height}{Environment.NewLine}" +
Expand All @@ -246,29 +251,35 @@ public static string Print(MediaInfo value)
$"Language : {item.Language}{Environment.NewLine}";
}

var audio = $"{Environment.NewLine}Audio{Environment.NewLine}";
var a = 0;
var audio = string.Empty;
foreach (var item in value.Audio)
{
audio += $"ID : {item.Id}{Environment.NewLine}" +
audio += $"{Environment.NewLine}Audio #{a++}{Environment.NewLine}" +
$"ID : {item.Id}{Environment.NewLine}" +
$"Format : {item.Codec}{Environment.NewLine}" +
$"Channel(s) : {item.Channel}{Environment.NewLine}" +
$"Sampling rate : {item.SampleRate} Hz{Environment.NewLine}" +
$"Bit Depth : {item.BitDepth} bits{Environment.NewLine}" +
$"Language : {item.Language}{Environment.NewLine}";
}

var subtitle = $"{Environment.NewLine}Subtitles{Environment.NewLine}";
var s = 0;
var subtitle = string.Empty;
foreach (var item in value.Subtitle)
{
subtitle += $"ID : {item.Id}{Environment.NewLine}" +
subtitle += $"{Environment.NewLine}Subtitles #{s++}{Environment.NewLine}" +
$"ID : {item.Id}{Environment.NewLine}" +
$"Format : {item.Codec}{Environment.NewLine}" +
$"Language : {item.Language}{Environment.NewLine}";
}

var attach = $"{Environment.NewLine}Attachments{Environment.NewLine}";
var t = 0;
var attach = string.Empty;
foreach (var item in value.Attachment)
{
attach += $"ID : {item.Id}{Environment.NewLine}" +
attach += $"{Environment.NewLine}Attachments #{t++}{Environment.NewLine}" +
$"ID : {item.Id}{Environment.NewLine}" +
$"Name : {item.FileName}{Environment.NewLine}" +
$"MIME : {item.MimeType}{Environment.NewLine}";
}
Expand Down
8 changes: 8 additions & 0 deletions IFME/IFME.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
<None Include="Profiles\Profile_0000-00-00_00-00-00_0004.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Profiles\Profile_0000-00-00_00-00-00_0006.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down Expand Up @@ -179,5 +182,10 @@
<ItemGroup>
<None Include="Resources\SplashScreen7A_DPI.png" />
</ItemGroup>
<ItemGroup>
<None Include="Profiles\Profile_0000-00-00_00-00-00_0005.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
42 changes: 25 additions & 17 deletions IFME/MediaEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ internal static void Audio(MediaQueue queue, string tempDir)

internal static void Video(MediaQueue queue, string tempDir)
{
var original_data = new FFmpeg.MediaInfo(queue.FilePath);

for (int i = 0; i < queue.Video.Count; i++)
{
var item = queue.Video[i];
Expand Down Expand Up @@ -152,15 +150,6 @@ internal static void Video(MediaQueue queue, string tempDir)
fps = $"-r {item.Quality.FrameRate}";
}

if (item.Quality.Width >= 128 && item.Quality.Height >= 128)
{
res = $"-s {item.Quality.Width}x{item.Quality.Height}";
}
else
{
res = $"-s {original_data.Video[i].Width}x{original_data.Video[i].Height}";
}

if (!vc.Args.Preset.IsDisable() && !item.Encoder.Preset.IsDisable())
{
preset = $"{vc.Args.Preset} {item.Encoder.Preset}";
Expand Down Expand Up @@ -188,12 +177,24 @@ internal static void Video(MediaQueue queue, string tempDir)
}

// FFmpeg Video Filter
if (item.Quality.Width >= 128 && item.Quality.Height >= 128)
{
res = $"scale={item.Quality.Width}:{item.Quality.Height}";

if (item.Quality.OriginalWidth > item.Quality.Width)
res += ":flags=lanczos";
}
else
{
res = $"scale={item.Quality.OriginalWidth}:{item.Quality.OriginalHeight}";
}

if (item.DeInterlace.Enable)
{
fi.Add($"yadif={item.DeInterlace.Mode}:{item.DeInterlace.Field}:0");
}

if (item.Quality.CommandFilter.IsDisable())
if (!item.Quality.CommandFilter.IsDisable())
{
fi.Add(item.Quality.CommandFilter);
}
Expand All @@ -218,8 +219,15 @@ internal static void Video(MediaQueue queue, string tempDir)
}
}

// Resolution filter
fi.Add(res);

// Concat multiple filter
if (fi.Count > 0)
vf = $"-vf \"{string.Join(", ", fi)}\"";
vf = $"-vf \"{string.Join(",", fi)}\"";

// Tell You
Console2.WriteLine($"[INFO] Video filter command is: {vf}");

// begin encoding
Console2.WriteLine($"[INFO] Encoding video file...");
Expand All @@ -244,9 +252,9 @@ internal static void Video(MediaQueue queue, string tempDir)
Console2.WriteLine($"[INFO] Multi-pass encoding: {p} of {item.Encoder.MultiPass}");

if (vc.Args.Pipe)
ProcessManager.Start(tempDir, $"\"{FFmpeg}\" -hide_banner -v error -i \"{item.File}\" -strict -1 {trim} -map 0:{item.Id} -f yuv4mpegpipe -pix_fmt {yuv} {res} {fps} {vf} {item.Quality.Command} - | \"{en}\" {vc.Args.Input} {vc.Args.Y4M} {preset} {quality} {tune} {bitdepth} {pass} {item.Encoder.Command} {vc.Args.Output} {outrawfile}");
ProcessManager.Start(tempDir, $"\"{FFmpeg}\" -hide_banner -v error -i \"{item.File}\" -strict -1 {trim} -map 0:{item.Id} -f yuv4mpegpipe -pix_fmt {yuv} {fps} {vf} {item.Quality.Command} - | \"{en}\" {vc.Args.Input} {vc.Args.Y4M} {preset} {quality} {tune} {bitdepth} {pass} {item.Encoder.Command} {vc.Args.Output} {outrawfile}");
else
ProcessManager.Start(tempDir, $"\"{en}\" {vc.Args.Input} \"{item.File}\" {trim} -map 0:{item.Id} -pix_fmt {yuv} {res} {fps} {vf} {vc.Args.UnPipe} {preset} {quality} {tune} {pass} {item.Encoder.Command} {vc.Args.Command} {vc.Args.Output} {outrawfile}");
ProcessManager.Start(tempDir, $"\"{en}\" {vc.Args.Input} \"{item.File}\" {trim} -map 0:{item.Id} -pix_fmt {yuv} {fps} {vf} {vc.Args.UnPipe} {preset} {quality} {tune} {pass} {item.Encoder.Command} {vc.Args.Command} {vc.Args.Output} {outrawfile}");

++p;

Expand All @@ -255,9 +263,9 @@ internal static void Video(MediaQueue queue, string tempDir)
else
{
if (vc.Args.Pipe)
ProcessManager.Start(tempDir, $"\"{FFmpeg}\" -hide_banner -v error -i \"{item.File}\" -strict -1 {trim} -map 0:{item.Id} -f yuv4mpegpipe -pix_fmt {yuv} {res} {fps} {vf} {item.Quality.Command} - | \"{en}\" {vc.Args.Input} {vc.Args.Y4M} {preset} {quality} {tune} {bitdepth} {framecount} {item.Encoder.Command} {vc.Args.Output} {outrawfile}");
ProcessManager.Start(tempDir, $"\"{FFmpeg}\" -hide_banner -v error -i \"{item.File}\" -strict -1 {trim} -map 0:{item.Id} -f yuv4mpegpipe -pix_fmt {yuv} {fps} {vf} {item.Quality.Command} - | \"{en}\" {vc.Args.Input} {vc.Args.Y4M} {preset} {quality} {tune} {bitdepth} {framecount} {item.Encoder.Command} {vc.Args.Output} {outrawfile}");
else
ProcessManager.Start(tempDir, $"\"{en}\" {vc.Args.Input} \"{item.File}\" {trim} -map 0:{item.Id} -pix_fmt {yuv} {res} {fps} {vf} {vc.Args.UnPipe} {preset} {quality} {tune} {item.Encoder.Command} {vc.Args.Output} {outrawfile}");
ProcessManager.Start(tempDir, $"\"{en}\" {vc.Args.Input} \"{item.File}\" {trim} -map 0:{item.Id} -pix_fmt {yuv} {fps} {vf} {vc.Args.UnPipe} {preset} {quality} {tune} {item.Encoder.Command} {vc.Args.Output} {outrawfile}");
}

// Raw file dont have pts (time), need to remux
Expand Down
2 changes: 2 additions & 0 deletions IFME/MediaQueue/MediaQueueParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static MediaQueueVideo Video(string path, FFmpeg.StreamVideo data)
{
Width = data.Width,
Height = data.Height,
OriginalWidth = data.Width,
OriginalHeight = data.Height,
FrameRate = (float)Math.Round(data.FrameRateAvg, 3),
FrameRateAvg = data.FrameRateAvg,
FrameCount = (int)Math.Ceiling(data.Duration * data.FrameRate),
Expand Down
2 changes: 2 additions & 0 deletions IFME/MediaQueue/MediaQueueVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class MediaQueueVideoQuality
{
public int Width { get; set; }
public int Height { get; set; }
public int OriginalWidth { get; set; }
public int OriginalHeight { get; set; }
public bool IsVFR { get; set; }
public float FrameRate { get; set; }
public float FrameRateAvg { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion IFME/Profiles/Profile_0000-00-00_00-00-00_0003.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ProfileName": "HDR to SDR YUV420 (x265 AAC)",
"ProfileName": "HDR to SDR YUV420 8bit (x265 AAC)",
"ProfileAuthor": "Anime4000",
"Container": "mkv",
"Video": {
Expand Down
2 changes: 1 addition & 1 deletion IFME/Profiles/Profile_0000-00-00_00-00-00_0004.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ProfileName": "HDR to SDR YUV444 (x265 AAC)",
"ProfileName": "HDR to SDR YUV444 8bit (x265 AAC)",
"ProfileAuthor": "Anime4000",
"Container": "mkv",
"Video": {
Expand Down
43 changes: 43 additions & 0 deletions IFME/Profiles/Profile_0000-00-00_00-00-00_0005.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"ProfileName": "HDR to SDR YUV420 10bit (x265 AAC)",
"ProfileAuthor": "Anime4000",
"Container": "mkv",
"Video": {
"Encoder": {
"Id": "deadbeef-0265-0265-0265-026502650265",
"Preset": "veryslow",
"Tune": "psnr",
"Mode": 0,
"Value": 23,
"MultiPass": 2,
"Command": "--pme --pmode"
},
"Quality": {
"Width": 0,
"Height": 0,
"FrameRate": 0,
"BitDepth": 10,
"PixelFormat": 420,
"Command": "",
"CommandFilter": "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p10le"
},
"DeInterlace": {
"Enable": false,
"Field": 0,
"Mode": 1
}
},
"Audio": {
"Encoder": {
"Id": "deadbeef-0aac-0aac-0aac-0aac0aac0aac",
"Mode": 0,
"Quality": 128,
"SampleRate": 44100,
"Channel": 2,
"Command": ""
},
"Copy": false,
"Command": "",
"CommandFilter": ""
}
}
43 changes: 43 additions & 0 deletions IFME/Profiles/Profile_0000-00-00_00-00-00_0006.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"ProfileName": "HDR to SDR YUV444 10bit (x265 AAC)",
"ProfileAuthor": "Anime4000",
"Container": "mkv",
"Video": {
"Encoder": {
"Id": "deadbeef-0265-0265-0265-026502650265",
"Preset": "veryslow",
"Tune": "psnr",
"Mode": 0,
"Value": 23,
"MultiPass": 2,
"Command": "--pme --pmode"
},
"Quality": {
"Width": 0,
"Height": 0,
"FrameRate": 0,
"BitDepth": 10,
"PixelFormat": 444,
"Command": "",
"CommandFilter": "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv444p10le"
},
"DeInterlace": {
"Enable": false,
"Field": 0,
"Mode": 1
}
},
"Audio": {
"Encoder": {
"Id": "deadbeef-0aac-0aac-0aac-0aac0aac0aac",
"Mode": 0,
"Quality": 128,
"SampleRate": 44100,
"Channel": 2,
"Command": ""
},
"Copy": false,
"Command": "",
"CommandFilter": ""
}
}
Loading

0 comments on commit ccb7cb9

Please sign in to comment.