/// <summary> /// Permette di convertire una Lista di Nodi partendo dai file sorgenti specificati nel file index e come destinazione ha la DestFolder /// Viene usato il ConversionParameter per specificare come convertire i media /// </summary> /// <param name="ListaNodi">Lista dei Nodi da convertire</param> /// <param name="IndexFileSorgente"> IndexFile dei media sorgenti</param> /// <param name="DestFolder"> Cartella di destinazione dei media</param> /// <param name="cp">Parametri di conversione</param> public void ConvertPlaylist(IEnumerable<FileSystemNodePlus<MyAddittionalData>> ListaNodi, IndexFile IndexFileSorgente, String DestFolder, ConversionParameter cp) { if (GUI) StartGui(); //seleziono effettivamente tutti i file IEnumerable<FileSystemNodePlus<MyAddittionalData>> i = ListaNodi.Where(x => x.Type == FileSystemNodePlusType.File); bool Abort = false; if (CanUseGui) { CMForm.SetPause += (bool pau) => { Pause = pau; }; CMForm.Stop += () => { Abort = true; }; CMForm.SetProgressTotalMax(i.Count()); } foreach (FileSystemNodePlus<MyAddittionalData> n in i) { while (Pause && !Abort) Thread.Sleep(100); if (Abort) break; FileSystemNodePlus<MyAddittionalData> tt = IndexFileSorgente.RootFileSystem.FindFirst((x) => { return x.AddittionalData.MD5 == n.AddittionalData.MD5; }); if (tt == null) { //TODO: non trovato -> gestire la ricerca dei file non trovati } else { if (cp.TipoConversione == ConversinType.Mai) { String PathSource = IndexFileSorgente.RootFileSystem.GetFullPath(tt); String PathDestination = SystemService.Combine(DestFolder, (n.GetFullPath().TrimStart('\\', '/'))); if (CanUseGui) CMForm.StartConvertItem(PathSource, PathDestination); OnStartConverting?.Invoke(PathSource, PathDestination); SystemService.CopySecure(PathSource, PathDestination, cp.OverrideIfExist, (double Percent, ref bool cancelFlag) => { if (CanUseGui) CMForm.UpdateProgressPartial((int)Percent); OnProgressConverting?.Invoke(PathSource, PathDestination, Percent); }, (bool copiato, Exception ex) => { OnEndConverting?.Invoke(PathSource, PathDestination, copiato ? ConvertMediaError.nul : ConvertMediaError.ErrDuranteLaCopia); }); } else { String PathSource = IndexFileSorgente.RootFileSystem.GetFullPath(tt); String PathDestination = SystemService.Combine(DestFolder, SystemService.ChangeExtension(n.GetFullPath().TrimStart('\\', '/'), cp.ConvertiIn.GetDefaultExtension())); ConvertionEntity Source = new ConvertionEntity(PathSource, tt.AddittionalData.Metadata.MediaMetadata); ConvertionEntity Dest = new ConvertionEntity(PathDestination, cp.ConvertiIn); bool ForceConversion = cp.TipoConversione == ConversinType.Sempre ? true : false; bool Err = false; FFmpeg.ConvertTo(Source, Dest, ForceConversion, cp.OverrideIfExist, (st, src, des) => { if (st == FFmpegStatus.Running) { if (CanUseGui) CMForm.StartConvertItem(src, des); OnStartConverting?.Invoke(src, des); } }, (Percent, src, Destination, Error) => { if (Error == FFmpegError.DestFolderNotFound) { Err = true; if (CanUseGui) CMForm.AppendLog("[ERR] File non trovato " + src); OnEndConverting?.Invoke(src, Destination, ConvertMediaError.FileNonTrovato); } else { if (CanUseGui) CMForm.UpdateProgressPartial(Percent); OnProgressConverting?.Invoke(src, Destination, Percent); } }, false); if (!Err) OnEndConverting?.Invoke(PathSource, PathDestination, ConvertMediaError.nul); } } } if (CanUseGui) CMForm.Finito(); }
object ICloneablePlus.Clone() { ConvertionEntity t = new ConvertionEntity(Path, MediaMetadata.CloneClass()); return t; }
private void I_OnEnd2(IndexFile i) { FileSystemNodePlus<MyAddittionalData> n = i.RootFileSystem.FindFirst((x) => { return x.Name.StartsWith("Summertime"); }); ConvertionEntity s = new ConvertionEntity(i.RootFileSystem.GetFullPath(n), n.AddittionalData.Metadata.MediaMetadata); FFMpegMediaMetadataFlac mm = new FFMpegMediaMetadataFlac(n.AddittionalData.Metadata.MediaMetadata); mm.SamplingRate = SamplingRateInfo._44100; mm.Bit = BitInfo._24; ConvertionEntity d = new ConvertionEntity(SystemService.ChangeExtension(i.RootFileSystem.GetFullPath(n), "flac"),mm); FFmpeg.ConvertTo(s, d, true, true, (status, source, dest) => { if (status == FFmpegStatus.Stop) MessageBox.Show(source + " -> " + dest + "\r\n FINE!"); }, (percent, source, dest, error) => { }, false); }
/// <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; }