BizHawk/BizHawk.Client.DiscoHawk/AudioExtractor.cs

59 lines
1.5 KiB
C#
Raw Normal View History

2020-01-25 19:43:59 +00:00
using System.Windows.Forms;
using System.IO;
using BizHawk.Emulation.DiscSystem;
2011-09-03 04:01:06 +00:00
namespace BizHawk.Client.DiscoHawk
2011-09-03 04:01:06 +00:00
{
2020-01-25 19:43:59 +00:00
public class AudioExtractor
{
public static string FFmpegPath;
2011-09-03 04:01:06 +00:00
2020-01-25 19:43:59 +00:00
public static void Extract(Disc disc, string path, string fileBase)
{
2015-06-23 18:57:11 +00:00
var dsr = new DiscSectorReader(disc);
bool confirmed = false;
2015-07-08 03:29:11 +00:00
var tracks = disc.Session1.Tracks;
foreach (var track in tracks)
{
if (!track.IsAudio)
continue;
2011-09-03 04:01:06 +00:00
2015-07-08 03:29:11 +00:00
int trackLength = track.NextTrack.LBA - track.LBA;
var waveData = new byte[trackLength * 2352];
int startLba = track.LBA;
2015-07-08 03:29:11 +00:00
for (int sector = 0; sector < trackLength; sector++)
2020-01-25 19:43:59 +00:00
{
2015-06-23 18:57:11 +00:00
dsr.ReadLBA_2352(startLba + sector, waveData, sector * 2352);
2020-01-25 19:43:59 +00:00
}
2011-09-03 04:01:06 +00:00
2020-01-25 19:43:59 +00:00
string mp3Path = $"{Path.Combine(path, fileBase)} - Track {track.Number:D2}.mp3";
if (File.Exists(mp3Path))
{
if (!confirmed)
{
var dr = MessageBox.Show("This file already exists. Do you want extraction to proceed overwriting files, or cancel the entire operation immediately?", "File already exists", MessageBoxButtons.OKCancel);
if (dr == DialogResult.Cancel) return;
confirmed = true;
}
2020-01-25 19:43:59 +00:00
File.Delete(mp3Path);
}
string tempfile = Path.GetTempFileName();
try
{
File.WriteAllBytes(tempfile, waveData);
2011-09-04 05:54:50 +00:00
var ffmpeg = new FFMpeg();
ffmpeg.Run("-f", "s16le", "-ar", "44100", "-ac", "2", "-i", tempfile, "-f", "mp3", "-ab", "192k", mp3Path);
}
finally
{
File.Delete(tempfile);
}
}
}
}
2011-09-03 04:01:06 +00:00
}