Ejemplo n.º 1
0
 static async Task <bool> To_MP3(int File_Number, string To_Dir, bool IsFromFileDelete)
 {
     if (!File.Exists(From_Files[File_Number]))
     {
         return(false);
     }
     try
     {
         string To_Audio_File = To_Dir + "\\" + Path.GetFileNameWithoutExtension(From_Files[File_Number]) + ".mp3";
         await Task.Run(() =>
         {
             string Ex = Path.GetExtension(From_Files[File_Number]);
             if (Ex == ".ogg")
             {
                 using (NAudio.Vorbis.VorbisWaveReader reader = new NAudio.Vorbis.VorbisWaveReader(From_Files[File_Number]))
                     using (LameMP3FileWriter wtr = new LameMP3FileWriter(To_Audio_File, reader.WaveFormat, 128))
                         reader.CopyTo(wtr);
             }
             else if (Ex == ".wav")
             {
                 using (WaveFileReader reader = new WaveFileReader(From_Files[File_Number]))
                     using (LameMP3FileWriter wtr = new LameMP3FileWriter(To_Audio_File, reader.WaveFormat, 128))
                         reader.CopyTo(wtr);
             }
             else if (Ex == ".flac")
             {
                 using (NAudio.Flac.FlacReader reader = new NAudio.Flac.FlacReader(From_Files[File_Number]))
                     using (LameMP3FileWriter wtr = new LameMP3FileWriter(To_Audio_File, reader.WaveFormat, 128))
                         reader.CopyTo(wtr);
             }
             else if (Ex == ".aac" || Ex == ".wma" || Ex == ".mp4" || Ex == ".webm")
             {
                 using (MediaFoundationReader reader = new MediaFoundationReader(From_Files[File_Number]))
                     using (LameMP3FileWriter wtr = new LameMP3FileWriter(To_Audio_File, reader.WaveFormat, 128))
                         reader.CopyTo(wtr);
             }
             else
             {
                 Un4seen.Bass.Misc.EncoderLAME mc = new Un4seen.Bass.Misc.EncoderLAME(0);
                 mc.EncoderDirectory = Voice_Set.Special_Path + "/Encode_Mp3";
                 mc.InputFile        = From_Files[File_Number];
                 mc.OutputFile       = To_Audio_File;
                 mc.LAME_Bitrate     = (int)Un4seen.Bass.Misc.EncoderLAME.BITRATE.kbps_160;
                 mc.LAME_Mode        = Un4seen.Bass.Misc.EncoderLAME.LAMEMode.Default;
                 mc.LAME_Quality     = Un4seen.Bass.Misc.EncoderLAME.LAMEQuality.Q2;
                 Un4seen.Bass.Misc.BaseEncoder.EncodeFile(mc, null, true, false, true);
             }
             if (IsFromFileDelete)
             {
                 File.Delete(From_Files[File_Number]);
             }
         });
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 2
0
 public static TimeSpan GetFileDurationFlac(string file)
 {
     using (var fs = File.OpenRead(file))
     using (var reader = new NAudio.Flac.FlacReader(fs))
     {
         return reader.TotalTime;
     }
 }
Ejemplo n.º 3
0
        public static byte[] Flac_to_wav(string fil_n, byte ch, out byte[] mono)
        {
            byte[] mono2 = null;
            Task   a     = Task.Run(() =>
            {
                WaveFormat format = new WaveFormat(48000, 16, 1);
                try
                {
                    using (NAudio.Flac.FlacReader reader = new NAudio.Flac.FlacReader(fil_n))
                    {
                        using (WaveFormatConversionStream stream = new WaveFormatConversionStream(format, reader))
                        {
                            mono2 = new byte[stream.Length];
                            stream.Read(mono2, 0, mono2.Length);
                        }
                    }
                }
                catch (Exception) { }
            });

            byte[] buffer = null;
            try
            {
                WaveFormat format = new WaveFormat(48000, 16, ch);
                using (NAudio.Flac.FlacReader reader = new NAudio.Flac.FlacReader(fil_n))
                {
                    using (WaveFormatConversionStream stream = new WaveFormatConversionStream(format, reader))
                    {
                        buffer = new byte[stream.Length];
                        stream.Read(buffer, 0, buffer.Length);
                    }
                }
            }
            catch (Exception) { }
            a.Wait();
            mono = mono2;
            return(buffer);
        }
Ejemplo n.º 4
0
        static async Task <bool> To_WAV(int File_Number, string To_Dir, bool IsFromFileDelete, bool IsUseFFmpeg, bool IsUseBass)
        {
            if (!File.Exists(From_Files[File_Number]))
            {
                return(false);
            }
            if (IsUseFFmpeg)
            {
                if (IsUseBass)
                {
                    string To_Audio_File           = To_Dir + "\\" + Path.GetFileNameWithoutExtension(From_Files[File_Number]) + ".wav";
                    Un4seen.Bass.Misc.EncoderWAV w = new Un4seen.Bass.Misc.EncoderWAV(0);
                    w.InputFile         = From_Files[File_Number];
                    w.OutputFile        = To_Audio_File;
                    w.WAV_BitsPerSample = 24;
                    w.Start(null, IntPtr.Zero, false);
                    w.Stop();
                    if (IsFromFileDelete)
                    {
                        File.Delete(From_Files[File_Number]);
                    }
                }
                else
                {
                    string       Encode_Style = "-y -vn -ac 2 -ar 44100 -acodec pcm_s24le -f wav";
                    StreamWriter stw          = File.CreateText(Voice_Set.Special_Path + "/Encode_Mp3/Audio_Encode" + File_Number + ".bat");
                    stw.WriteLine("chcp 65001");
                    stw.Write("\"" + Voice_Set.Special_Path + "/Encode_Mp3/ffmpeg.exe\" -i \"" + From_Files[File_Number] + "\" " + Encode_Style + " \"" + To_Dir + "\\" +
                              Path.GetFileNameWithoutExtension(From_Files[File_Number]) + ".wav\"");
                    stw.Close();
                    ProcessStartInfo processStartInfo = new ProcessStartInfo
                    {
                        FileName        = Voice_Set.Special_Path + "/Encode_Mp3/Audio_Encode" + File_Number + ".bat",
                        CreateNoWindow  = true,
                        UseShellExecute = false
                    };
                    Process p = Process.Start(processStartInfo);
                    await Task.Run(() =>
                    {
                        p.WaitForExit();
                        if (IsFromFileDelete)
                        {
                            File.Delete(From_Files[File_Number]);
                        }
                        File.Delete(Voice_Set.Special_Path + "/Encode_Mp3/Audio_Encode" + File_Number + ".bat");
                    });
                }
            }
            else if (IsUseBass)
            {
                string To_Audio_File           = To_Dir + "\\" + Path.GetFileNameWithoutExtension(From_Files[File_Number]) + ".wav";
                Un4seen.Bass.Misc.EncoderWAV w = new Un4seen.Bass.Misc.EncoderWAV(0);
                w.InputFile         = From_Files[File_Number];
                w.OutputFile        = To_Audio_File;
                w.WAV_BitsPerSample = 24;
                w.Start(null, IntPtr.Zero, false);
                w.Stop();
                if (IsFromFileDelete)
                {
                    File.Delete(From_Files[File_Number]);
                }
            }
            else
            {
                int    Number        = 0;
                string To_Audio_File = To_Dir + "\\" + Path.GetFileNameWithoutExtension(From_Files[File_Number]) + ".wav";
                await Task.Run(() =>
                {
start:
                    try
                    {
                        string Ex = Path.GetExtension(From_Files[File_Number]);
                        if (Ex == ".ogg")
                        {
                            using (NAudio.Vorbis.VorbisWaveReader reader = new NAudio.Vorbis.VorbisWaveReader(From_Files[File_Number]))
                                using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                                    WaveFileWriter.CreateWaveFile(To_Audio_File, pcmStream);
                        }
                        else if (Ex == ".mp3")
                        {
                            using (NAudio.Wave.Mp3FileReader reader = new NAudio.Wave.Mp3FileReader(From_Files[File_Number]))
                                using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                                    WaveFileWriter.CreateWaveFile(To_Audio_File, pcmStream);
                        }
                        else if (Ex == ".flac")
                        {
                            using (NAudio.Flac.FlacReader reader = new NAudio.Flac.FlacReader(From_Files[File_Number]))
                                using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                                    WaveFileWriter.CreateWaveFile(To_Audio_File, pcmStream);
                        }
                        else if (Ex == ".aac" || Ex == ".wma" || Ex == ".mp4")
                        {
                            using (MediaFoundationReader reader = new MediaFoundationReader(From_Files[File_Number]))
                                WaveFileWriter.CreateWaveFile(To_Audio_File, reader);
                        }
                        else if (Ex == ".wav")
                        {
                            using (WaveFileReader reader = new WaveFileReader(From_Files[File_Number]))
                                using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                                    WaveFileWriter.CreateWaveFile(To_Audio_File, pcmStream);
                        }
                        if (IsFromFileDelete)
                        {
                            File.Delete(From_Files[File_Number]);
                        }
                    }
                    catch (Exception e)
                    {
                        if (Number < 4)
                        {
                            Number++;
                            //goto文...許してください...
                            goto start;
                        }
                        else
                        {
                            Sub_Code.Error_Log_Write(e.Message);
                        }
                    }
                });
            }
            return(true);
        }