/*public static void ConvertWavStreamToMp3File(ref MemoryStream ms, string savetofilename) * { * //rewind to beginning of stream * ms.Seek(0, SeekOrigin.Begin); * * using (var retMs = new MemoryStream()) * using (var rdr = new WaveFileReader(ms)) * using (var wtr = new LameMP3FileWriter(savetofilename, rdr.WaveFormat, LAMEPreset.VBR_90)) * { * rdr.CopyTo(wtr); * }*/ public void cnv(string sourceFilename, string targetFilename) { using (var reader = new NAudio.Wave.AudioFileReader(sourceFilename)) using (var writer = new NAudio.Lame.LameMP3FileWriter(targetFilename, reader.WaveFormat, NAudio.Lame.LAMEPreset.STANDARD)) { reader.CopyTo(writer); } }
public static void ConvertToMP3(string inFile, string outFile, int bitRate = 64) { using (var reader = new AudioFileReader(inFile)) { using (var writer = new LameMP3FileWriter(outFile, reader.WaveFormat, bitRate)) reader.CopyTo(writer); } }
private static void WmaToMp3(XConvertJob param) { var targetFilename = string.IsNullOrEmpty(param.AlternativeOutputPath) ? param.SourceFileName.GenerateOutPutPath(XFileType.Mp3) : param.AlternativeOutputPath; if (param.SourceData != null) { File.WriteAllBytes(param.SourceFileName, param.SourceData); } using (var reader = new NAudio.Wave.AudioFileReader(param.SourceFileName)) using (var writer = new NAudio.Lame.LameMP3FileWriter(targetFilename, reader.WaveFormat, NAudio.Lame.LAMEPreset.STANDARD)) { reader.CopyTo(writer); } param.ResulFileName = targetFilename; }
private void ProcessVideo(SelectableViewModel selectedItem) { var videoInfo = DownloadUrlResolver.GetDownloadUrls(selectedItem.URL); // The long part. VideoInfo video = null; int[] bitrate = {0}; // Another ugly hack. foreach ( var _video in videoInfo.Where(_video => _video.AudioExtension == ".aac") .Where(_video => _video.AudioBitrate > bitrate[0])) { bitrate[0] = _video.AudioBitrate; video = _video; } if (video != null && video.RequiresDecryption) { DownloadUrlResolver.DecryptDownloadUrl(video); // Some fancy shit I don't know what it does but it's in the example app so I added it here. } var localFile = Path.Combine($"{Settings.Default.dlDir}/{video?.Title + video?.AudioExtension}"); var videoDownloader = new VideoDownloader(video, localFile); // Prepare for video downloading. Dispatcher.Invoke(() => selectedItem.Progress = ProgressBar.Update(0)); // Initialise our lovely progress bar. videoDownloader.DownloadProgressChanged += (s, a) => Dispatcher.Invoke(() => { selectedItem.Progress = ProgressBar.Update(a.ProgressPercentage); }); try { videoDownloader.Execute(); } catch (Exception ex) { if (ex.Message == "The given path's format is not supported.") // Title most probably contains some NTFS-incompatible chars. { MessageBox.Show("Invalid filename detected, please choose a new filename in the next window.", "LeafyYT"); var dialog = new VistaSaveFileDialog { Title = @"LeafyYT", CheckFileExists = true, ValidateNames = true, InitialDirectory = Settings.Default.dlDir, AddExtension = true, DefaultExt = ".aac" }; dialog.ShowDialog(); // Should probably send this back into a method instead of creating new ones... if (!string.IsNullOrEmpty(dialog.FileName)) { localFile = Path.Combine($"{Settings.Default.dlDir}/{dialog.FileName + video?.AudioExtension}"); var videoDownloader2 = new VideoDownloader(video, localFile); Dispatcher.Invoke(() => selectedItem.Progress = ProgressBar.Update(0)); videoDownloader2.DownloadProgressChanged += (s, a) => Dispatcher.Invoke( () => { selectedItem.Progress = ProgressBar.Update(a.ProgressPercentage); }); try { videoDownloader2.Execute(); } catch (Exception ex2) { MessageBox.Show($"EXCEPTION: {ex2.GetType()}: {ex2.Message}"); return; } } } else { MessageBox.Show($"EXCEPTION: {ex.GetType()}: {ex.Message}"); return; } } Dispatcher.Invoke(() => { selectedItem.Progress = "Converting..."; }); var oldFile = localFile.Replace("/", "\\"); // LameWriter doesn't seem to like / in filePath. var newFile = oldFile.Replace(".aac", "") + ".mp3"; // lol. using (var reader = new AudioFileReader(oldFile)) { if (video != null) { using (var writer = new LameMP3FileWriter(newFile, reader.WaveFormat, video.AudioBitrate)) { reader.CopyTo(writer); // Convert aac to mp3 using some black magic shit. } } } File.Delete(localFile); Dispatcher.Invoke(() => { selectedItem.Progress = "Finished"; }); // Tadaaaaaaaaaaaa. }