Skip to content

Commit fc9b90d

Browse files
committed
Encoder class refactoring
1 parent 2619b60 commit fc9b90d

37 files changed

+1211
-608
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Nmkoder.Data.Codecs;
2+
using Nmkoder.Extensions;
3+
using Nmkoder.IO;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Nmkoder.Data
11+
{
12+
class CodecUtils
13+
{
14+
//public enum CodecType { Video, AnimImage, Image, Audio }
15+
16+
public enum Av1anCodec { AomAv1, SvtAv1, VpxVp9, X265 };
17+
public enum VideoCodec { CopyVideo, StripVideo, H264, H265, H264Nvenc, H265Nvenc, Vp9, Av1, Gif, Png, Jpg };
18+
public enum AudioCodec { CopyAudio, StripAudio, Aac, Opus, Mp3, Flac };
19+
public enum SubtitleCodec { CopySubs, StripSubs, MovText, Srt, WebVtt };
20+
21+
public static IEncoder GetCodec(VideoCodec c)
22+
{
23+
if (c == VideoCodec.StripVideo) return new StripVideo();
24+
if (c == VideoCodec.CopyVideo) return new CopyVideo();
25+
if (c == VideoCodec.H264) return new H264();
26+
if (c == VideoCodec.H264Nvenc) return new H264Nvenc();
27+
if (c == VideoCodec.H265) return new H265();
28+
if (c == VideoCodec.H265Nvenc) return new H265Nvenc();
29+
if (c == VideoCodec.Vp9) return new Vp9();
30+
if (c == VideoCodec.Av1) return new Av1();
31+
if (c == VideoCodec.Gif) return new Gif();
32+
if (c == VideoCodec.Png) return new Png();
33+
if (c == VideoCodec.Jpg) return new Jpg();
34+
return null;
35+
}
36+
37+
public static IEncoder GetCodec(Av1anCodec c)
38+
{
39+
if (c == Av1anCodec.AomAv1) return new AomAv1();
40+
if (c == Av1anCodec.SvtAv1) return new SvtAv1();
41+
if (c == Av1anCodec.VpxVp9) return new VpxVp9();
42+
if (c == Av1anCodec.X265) return new X265();
43+
return null;
44+
}
45+
46+
public static IEncoder GetCodec(AudioCodec c)
47+
{
48+
if (c == AudioCodec.StripAudio) return new StripAudio();
49+
if (c == AudioCodec.CopyAudio) return new CopyAudio();
50+
if (c == AudioCodec.Aac) return new Aac();
51+
if (c == AudioCodec.Opus) return new Opus();
52+
if (c == AudioCodec.Mp3) return new Mp3();
53+
if (c == AudioCodec.Flac) return new Flac();
54+
return null;
55+
}
56+
57+
public static IEncoder GetCodec(SubtitleCodec c)
58+
{
59+
if (c == SubtitleCodec.StripSubs) return new StripSubs();
60+
if (c == SubtitleCodec.CopySubs) return new CopySubs();
61+
if (c == SubtitleCodec.MovText) return new MovText();
62+
if (c == SubtitleCodec.Srt) return new Srt();
63+
if (c == SubtitleCodec.WebVtt) return new WebVtt();
64+
return null;
65+
}
66+
67+
public static string GetKeyIntArg(MediaFile mediaFile, int intervalSeconds, string arg = "-g ")
68+
{
69+
if (mediaFile == null || mediaFile.VideoStreams.Count < 1)
70+
return "";
71+
72+
int keyInt = ((float)(mediaFile?.VideoStreams.FirstOrDefault().Rate.GetFloat() * intervalSeconds)).RoundToInt();
73+
return keyInt >= 24 ? $"{arg}{keyInt}" : "";
74+
}
75+
}
76+
}

ff-utils-winforms/Data/Codecs.cs

Lines changed: 0 additions & 462 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Nmkoder.IO;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Nmkoder.Data.Codecs
6+
{
7+
class Aac : IEncoder
8+
{
9+
public Streams.Stream.StreamType Type { get; } = Streams.Stream.StreamType.Audio;
10+
public string Name { get; } = "Aac";
11+
public string FriendlyName { get; } = "AAC (Advanced Audio Coding)";
12+
public string[] Presets { get; } = new string[] { };
13+
public int PresetDefault { get; }
14+
public string[] ColorFormats { get; }
15+
public int ColorFormatDefault { get; }
16+
public int QMin { get; } = 8;
17+
public int QMax { get; } = 640;
18+
public int QDefault { get; } = 144;
19+
public string QInfo { get; }
20+
public string PresetInfo { get; }
21+
22+
public bool DoesNotEncode { get; } = false;
23+
public bool IsFixedFormat { get; } = false;
24+
public bool IsSequence { get; } = false;
25+
26+
public CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null)
27+
{
28+
string bitrate = encArgs.ContainsKey("bitrate") ? encArgs["bitrate"] : "128k";
29+
string channels = encArgs.ContainsKey("ac") ? encArgs["ac"] : "2";
30+
return new CodecArgs($"-c:a aac -b:a {bitrate}k -aac_coder twoloop -ac {channels}");
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Nmkoder.Data.Codecs
8+
{
9+
class CopyAudio : IEncoder
10+
{
11+
public Streams.Stream.StreamType Type { get; } = Streams.Stream.StreamType.Audio;
12+
public string Name { get; } = "CopyAudio";
13+
public string FriendlyName { get; } = "Copy Audio Without Re-Encoding";
14+
public string[] Presets { get; } = new string[] { };
15+
public int PresetDefault { get; }
16+
public string[] ColorFormats { get; }
17+
public int ColorFormatDefault { get; }
18+
public int QMin { get; }
19+
public int QMax { get; }
20+
public int QDefault { get; }
21+
public string QInfo { get; } = "Does not alter quality.";
22+
public string PresetInfo { get; }
23+
24+
public bool DoesNotEncode { get; } = true;
25+
public bool IsFixedFormat { get; } = false;
26+
public bool IsSequence { get; } = false;
27+
28+
public CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null)
29+
{
30+
return new CodecArgs($"-c:a copy");
31+
}
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Nmkoder.IO;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Nmkoder.Data.Codecs
6+
{
7+
class Flac : IEncoder
8+
{
9+
public Streams.Stream.StreamType Type { get; } = Streams.Stream.StreamType.Audio;
10+
public string Name { get; } = "Flac";
11+
public string FriendlyName { get; } = "FLAC (Free Lossless Audio Coding)";
12+
public string[] Presets { get; } = new string[] { };
13+
public int PresetDefault { get; }
14+
public string[] ColorFormats { get; }
15+
public int ColorFormatDefault { get; }
16+
public int QMin { get; }
17+
public int QMax { get; }
18+
public int QDefault { get; }
19+
public string QInfo { get; }
20+
public string PresetInfo { get; }
21+
22+
public bool DoesNotEncode { get; } = false;
23+
public bool IsFixedFormat { get; } = false;
24+
public bool IsSequence { get; } = false;
25+
26+
public CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null)
27+
{
28+
string channels = encArgs.ContainsKey("ac") ? encArgs["ac"] : "2";
29+
return new CodecArgs($"-c:a flac -ac {channels}");
30+
}
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Nmkoder.IO;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Nmkoder.Data.Codecs
6+
{
7+
class Mp3 : IEncoder
8+
{
9+
public Streams.Stream.StreamType Type { get; } = Streams.Stream.StreamType.Audio;
10+
public string Name { get; } = "Mp3";
11+
public string FriendlyName { get; } = "MP3";
12+
public string[] Presets { get; }
13+
public int PresetDefault { get; }
14+
public string[] ColorFormats { get; }
15+
public int ColorFormatDefault { get; }
16+
public int QMin { get; } = 32;
17+
public int QMax { get; } = 1280;
18+
public int QDefault { get; } = 320;
19+
public string QInfo { get; }
20+
public string PresetInfo { get; }
21+
22+
public bool DoesNotEncode { get; } = false;
23+
public bool IsFixedFormat { get; } = false;
24+
public bool IsSequence { get; } = false;
25+
26+
public CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null)
27+
{
28+
string bitrate = encArgs.ContainsKey("bitrate") ? encArgs["bitrate"] : "320k";
29+
string channels = encArgs.ContainsKey("ac") ? encArgs["ac"] : "2";
30+
return new CodecArgs($"-c:a libmp3lame -b:a {bitrate}k -ac {channels}");
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Nmkoder.IO;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Nmkoder.Data.Codecs
6+
{
7+
class Opus : IEncoder
8+
{
9+
public Streams.Stream.StreamType Type { get; } = Streams.Stream.StreamType.Audio;
10+
public string Name { get; } = "Opus";
11+
public string FriendlyName { get; } = "Opus";
12+
public string[] Presets { get; }
13+
public int PresetDefault { get; }
14+
public string[] ColorFormats { get; }
15+
public int ColorFormatDefault { get; }
16+
public int QMin { get; } = 8;
17+
public int QMax { get; } = 640;
18+
public int QDefault { get; } = 128;
19+
public string QInfo { get; }
20+
public string PresetInfo { get; }
21+
22+
public bool DoesNotEncode { get; } = false;
23+
public bool IsFixedFormat { get; } = false;
24+
public bool IsSequence { get; } = false;
25+
26+
public CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null)
27+
{
28+
string bitrate = encArgs.ContainsKey("bitrate") ? encArgs["bitrate"] : "96k";
29+
string channels = encArgs.ContainsKey("ac") ? encArgs["ac"] : "2";
30+
return new CodecArgs($"-c:a libopus -b:a {bitrate}k -ac {channels}");
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Nmkoder.Data.Codecs
8+
{
9+
class StripAudio : IEncoder
10+
{
11+
public Streams.Stream.StreamType Type { get; } = Streams.Stream.StreamType.Audio;
12+
public string Name { get; } = "StripAudio";
13+
public string FriendlyName { get; } = "Disable (Strip Audio)";
14+
public string[] Presets { get; }
15+
public int PresetDefault { get; }
16+
public string[] ColorFormats { get; }
17+
public int ColorFormatDefault { get; }
18+
public int QMin { get; }
19+
public int QMax { get; }
20+
public int QDefault { get; }
21+
public string QInfo { get; }
22+
public string PresetInfo { get; }
23+
24+
public bool DoesNotEncode { get; } = true;
25+
public bool IsFixedFormat { get; } = false;
26+
public bool IsSequence { get; } = false;
27+
28+
public CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null)
29+
{
30+
return new CodecArgs($"-an");
31+
}
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Nmkoder.Data.Codecs
8+
{
9+
interface IEncoder
10+
{
11+
Streams.Stream.StreamType Type { get; }
12+
string Name { get; }
13+
string FriendlyName { get; }
14+
string[] Presets { get; }
15+
int PresetDefault { get; }
16+
string[] ColorFormats { get; }
17+
int ColorFormatDefault { get; }
18+
int QMin { get; }
19+
int QMax { get; }
20+
int QDefault { get; }
21+
string QInfo { get; }
22+
string PresetInfo { get; }
23+
24+
bool DoesNotEncode { get; }
25+
bool IsFixedFormat { get; }
26+
bool IsSequence { get; }
27+
28+
CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null);
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Nmkoder.Data.Codecs
8+
{
9+
class CopySubs : IEncoder
10+
{
11+
public Streams.Stream.StreamType Type { get; } = Streams.Stream.StreamType.Subtitle;
12+
public string Name { get; } = "CopySubtitles";
13+
public string FriendlyName { get; } = "Copy Subtitles Without Re-Encoding";
14+
public string[] Presets { get; } = new string[] { };
15+
public int PresetDefault { get; }
16+
public string[] ColorFormats { get; }
17+
public int ColorFormatDefault { get; }
18+
public int QMin { get; }
19+
public int QMax { get; }
20+
public int QDefault { get; }
21+
public string QInfo { get; }
22+
public string PresetInfo { get; }
23+
24+
public bool DoesNotEncode { get; } = true;
25+
public bool IsFixedFormat { get; } = false;
26+
public bool IsSequence { get; } = false;
27+
28+
public CodecArgs GetArgs(Dictionary<string, string> encArgs = null, MediaFile mediaFile = null)
29+
{
30+
return new CodecArgs($"-c:v copy");
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)