Skip to content

Commit

Permalink
Add Import Audio Option #108
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkl58 committed Oct 30, 2022
1 parent 04c288d commit 0c069f6
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 155 deletions.
2 changes: 2 additions & 0 deletions NotEnoughAV1Encodes/Audio/AudioTracks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public class AudioTracks
public List<string> Languages { get; set; }
public string CustomName { get; set; }
public bool PCM { get; set; }
public bool External { get; set; }
public string ExternalPath { get; set; }
}
}
33 changes: 23 additions & 10 deletions NotEnoughAV1Encodes/Audio/CommandGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public string Generate(System.Windows.Controls.ItemCollection tracks)
{
string audioCommand = "";
int endIndex = 0;
int externalIndex = 0;
bool copyaudio = false;
bool noaudio = true;

Expand All @@ -14,7 +15,12 @@ public string Generate(System.Windows.Controls.ItemCollection tracks)
// Skip Audio Track if not active
if(track.Active == false) continue;

audioCommand += MultipleTrackCommandGenerator(int.Parse(track.Bitrate), track.Index, endIndex, track.Codec, track.Channels, track.Language, track.CustomName, track.PCM);
if (track.External)
{
externalIndex += 1;
}

audioCommand += MultipleTrackCommandGenerator(int.Parse(track.Bitrate), track.Index, endIndex, track.Codec, track.Channels, track.Language, track.CustomName, track.PCM, track.External, externalIndex);
endIndex += 1;

if (track.Codec == 5)
Expand Down Expand Up @@ -48,26 +54,33 @@ private static string SwitchCodec(int _audioCodec, bool _pcmBluray)
return audioCodeSwitch;
}

private string MultipleTrackCommandGenerator(int _activeTrackBitrate, int _mapIndex, int _endIndex, int _activTrackCodec, int _channelLayout, string _language, string _trackName, bool _pcmBluray)
private string MultipleTrackCommandGenerator(int activeTrackBitrate, int mapIndex, int endIndex, int activTrackCodec, int channelLayout, string language, string trackName, bool pcmBluray, bool external, int externalIndex)
{
string audioCodecCommand;

// Audio Mapping
audioCodecCommand = " -map 0:a:" + _mapIndex + " -c:a:" + _endIndex;
audioCodecCommand = " -map 0:a:" + mapIndex + " -c:a:" + endIndex;

if (external)
{
audioCodecCommand = " -map " + externalIndex + ":a:" + mapIndex + " -c:a:" + endIndex;
}

// Codec
audioCodecCommand += SwitchCodec(_activTrackCodec, _pcmBluray);
audioCodecCommand += SwitchCodec(activTrackCodec, pcmBluray);
// Bitrate
if (_activTrackCodec != 5)
if (activTrackCodec != 5)
{
audioCodecCommand += " -b:a:" + _endIndex + " " + _activeTrackBitrate + "k";
audioCodecCommand += " -b:a:" + endIndex + " " + activeTrackBitrate + "k";
}
// Channel Layout
audioCodecCommand += " -ac:a:" + _endIndex + " " + SetChannelLayout(_channelLayout);
audioCodecCommand += " -ac:a:" + endIndex + " " + SetChannelLayout(channelLayout);
// Metadata
audioCodecCommand += " -metadata:s:a:" + _endIndex + " language=" + resources.MediaLanguages.Languages[_language];
audioCodecCommand += " -metadata:s:a:" + endIndex + " language=" + resources.MediaLanguages.Languages[language];
// Title
if (_activTrackCodec != 5)
if (activTrackCodec != 5)
{
audioCodecCommand += " -metadata:s:a:" + _endIndex + " title=" + '\u0022' + _trackName + '\u0022';
audioCodecCommand += " -metadata:s:a:" + endIndex + " title=" + '\u0022' + trackName + '\u0022';
}
return audioCodecCommand;
}
Expand Down
9 changes: 8 additions & 1 deletion NotEnoughAV1Encodes/Audio/EncodeAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ public void Encode(Queue.QueueElement queueElement, CancellationToken _token)

Global.Logger("INFO - EncodeAudio.Encode() => Command: " + queueElement.AudioCommand, queueElement.Output + ".log");

string externalInput = "";
foreach (AudioTracks audioTrack in queueElement.VideoDB.AudioTracks)
{
if (!audioTrack.External) continue;
externalInput += " -i \"" + audioTrack.ExternalPath + "\"";
}

Process processAudio = new();
ProcessStartInfo startInfo = new()
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Apps", "FFmpeg"),
Arguments = "/C ffmpeg.exe -i \"" + queueElement.VideoDB.InputPath + "\" -vn -sn -map_metadata -1 " + queueElement.AudioCommand + " \"" + Path.Combine(Global.Temp, "NEAV1E", queueElement.UniqueIdentifier, "Audio", "audio.mkv") + "\"",
Arguments = "/C ffmpeg.exe -i \"" + queueElement.VideoDB.InputPath + "\" " + externalInput + " -vn -sn -map_metadata -1 " + queueElement.AudioCommand + " \"" + Path.Combine(Global.Temp, "NEAV1E", queueElement.UniqueIdentifier, "Audio", "audio.mkv") + "\"",
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true
Expand Down
9 changes: 9 additions & 0 deletions NotEnoughAV1Encodes/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,15 @@
<Label x:Name="LabelNoAudioFound" Content="{lex:Loc}" HorizontalContentAlignment="Center" FontSize="20" />
</Grid>
</ListBoxItem>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem x:Name="AudioTracksImport" Header="{lex:Loc}" Click="AudioTracksImport_Click">
<MenuItem.Icon>
<Image Source="resources/img/folder.png" Height="20" Width="20"></Image>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="{Binding ElementName=ListBoxAudioTracks, Path=Background, UpdateSourceTrigger=PropertyChanged}"></Setter>
Expand Down
32 changes: 32 additions & 0 deletions NotEnoughAV1Encodes/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,38 @@ private void ListBoxQueue_KeyDown(object sender, KeyEventArgs e)
DeleteQueueItems();
}
}

private void AudioTracksImport_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openAudioFilesDialog = new()
{
Filter = "Audio Files|*.mp3;*.aac;*.flac;*.m4a;*.ogg;*.opus;*.wav;*.wma|All Files|*.*",
Multiselect = true
};

bool? result = openAudioFilesDialog.ShowDialog();
if (result == true)
{
List<Audio.AudioTracks> AudioTracks = new();
if (ListBoxAudioTracks.ItemsSource != null)
{
AudioTracks = (List<Audio.AudioTracks>) ListBoxAudioTracks.ItemsSource;
}
foreach (string file in openAudioFilesDialog.FileNames)
{
Debug.WriteLine(file);
AudioTracks.Add(videoDB.ParseMediaInfoAudio(file, PresetSettings));
}

try { ListBoxAudioTracks.Items.Clear(); } catch { }
try { ListBoxAudioTracks.ItemsSource = null; } catch { }
try { ListBoxSubtitleTracks.Items.Clear(); } catch { }
try { ListBoxSubtitleTracks.ItemsSource = null; } catch { }

videoDB.AudioTracks = AudioTracks;
ListBoxAudioTracks.ItemsSource = AudioTracks;
}
}
#endregion

#region UI Functions
Expand Down
77 changes: 77 additions & 0 deletions NotEnoughAV1Encodes/Video/VideoDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,82 @@ public void ParseMediaInfo(VideoSettings settings)
InputFileName = Path.GetFileName(InputPath);
}
}

public Audio.AudioTracks ParseMediaInfoAudio(string inputPath, VideoSettings settings)
{
Audio.AudioTracks audioTrack = new();

audioTrack.External = true;
audioTrack.ExternalPath = inputPath;

MediaInfo mediaInfo = new();
mediaInfo.Open(inputPath);

try
{
int i = 0;

bool pcm = false;
try { pcm = mediaInfo.Get(StreamKind.Audio, i, "Format") == "PCM" && mediaInfo.Get(StreamKind.Audio, i, "MuxingMode") == "Blu-ray"; } catch { }

string name = Path.GetFileNameWithoutExtension(inputPath);

int channels = 1;
try { channels = int.Parse(mediaInfo.Get(StreamKind.Audio, i, "Channels")); } catch { }

int bitrate = 128;
int codec = 0;
switch (channels)
{
case 1:
channels = 0;
bitrate = settings.AudioBitrateMono;
codec = settings.AudioCodecMono;
break;
case 2:
channels = 1;
bitrate = settings.AudioBitrateStereo;
codec = settings.AudioCodecStereo;
break;
case 6:
channels = 2;
bitrate = settings.AudioBitrateSixChannel;
codec = settings.AudioCodecSixChannel;
break;
case 8:
channels = 3;
bitrate = settings.AudioBitrateEightChannel;
codec = settings.AudioCodecEightChannel;
break;
default:
break;
}

string lang = "und";
try
{
lang = mediaInfo.Get(StreamKind.Audio, i, "Language/String3");
if (!resources.MediaLanguages.Languages.ContainsValue(lang))
{
lang = "und";
}
lang = resources.MediaLanguages.Languages.FirstOrDefault(x => x.Value == lang).Key;
}
catch { }

audioTrack.Active = true;
audioTrack.Index = i;
audioTrack.Codec = codec;
audioTrack.Bitrate = bitrate.ToString();
audioTrack.Languages = resources.MediaLanguages.LanguageKeys;
audioTrack.Language = lang;
audioTrack.CustomName = name;
audioTrack.Channels = channels;
audioTrack.PCM = pcm;
}
catch { }

return audioTrack;
}
}
}
Loading

0 comments on commit 0c069f6

Please sign in to comment.