public static bool FlacToMp3(String Input,String Output,FFMpegMediaMetadataMp3 ConversionParameters, bool OverrideIfExist,FFmpegConvertStatusChanged OnStatusChanged=null, FFmpegConvertProgressChanged OnProgressChanged=null, bool Async = true ) { if (!_Loaded) return false; if (ConversionParameters == null || ConversionParameters.SamplingRate == SamplingRateInfo.nul || ConversionParameters.SamplingRate == 0 || ConversionParameters.BitRateMp3 == 0) return false; if (CheckValidInput(Input) && CheckValidOutput(Output)) { if (File.Exists(Output)) { if (OverrideIfExist) File.Delete(Output); else return true; } MyProcess p = new MyProcess(_PathFFmpeg, "-i \"" + Input + "\" -map 0:0 -map 0:1? -c:a:0 libmp3lame -ab "+ ConversionParameters.BitRateMp3 + "k -ar "+ConversionParameters.SamplingRate.ToStringReplace("_","")+" -map_metadata 0 -id3v2_version 3 -c:v copy \"" + Output + "\""); if (OnStatusChanged != null) { p.OnStatusChanged += (ProcessStatus s) => { if (s == ProcessStatus.Running) OnStatusChanged(FFmpegStatus.Running, Input, Output); else if (s == ProcessStatus.Stop) OnStatusChanged(FFmpegStatus.Stop, Input, Output); }; } if (OnProgressChanged != null) { bool AspettaLaDurata = false; bool AspettaProgress = false; long TotalMilliSec = 0; p.OnNewLine += (string line) => { if (line == null) return; else if (line.StartsWith("Input")) { AspettaLaDurata = true; AspettaProgress = false; } else if (AspettaLaDurata && line.StartsWith(" Duration:")) { line = line.RemoveLeft(" Duration: "); line = line.SplitAndGetFirst(','); string[] ss=line.Split(':', '.'); if (ss.Length == 4) { AspettaLaDurata = false; AspettaProgress = true; TotalMilliSec = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; } else { TotalMilliSec = -1; AspettaLaDurata = false; AspettaProgress = false; } } else if(AspettaProgress && line.Contains("No such file or directory")) { OnProgressChanged(-1, Input, Output,FFmpegError.DestFolderNotFound); } else if (AspettaProgress && ( line.StartsWith("frame") || line.StartsWith("size") )) { line = line.Substring(line.IndexOf("time=") + 5, 11); string[] ss = line.Split(':', '.'); if (ss.Length == 4) { long current = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; OnProgressChanged((int)((double)current / TotalMilliSec * 100), Input, Output); } } }; } p.UseShellExecute = false; p.RedirectStandardOutput = true; p.RedirectStandardError = true; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; SystemService.CreateFolderSecure(SystemService.GetParent(Output)); p.Async = Async; p.Start(); } else return false; return true; }
public static bool Mp3ToFlac(String Input, String Output, FFMpegMediaMetadataFlac ConversionParameters, bool OverrideIfExist, FFmpegConvertStatusChanged OnStatusChanged = null, FFmpegConvertProgressChanged OnProgressChanged = null, bool Async = true) { bool ret = true; Thread t = new Thread(() => { if (!_Loaded) { ret = false; return; } if (ConversionParameters == null || ConversionParameters.SamplingRate == SamplingRateInfo.nul || ConversionParameters.SamplingRate == 0 || ConversionParameters.Bit == BitInfo.nul) { ret = false; return; } if (CheckValidInput(Input) && CheckValidOutput(Output)) { if (File.Exists(Output)) { if (OverrideIfExist) File.Delete(Output); else { ret = false; return; } } if (OnStatusChanged != null) { OnStatusChanged(FFmpegStatus.Running, Input, Output); } MyProcess p = new MyProcess(_PathFFmpeg, "-i \"" + Input + "\" -map 0:1? -c copy " + JpgNameTemp+" -y"); p.UseShellExecute = false; p.RedirectStandardOutput = false; p.RedirectStandardError = false; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Async = false; p.Start(); p = new MyProcess(_PathFFmpeg, "-i \"" + Input + "\" -map 0:0 -c:a:0 flac -map_metadata 0 -id3v2_version 3 -ar " + ConversionParameters.SamplingRate.ToStringReplace("_", "") + " \"" + Output + "\""); if (OnProgressChanged != null) { bool AspettaLaDurata = false; bool AspettaProgress = false; long TotalMilliSec = 0; p.OnNewLine += (string line) => { if (line == null) return; else if (line.StartsWith("Input")) { AspettaLaDurata = true; AspettaProgress = false; } else if (AspettaLaDurata && line.StartsWith(" Duration:")) { line = line.RemoveLeft(" Duration: "); line = line.SplitAndGetFirst(','); string[] ss = line.Split(':', '.'); if (ss.Length == 4) { AspettaLaDurata = false; AspettaProgress = true; TotalMilliSec = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; } else { TotalMilliSec = -1; AspettaLaDurata = false; AspettaProgress = false; } } else if (AspettaProgress && line.Contains("No such file or directory")) { OnProgressChanged(-1, Input, Output, FFmpegError.DestFolderNotFound); } else if (AspettaProgress && (line.StartsWith("frame") || line.StartsWith("size"))) { line = line.Substring(line.IndexOf("time=") + 5, 11); string[] ss = line.Split(':', '.'); if (ss.Length == 4) { long current = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; OnProgressChanged((int)((double)current / TotalMilliSec * 100), Input, Output); } } }; } p.UseShellExecute = false; p.RedirectStandardOutput = true; p.RedirectStandardError = true; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; SystemService.CreateFolderSecure(SystemService.GetParent(Output)); p.Async = false; p.Start(); if (SystemService.FileExist(JpgNameTemp)) { p = new MyProcess(_PathMetaflac, "--import-picture-from=\"" + JpgNameTemp + "\" \"" + Output+"\""); p.UseShellExecute = false; p.RedirectStandardOutput = false; p.RedirectStandardError = false; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Async = false; p.Start(); } if (OnStatusChanged != null) { OnStatusChanged(FFmpegStatus.Stop, Input, Output); } } else { ret = false; return; } ret = true; }); t.Start(); if (!Async) t.Join(); return ret; /* ffmpeg.exe -i "07. Magic Box - Scream My Name (Radio Edit).mp3" -map 0:1? -c copy OUT.jpg ffmpeg.exe -i "07. Magic Box - Scream My Name (Radio Edit).mp3" -map 0:0 -c:a: 0 flac -map_metadata 0 -id3v2_version 3 out.flac metaflac --import-picture-from="OUT.jpg" out.flac */ }
/// <summary> /// Consente di convertire un media da un formato all'altro, di default, un file lossy in lossless ( o da un lossy peggiore ad uno migliore ) /// </summary> /// <param name="Source"> File sorgente </param> /// <param name="Destination"> file di destinazione ( i MediaMetadati verranno usati come parametri di conversione )</param> /// <param name="ForceConvertion">permette di forzare la conversione (viene ignorato l'ottimizzazione di conversione lossy -> lossless </param> /// <param name="OverrideIfExist"> permette di cancellare il file destinazione, se presente</param> /// <param name="OnStatusChanged"></param> /// <param name="OnProgressChanged"></param> /// <param name="Async"> Lancia il comando in modalità asincrona</param> /// <returns></returns> public static bool ConvertTo(ConvertionEntity Source, ConvertionEntity Destination,bool ForceConvertion, bool OverrideIfExist, FFmpegConvertStatusChanged OnStatusChanged = null, FFmpegConvertProgressChanged OnProgressChanged = null, bool Async = true) { if (!ForceConvertion) { if (Source.MediaMetadata is FFMpegMediaMetadataMp3) { if (Destination.MediaMetadata is FFMpegMediaMetadataMp3) { if ((Source.MediaMetadata as FFMpegMediaMetadataMp3).BitRateMp3 < (Destination.MediaMetadata as FFMpegMediaMetadataMp3).BitRateMp3) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b= SystemService.CopySecure(Source.Path, Destination.Path, OverrideIfExist, (double percent, ref bool cancelFlag)=> { OnProgressChanged?.Invoke((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex)=> { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path,FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } } else if (Destination.MediaMetadata is FFMpegMediaMetadataFlac) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b = SystemService.CopySecure(Source.Path, SystemService.ChangeExtension(Destination.Path, "mp3"), OverrideIfExist, (double percent, ref bool cancelFlag) => { if (OnProgressChanged != null) OnProgressChanged((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex) => { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path, FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } else if (Destination.MediaMetadata is FFMpegMediaMetadataWav) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b = SystemService.CopySecure(Source.Path, SystemService.ChangeExtension(Destination.Path, "mp3"), OverrideIfExist, (double percent, ref bool cancelFlag) => { if (OnProgressChanged != null) OnProgressChanged((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex) => { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path, FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } } else if (Source.MediaMetadata is FFMpegMediaMetadataFlac) { if (Destination.MediaMetadata is FFMpegMediaMetadataFlac) { if ((Source.MediaMetadata as FFMpegMediaMetadataFlac).SamplingRate < (Destination.MediaMetadata as FFMpegMediaMetadataFlac).SamplingRate) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b = SystemService.CopySecure(Source.Path, Destination.Path, OverrideIfExist, (double percent, ref bool cancelFlag) => { if (OnProgressChanged != null) OnProgressChanged((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex) => { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path, FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } else if ((Source.MediaMetadata as FFMpegMediaMetadataFlac).Bit < (Destination.MediaMetadata as FFMpegMediaMetadataFlac).Bit) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b = SystemService.CopySecure(Source.Path, Destination.Path, OverrideIfExist, (double percent, ref bool cancelFlag) => { if (OnProgressChanged != null) OnProgressChanged((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex) => { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path, FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } } } else if (Source.MediaMetadata is FFMpegMediaMetadataWav) { if (Destination.MediaMetadata is FFMpegMediaMetadataWav) { if ((Source.MediaMetadata as FFMpegMediaMetadataWav).SamplingRate < (Destination.MediaMetadata as FFMpegMediaMetadataWav).SamplingRate) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b = SystemService.CopySecure(Source.Path, Destination.Path, OverrideIfExist, (double percent, ref bool cancelFlag) => { if (OnProgressChanged != null) OnProgressChanged((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex) => { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path, FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } } } } //TODO: implementare le altre conversioni ( wav ) if ( Source.MediaMetadata is FFMpegMediaMetadataFlac) { FFMpegMediaMetadataFlac ts = (Source.MediaMetadata as FFMpegMediaMetadataFlac); if (Destination.MediaMetadata is FFMpegMediaMetadataMp3) return FlacToMp3(Source.Path, Destination.Path,(Destination.MediaMetadata as FFMpegMediaMetadataMp3), OverrideIfExist, OnStatusChanged, OnProgressChanged, Async); else if (Destination.MediaMetadata is FFMpegMediaMetadataFlac) { FFMpegMediaMetadataFlac td = (Destination.MediaMetadata as FFMpegMediaMetadataFlac); if (ts.Bit == td.Bit && ts.SamplingRate == td.SamplingRate) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b = SystemService.CopySecure(Source.Path, Destination.Path, OverrideIfExist, (double percent, ref bool cancelFlag) => { if (OnProgressChanged != null) OnProgressChanged((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex) => { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path, FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } else { return Mp3ToFlac(Source.Path, Destination.Path, td, OverrideIfExist, OnStatusChanged, OnProgressChanged, Async); } } } else if (Source.MediaMetadata is FFMpegMediaMetadataMp3) { FFMpegMediaMetadataMp3 ts = (Source.MediaMetadata as FFMpegMediaMetadataMp3); if (Destination.MediaMetadata is FFMpegMediaMetadataFlac) return Mp3ToFlac(Source.Path, Destination.Path, (Destination.MediaMetadata as FFMpegMediaMetadataFlac), OverrideIfExist, OnStatusChanged, OnProgressChanged, Async); else if (Destination.MediaMetadata is FFMpegMediaMetadataMp3) { FFMpegMediaMetadataMp3 td = (Destination.MediaMetadata as FFMpegMediaMetadataMp3); if (ts.BitRateMp3 == td.BitRateMp3 && ts.SamplingRate == td.SamplingRate) { OnStatusChanged?.Invoke(FFmpegStatus.Running, Source.Path, Destination.Path); bool b = SystemService.CopySecure(Source.Path, Destination.Path, OverrideIfExist, (double percent, ref bool cancelFlag) => { if (OnProgressChanged != null) OnProgressChanged((int)percent, Source.Path, Destination.Path); }, (bool copiato, Exception ex) => { if (copiato) OnProgressChanged?.Invoke(100, Source.Path, Destination.Path); else OnProgressChanged?.Invoke(0, Source.Path, Destination.Path, FFmpegError.CopyError); }); OnStatusChanged?.Invoke(FFmpegStatus.Stop, Source.Path, Destination.Path); return b; } else { return FlacToMp3(Source.Path, Destination.Path, td, OverrideIfExist, OnStatusChanged, OnProgressChanged, Async); } } } return false; }