omansak / libvideo

A lightweight .NET library to download YouTube videos.
BSD 2-Clause "Simplified" License
553 stars 163 forks source link

can get mp4 video to work fine with vb.net... having trouble with getting audio/ converting to mp3, #117

Closed Syth-1 closed 4 years ago

Syth-1 commented 5 years ago

as said in the title i'm having trouble converting a video to .mp3 in visual basic .net, maybe its a lack of programming skills... but i've been at this for 4 hours and scavenged the internet left right and centre and still got nothing.

any help/ pointers will be appreciated.

sammeboy635 commented 5 years ago

I use the CSCORE library by Florian R. to convert my mp4 to mp3 its extremely fast depending on download speed and speed of your computer but I get about 1 song per second downloaded and converted to mp3. Here my methods I use. I use a threadpool so thats why its kinda weird hope it helps private static bool DownloadYoutubeVideo(string Url) { string videoFullName = "foo"; try { var youtube = YouTube.Default; var video = youtube.GetVideo(Url); videoFullName = video.FullName.Replace(" - YouTube.mp4", ""); Console.WriteLine("Downloading " + videoFullName); //File.WriteAllBytes(PATH + videoFullName + ".mp4", video.GetBytes());

            IWaveSource videoSource = CSCore.Codecs.CodecFactory.Instance.GetCodec(new Uri(video.Uri));
            Tuple<IWaveSource, String> package = Tuple.Create(videoSource, videoFullName);
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ThreadedConvertToMp3), package);
        }
        catch(Exception e)
        {
            if (File.Exists(PATH + videoFullName + ".mp3"))
                File.Delete(PATH + videoFullName + ".mp3");
            Console.WriteLine(e);
            return false;
        }
        return true;
    }

//* private static void ThreadedConvertToMp3(object callback) { IWaveSource source = ((Tuple<IWaveSource, String>)callback).Item1; String videoTitle = ((Tuple<IWaveSource, String>)callback).Item2; Console.WriteLine("Proccessing " + videoTitle); ConvertToMp3(source, videoTitle); } //*** private static bool ConvertToMp3(IWaveSource source, string videoTitle) { var supportedFormats = MediaFoundationEncoder.GetEncoderMediaTypes(AudioSubTypes.MpegLayer3); if (!supportedFormats.Any()) { Console.WriteLine("The current platform does not support mp3 encoding."); return true; }

        if (supportedFormats.All(
                x => x.SampleRate != source.WaveFormat.SampleRate && x.Channels == source.WaveFormat.Channels))
        {
            int sampleRate =
                supportedFormats.OrderBy(x => Math.Abs(source.WaveFormat.SampleRate - x.SampleRate))
                    .First(x => x.Channels == source.WaveFormat.Channels)
                    .SampleRate;

            Console.WriteLine("Samplerate {0} -> {1}", source.WaveFormat.SampleRate, sampleRate);
            Console.WriteLine("Channels {0} -> {1}", source.WaveFormat.Channels, 2);
            source = source.ChangeSampleRate(sampleRate);
        }
        using (source)
        {
            using (var encoder = MediaFoundationEncoder.CreateMP3Encoder(source.WaveFormat, PATH + videoTitle + ".mp3"))
            {
                byte[] buffer = new byte[source.WaveFormat.BytesPerSecond];
                int read;
                while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    encoder.Write(buffer, 0, read);

                    //Console.CursorLeft = 0;
                    //Console.Write("{0:P}/{1:P}", (double)source.Position / source.Length, 1);
                }
            }
        }
        File.Delete(PATH + videoTitle + ".mp4");
        return false;
    }