Skip to content

Commit 714ef78

Browse files
committed
Allowed Audio Tracks to be muxed into MKA container.
1 parent c8e9f23 commit 714ef78

File tree

6 files changed

+98
-63
lines changed

6 files changed

+98
-63
lines changed

OKEGui/OKEGui/Job/AudioJob/AudioJob.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ namespace OKEGui
44
{
55
class AudioJob : Job
66
{
7-
public string Language;
8-
public int Bitrate;
7+
public readonly AudioInfo Info;
98

10-
public AudioJob(string codec) : base(codec)
9+
public AudioJob(AudioInfo info) : base(info.OutputCodec)
1110
{
12-
11+
Info = info;
1312
}
1413

1514
public override JobType GetJobType()

OKEGui/OKEGui/JobProcessor/Demuxer/EACDemuxer.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.IO;
55
using System.Text.RegularExpressions;
66
using System.Threading;
7+
using OKEGui.JobProcessor;
78
using OKEGui.Model;
9+
using OKEGui.Utils;
810

911
namespace OKEGui
1012
{
@@ -49,6 +51,7 @@ public EacOutputTrackType(TrackCodec codec, string rawOutput, string extension,
4951
private ManualResetEvent mre = new ManualResetEvent(false);
5052
private ProcessState state;
5153
private string sourceFile;
54+
private List<AudioInfo> JobAudio;
5255

5356
private static List<EacOutputTrackType> s_eacOutputs = new List<EacOutputTrackType> {
5457
new EacOutputTrackType(TrackCodec.RAW_PCM, "RAW/PCM", "flac", true, TrackType.Audio),
@@ -63,10 +66,12 @@ public EacOutputTrackType(TrackCodec codec, string rawOutput, string extension,
6366
new EacOutputTrackType(TrackCodec.Chapter, "Chapters", "txt", true, TrackType.Chapter),
6467
};
6568

66-
public EACDemuxer(string eacPath, string fileName)
69+
public EACDemuxer(string eacPath, string fileName, List<AudioInfo> jobAudio)
6770
{
6871
_eacPath = eacPath;
6972
sourceFile = fileName;
73+
JobAudio = new List<AudioInfo>();
74+
JobAudio.AddRange(jobAudio);
7075
}
7176

7277
private void StartEac(string arguments, bool asyncRead)
@@ -252,17 +257,45 @@ public MediaFile Extract(Action<double, EACProgressType> progressCallback)
252257

253258
var args = new List<string>();
254259
var extractResult = new List<TrackInfo>();
260+
List<TrackInfo> srcAudio = new List<TrackInfo>();
255261

256-
foreach (var track in tracks)
262+
foreach (TrackInfo track in tracks)
257263
{
258-
if (!s_eacOutputs.Find(val => val.Codec == track.Codec).Extract)
264+
if (track.Type == TrackType.Audio)
265+
{
266+
srcAudio.Add(track);
267+
}
268+
}
269+
if (srcAudio.Count != JobAudio.Count)
270+
{
271+
OKETaskException ex = new OKETaskException(Constants.audioNumMismatchSmr);
272+
ex.progress = 0.0;
273+
ex.Data["SRC_TRACK"] = srcAudio.Count;
274+
ex.Data["DST_TRACK"] = JobAudio.Count;
275+
throw ex;
276+
}
277+
int audioId = 0;
278+
foreach (TrackInfo track in tracks)
279+
{
280+
EacOutputTrackType trackType = s_eacOutputs.Find(val => val.Codec == track.Codec);
281+
if (!trackType.Extract)
259282
{
260283
continue;
261284
};
285+
if (track.Type == TrackType.Audio)
286+
{
287+
AudioInfo jobAudioInfo = JobAudio[audioId++];
288+
if (jobAudioInfo.MuxOption == MuxOption.Skip)
289+
{
290+
continue;
291+
}
292+
}
262293

263294
args.Add($"{track.Index}:\"{track.OutFileName}\"");
264295
extractResult.Add(track);
265296
}
297+
JobAudio.RemoveAll(info => info.MuxOption == MuxOption.Skip);
298+
266299
state = ProcessState.ExtractStream;
267300
StartEac($"\"{sourceFile}\" {string.Join(" ", args)}", true);
268301

@@ -314,12 +347,15 @@ public MediaFile Extract(Action<double, EACProgressType> progressCallback)
314347
}
315348

316349
MediaFile mf = new MediaFile();
350+
audioId = 0;
317351
foreach (var item in extractResult)
318352
{
319353
OKEFile file = new OKEFile(item.OutFileName);
320354
if (item.Type == TrackType.Audio)
321355
{
322-
mf.AddTrack(new AudioTrack(file, new AudioInfo { DupOrEmpty = item.DupOrEmpty }));
356+
AudioInfo info = JobAudio[audioId++];
357+
info.DupOrEmpty = item.DupOrEmpty;
358+
mf.AddTrack(new AudioTrack(file, info));
323359
}
324360
else if (item.Type == TrackType.Subtitle)
325361
{

OKEGui/OKEGui/JobProcessor/Muxer/AutoMuxer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private Episode GenerateEpisode(
108108
episode.OutputFile = outputFileName;
109109
switch (Path.GetExtension(outputFileName).ToLower()) {
110110
case ".mkv":
111+
case ".mka":
111112
episode.OutputFileType = OutputType.Mkv;
112113
break;
113114

@@ -133,8 +134,11 @@ private string GenerateMkvMergeParameter(Episode episode)
133134
// parameters.Add("--ui-language zh_CN");
134135
parameters.Add($"--output \"{episode.OutputFile}\"");
135136

136-
parameters.Add(string.Format(trackTemplate, "und", episode.VideoFile));
137-
trackOrder.Add($"{fileID++}:0");
137+
if (episode.VideoFile != null)
138+
{
139+
parameters.Add(string.Format(trackTemplate, "und", episode.VideoFile));
140+
trackOrder.Add($"{fileID++}:0");
141+
}
138142

139143
for (int i = 0; i < episode.AudioFiles.Count; i++) {
140144
string audioFile = episode.AudioFiles[i];
@@ -192,6 +196,7 @@ private void StartProcess(string filename, string arguments)
192196
};
193197

194198
proc.StartInfo = startInfo;
199+
Debugger.Log(0, "", filename + " " + arguments + "\n");
195200

196201
try {
197202
proc.Start();
@@ -301,7 +306,7 @@ public OKEFile StartMuxing(string path, MediaFile mediaFile)
301306
List<string> subtitleLanguages = new List<string>();
302307

303308
foreach (var track in mediaFile.Tracks) {
304-
if (track.Info.MuxOption != MuxOption.Default) {
309+
if (track.Info.MuxOption != MuxOption.Default && track.Info.MuxOption != MuxOption.Mka) {
305310
continue;
306311
}
307312
switch (track.TrackType)

OKEGui/OKEGui/TaskDetail.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ public string WorkerName
249249
/// 输出文件结构
250250
/// </summary>
251251
public MediaFile MediaOutFile = new MediaFile();
252+
public MediaFile MkaOutFile = new MediaFile();
252253

253254
public JobProfile Profile;
254255

OKEGui/OKEGui/Worker/WorkerManager.cs

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
203203
while (isRunning)
204204
{
205205
TaskDetail task = args.taskManager.GetNextTask();
206-
JobProfile profile = task.Profile;
207206

208207
// 检查是否已经完成全部任务
209208
if (task == null)
@@ -225,6 +224,7 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
225224
return;
226225
}
227226

227+
JobProfile profile = task.Profile;
228228
try
229229
{
230230
task.WorkerName = args.Name;
@@ -240,7 +240,7 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
240240
ex.progress = 0.0;
241241
throw ex;
242242
}
243-
MediaFile srcTracks = new EACDemuxer(eacInfo.FullName, task.InputFile).Extract(
243+
MediaFile srcTracks = new EACDemuxer(eacInfo.FullName, task.InputFile, profile.AudioTracks).Extract(
244244
(double progress, EACProgressType type) =>
245245
{
246246
switch (type)
@@ -266,39 +266,27 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
266266
});
267267

268268
// 新建音频处理工作
269-
if (srcTracks.AudioTracks.Count != profile.AudioTracks.Count)
270-
{
271-
OKETaskException ex = new OKETaskException(Constants.audioNumMismatchSmr);
272-
ex.progress = 0.0;
273-
ex.Data["SRC_TRACK"] = srcTracks.AudioTracks.Count;
274-
ex.Data["DST_TRACK"] = profile.AudioTracks.Count;
275-
throw ex;
276-
}
277-
else
278-
{
279-
for (int i = 0; i < srcTracks.AudioTracks.Count; i++)
280-
{
281-
profile.AudioTracks[i].DupOrEmpty |= srcTracks.AudioTracks[i].Info.DupOrEmpty;
282-
}
283-
}
284-
285269
for (int id = 0; id < srcTracks.AudioTracks.Count; id++)
286270
{
287271
AudioTrack track = srcTracks.AudioTracks[id];
288-
MuxOption option = profile.AudioTracks[id].MuxOption;
289-
if (option != MuxOption.Default)
272+
OKEFile file = track.File;
273+
AudioInfo info = track.Info as AudioInfo;
274+
MuxOption option = info.MuxOption;
275+
switch (option)
290276
{
291-
continue;
277+
case MuxOption.Default:
278+
case MuxOption.Mka:
279+
case MuxOption.External:
280+
AudioJob audioJob = new AudioJob(info);
281+
audioJob.SetUpdate(task);
282+
audioJob.Input = file.GetFullPath();
283+
audioJob.Output = Path.ChangeExtension(audioJob.Input, "." + audioJob.CodecString.ToLower());
284+
285+
task.JobQueue.Enqueue(audioJob);
286+
break;
287+
default:
288+
break;
292289
}
293-
294-
AudioJob audioJob = new AudioJob(profile.AudioTracks[id].OutputCodec);
295-
audioJob.SetUpdate(task);
296-
297-
audioJob.Input = track.File.GetFullPath();
298-
audioJob.Language = profile.AudioTracks[id].Language;
299-
audioJob.Bitrate = profile.AudioTracks[id].Bitrate;
300-
301-
task.JobQueue.Enqueue(audioJob);
302290
}
303291

304292
// 新建视频处理工作
@@ -353,46 +341,41 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
353341
if (job is AudioJob)
354342
{
355343
AudioJob audioJob = job as AudioJob;
344+
AudioInfo info = audioJob.Info as AudioInfo;
356345
string srcFmt = Path.GetExtension(audioJob.Input).ToUpper().Remove(0, 1);
357346
if (srcFmt == "FLAC" && audioJob.CodecString == "AAC")
358347
{
359348
task.CurrentStatus = "音频转码中";
360349
task.IsUnKnowProgress = true;
361350

362-
AudioJob aEncode = new AudioJob("AAC");
363-
aEncode.Input = audioJob.Input;
364-
aEncode.Output = Path.ChangeExtension(audioJob.Input, ".aac");
365-
QAACEncoder qaac = new QAACEncoder(aEncode, audioJob.Bitrate > 0 ? audioJob.Bitrate : Utils.Constants.QAACBitrate);
351+
QAACEncoder qaac = new QAACEncoder(audioJob, info.Bitrate);
366352

367353
qaac.start();
368354
qaac.waitForFinish();
369-
370-
audioJob.Output = aEncode.Output;
371355
}
372-
else if (srcFmt == audioJob.CodecString)
373-
{
374-
audioJob.Output = audioJob.Input;
375-
}
376-
else
356+
else if (srcFmt != audioJob.CodecString)
377357
{
378358
OKETaskException ex = new OKETaskException(Constants.audioFormatMistachSmr);
379359
ex.Data["SRC_FMT"] = srcFmt;
380360
ex.Data["DST_FMT"] = audioJob.CodecString;
381361
throw ex;
382362
}
383363

384-
var audioFileInfo = new FileInfo(audioJob.Output);
385-
if (audioFileInfo.Length < 1024)
364+
OKEFile outputFile = new OKEFile(job.Output);
365+
switch (info.MuxOption)
386366
{
387-
// 无效音轨
388-
File.Move(audioJob.Output, Path.ChangeExtension(audioJob.Output, ".bak") + audioFileInfo.Extension);
389-
continue;
367+
case MuxOption.Default:
368+
task.MediaOutFile.AddTrack(new AudioTrack(outputFile, info));
369+
break;
370+
case MuxOption.Mka:
371+
task.MkaOutFile.AddTrack(new AudioTrack(outputFile, info));
372+
break;
373+
case MuxOption.External:
374+
outputFile.AddCRC32();
375+
break;
376+
default:
377+
break;
390378
}
391-
392-
AudioInfo info = new AudioInfo();
393-
info.Language = audioJob.Language;
394-
395-
task.MediaOutFile.AddTrack(new AudioTrack(new OKEFile(job.Output), info));
396379
}
397380
else if (job is VideoJob)
398381
{
@@ -452,6 +435,17 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
452435

453436
muxer.StartMuxing(Path.GetDirectoryName(task.InputFile) + "\\" + task.OutputFile, task.MediaOutFile);
454437
}
438+
if (task.MkaOutFile.Tracks.Count > 0)
439+
{
440+
task.CurrentStatus = "封装MKA中";
441+
FileInfo mkvInfo = new FileInfo(".\\tools\\mkvtoolnix\\mkvmerge.exe");
442+
FileInfo lsmash = new FileInfo(".\\tools\\l-smash\\muxer.exe");
443+
AutoMuxer muxer = new AutoMuxer(mkvInfo.FullName, lsmash.FullName);
444+
muxer.ProgressChanged += progress => task.ProgressValue = progress;
445+
string mkaOutputFile = task.InputFile + ".mka";
446+
447+
muxer.StartMuxing(mkaOutputFile, task.MkaOutFile);
448+
}
455449

456450
task.CurrentStatus = "完成";
457451
task.ProgressValue = 100;

OKEGui/OKEGui/demo_720p.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},{
1313
"TrackId" : 1,
1414
"OutputCodec" : "flac",
15-
"SkipMuxing" : true
15+
"MuxOption" : "Skip"
1616
}],
1717
"InputScript" : "demo_720p.vpy",
1818
"Fps" : 23.976

0 commit comments

Comments
 (0)