2011-09-03 04:01:06 +00:00
using System ;
2011-09-04 06:15:41 +00:00
using System.Windows.Forms ;
2011-09-03 04:01:06 +00:00
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using BizHawk.DiscSystem ;
using System.IO ;
using System.Diagnostics ;
namespace BizHawk
{
class AudioExtractor
{
public static string FFmpegPath ;
public static void Extract ( Disc disc , string path , string filebase )
{
2011-09-04 06:15:41 +00:00
bool confirmed = false ;
2011-09-03 04:01:06 +00:00
var tracks = disc . TOC . Sessions [ 0 ] . Tracks ;
foreach ( var track in tracks )
{
if ( track . TrackType ! = ETrackType . Audio )
continue ;
var waveData = new byte [ track . length_aba * 2352 ] ;
int startLba = track . Indexes [ 1 ] . LBA ;
for ( int sector = 0 ; sector < track . length_aba ; sector + + )
disc . ReadLBA_2352 ( startLba + sector , waveData , sector * 2352 ) ;
2011-09-04 06:15:41 +00:00
string mp3Path = string . Format ( "{0} - Track {1:D2}.mp3" , Path . Combine ( path , filebase ) , track . num ) ;
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 ;
}
File . Delete ( mp3Path ) ;
}
2011-09-04 05:53:09 +00:00
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 ) ;
2011-09-04 05:53:09 +00:00
}
finally
{
File . Delete ( tempfile ) ;
}
2011-09-03 04:01:06 +00:00
}
}
}
}