mirror of
https://github.com/4sval/FModel.git
synced 2026-05-06 04:47:18 -05:00
Convert audio via temp file
This commit is contained in:
parent
b378ccb26a
commit
fd4051b163
|
|
@ -340,12 +340,8 @@ public class AudioPlayerViewModel : ViewModel, ISource, IDisposable
|
|||
Directory.CreateDirectory(path.SubstringBeforeLast('/'));
|
||||
}
|
||||
|
||||
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
|
||||
using (var writer = new BinaryWriter(stream))
|
||||
{
|
||||
writer.Write(fileToSave.Data);
|
||||
writer.Flush();
|
||||
}
|
||||
using var stream = new FileStream(path, FileMode.Create, FileAccess.Write);
|
||||
stream.Write(fileToSave.Data);
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
|
|
@ -684,25 +680,11 @@ public class AudioPlayerViewModel : ViewModel, ISource, IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(inputFilePath.SubstringBeforeLast("/"));
|
||||
File.WriteAllBytes(inputFilePath, inputFileData);
|
||||
var success = TryConvertToWAV(inputFilePath, inputFileData, vgmFilePath, true, out var tempWavFilePath);
|
||||
|
||||
wavFilePath = Path.ChangeExtension(inputFilePath, ".wav");
|
||||
var vgmProcess = Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = vgmFilePath,
|
||||
Arguments = $"-o \"{wavFilePath}\" \"{inputFilePath}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
});
|
||||
vgmProcess?.WaitForExit(5000);
|
||||
|
||||
File.Delete(inputFilePath);
|
||||
|
||||
var success = vgmProcess?.ExitCode == 0 && File.Exists(wavFilePath);
|
||||
if (!success)
|
||||
{
|
||||
Log.Error("Failed to convert {InputFilePath} to .wav format", inputFilePath);
|
||||
Log.Error("Failed to convert {InputFilePath} to .wav format", Path.GetFileName(inputFilePath));
|
||||
if (updateUi)
|
||||
{
|
||||
FLogger.Append(ELog.Error, () =>
|
||||
|
|
@ -731,20 +713,37 @@ public class AudioPlayerViewModel : ViewModel, ISource, IDisposable
|
|||
return false;
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(SelectedAudioFile.FilePath.SubstringBeforeLast("/"));
|
||||
File.WriteAllBytes(SelectedAudioFile.FilePath, SelectedAudioFile.Data);
|
||||
return TryConvertToWAV(SelectedAudioFile.FilePath, SelectedAudioFile.Data, decoderPath, false, out rawFilePath);
|
||||
}
|
||||
|
||||
rawFilePath = Path.ChangeExtension(SelectedAudioFile.FilePath, ".wav");
|
||||
var decoderProcess = Process.Start(new ProcessStartInfo
|
||||
private static bool TryConvertToWAV(string inputFilePath, byte[] inputFileData, string converterPath, bool usevgmstream, out string wavFilePath)
|
||||
{
|
||||
wavFilePath = Path.ChangeExtension(inputFilePath, ".wav");
|
||||
var directory = Path.GetDirectoryName(inputFilePath);
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
var tempfile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + Path.GetExtension(inputFilePath));
|
||||
File.WriteAllBytes(tempfile, inputFileData);
|
||||
|
||||
var tempWavFilePath = Path.ChangeExtension(tempfile, ".wav");
|
||||
|
||||
var process = Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = decoderPath,
|
||||
Arguments = $"-i \"{SelectedAudioFile.FilePath}\" -o \"{rawFilePath}\"",
|
||||
FileName = converterPath,
|
||||
Arguments = usevgmstream ? $"-o \"{tempWavFilePath}\" \"{tempfile}\"" : $"-i \"{tempfile}\" -o \"{tempWavFilePath}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
});
|
||||
decoderProcess?.WaitForExit(5000);
|
||||
process?.WaitForExit(5000);
|
||||
|
||||
File.Delete(SelectedAudioFile.FilePath);
|
||||
return decoderProcess?.ExitCode == 0 && File.Exists(rawFilePath);
|
||||
File.Delete(tempfile);
|
||||
|
||||
var success = process?.ExitCode == 0 && File.Exists(tempWavFilePath);
|
||||
if (success)
|
||||
{
|
||||
File.Move(tempWavFilePath, wavFilePath, true);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user