flagbug / YoutubeExtractor

A .NET library, that allows to download videos from YouTube and/or extract their audio track (currently only for flash videos).
813 stars 374 forks source link

Extract mp3 from Youtube #155

Open dev45 opened 8 years ago

dev45 commented 8 years ago

Hi Friends! I want to know how to Implement a form for Extract mp3 from youtube video. can you help me? i'm developing c#

morefun0302 commented 8 years ago

hi @dev45 , you want sample codes or a complete project? basically you can follow the example code by flagbug to develop the program

dev45 commented 8 years ago

a complete or a sample code of how to download mp3 directly from a youtube url in a winform c# if it possible

nightfrost commented 8 years ago

I have:

link = textBox1.Text;
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link, false);
DownloadAudio(videoInfos);

Link is a string, the string is equeals to a text in a text box, this text box contains a youtube url.

private void DownloadAudio(IEnumerable<VideoInfo> videoInfos)
        {
            /*
             * We want the first extractable video with the highest audio quality.
             */
            VideoInfo video = videoInfos
                .Where(info => info.CanExtractAudio)
                .OrderByDescending(info => info.AudioBitrate)
                .First();

            /*
             * If the video has a decrypted signature, decipher it
             */
            if (video.RequiresDecryption)
            {
                DownloadUrlResolver.DecryptDownloadUrl(video);
            }

            /*
             * Create the audio downloader.
             * The first argument is the video where the audio should be extracted from.
             * The second argument is the path to save the audio file.
             */

            var audioDownloader = new AudioDownloader(video,
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                RemoveIllegalPathCharacters(video.Title) + video.AudioExtension));

            // Register the progress events. We treat the download progress as 85% of the progress
            // and the extraction progress only as 15% of the progress, because the download will
            // take much longer than the audio extraction.
            audioDownloader.DownloadProgressChanged += (sender, args) => progressBar1.Value = (int)args.ProgressPercentage;

            /*
             * Execute the audio downloader.
             * For GUI applications note, that this method runs synchronously.
             */
            audioDownloader.Execute();
        }

I pretty much just took Flagbugs console application example, tweaked it a bit for easier user control (A GUI). Link could be:

Link = "http://youtube.com/yourvideo"
dev45 commented 8 years ago

hi again i want to know how to make to download mp3 faster and how to cancel the download

nightfrost commented 8 years ago

mp3 should already download fast, depending on the length and quality of the video, I presume. I have heard that different "Qualities" on Youtube have different sound files, the higher the quality, the higher the audio quality, but this I am not sure of. You could always test it?

I will have to test canceling as I have no idea how this could be done. I presume the library uses a stream to get the audio, but I am not sure.

dev45 commented 8 years ago

i have a problem why the program when it's downloading don't let minimize and maximize the program

nightfrost commented 8 years ago

You're running it from the "F5" button on debug mode, I guess? Go to your Documents --> Visual Studio 2013 (Or the version you have) --> Projects --> Whatever you named the project --> Click the folder with the name again --> obj --> Debug --> run your projectname.exe

When you run it from Visual Studio there can be some different visual aspects too, such as the border not being seethrough and what not. This is my way of viewing the actual program, but there's probably a much smarter way to do it.

dev45 commented 8 years ago

Thank's You Bro!! :+1: i have other question how to make to download a mp3 with 128 bitrate or higher

dev45 commented 8 years ago

Hi Man! i'm again i have a problem the program stop working while is downloading the mp3

nightfrost commented 8 years ago

Hey again!

Can't help you with the bitrate, sorry. I have no idea how to do that.

It's maybe because it's extracting the sound? Not sure, though.

moiseyo commented 8 years ago

I have code to download sound from link https://www.youtube.com/watch?v=nZiBINQK_kY

Windows 10 Prof Vs 2013

I found that all entries in object videoInfos ->CanExtractAudio has false value

This related to every link I use on youtuube link above is just a a sample.

Code

private static void DownloadAudio(IEnumerable videoInfos) { if (videoInfos == null) { Console.WriteLine("ERROR: No videoInfos for audio "); result.ok = false; result.Title = "N/A No video Info"; return; } / * We want the first extractable video with the highest audio quality. / var videos = videoInfos .Where(info => info.CanExtractAudio && info.AudioType == AudioType.Mp3) //.Where(info => info.AudioType == AudioType.Mp3) .OrderByDescending(info => info.AudioBitrate) //.First() ;

        if (! videos.Any())
        {
            {
                Console.WriteLine("ERROR: No Valid audio info  ");
                result.ok = false;
                result.Title = "N/A No No Valid audio info ";
                return;
            }
        }

        VideoInfo video = videos.First();
        /*
         * If the video has a decrypted signature, decipher it
         */
        if (video.RequiresDecryption)
        {
            DownloadUrlResolver.DecryptDownloadUrl(video);
        }

        /*
         * Create the audio downloader.
         * The first argument is the video where the audio should be extracted from.
         * The second argument is the path to save the audio file.
         */

        audioName = Path.Combine(arguments.oDir,
            RemoveIllegalPathCharacters(video.Title) + video.AudioExtension).traslitarateName().replaceAll(" ", "");
        if (arguments.AudioProcess)
        {
            audioName = Path.Combine(arguments.oDir,
            RemoveIllegalPathCharacters(video.Title) + video.AudioExtension);

        }
        if (File.Exists(audioName))
        {
            return;
        }
        var audioDownloader = new AudioDownloader(video, audioName);

        // Register the progress events. We treat the download progress as 85% of the progress
        // and the extraction progress only as 15% of the progress, because the download will
        // take much longer than the audio extraction.
        audioDownloader.DownloadProgressChanged += (sender, args) => Console.WriteLine("{0:00} %   ", args.ProgressPercentage * 1);
        //audioDownloader.AudioExtractionProgressChanged += (sender, args) => Console.WriteLine("{0:00} %  ", args.ProgressPercentage * 0.15);
        /*
         * Execute the audio downloader.
         * For GUI applications note, that this method runs synchronously.
         */
        audioDownloader.Execute();
    }

Any Ideas

Best regrad

LinPolly commented 8 years ago

You can try this with the file extension ".mp3", although the audiobitrate = 128 in the file, but the details of the audiobitrate = null, but it is available. You can use this download audio manner 2160p, 1440p, 1080p, 480p video using ffmpeg will be synthesized audio and video files.

I currently use the following way to get all resolution video, and all have audio.

VideoInfo.cs

new VideoInfo(140, VideoType.Mp4, 0, false, AudioType.Aac, 128, AdaptiveType.Audio), new VideoInfo(171, VideoType.WebM, 0, false, AudioType.Vorbis, 128, AdaptiveType.Audio),

Code

This example is downloaded Mp4 audio. In the program set the file extension ".mp3"

VideoInfo video = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 0); var audioDownloader = new VideoDownloader(video, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), RemoveIllegalPathCharacters(video2.Title) + ".mp3")); audioDownloader.Execute();

Code

When you download the 1080p it is no audio Mp4, you can try the following examples.

string arg = "-i " + mp4file + " -i " + mp3file + " -map 0:0 -map 1:0 -codec copy " + outputfile; Process proc = new Process(); proc.StartInfo.WorkingDirectory = Application.StartupPath; proc.StartInfo.FileName = "ffmpeg.exe"; proc.StartInfo.Arguments = arg;

proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.Start();

Note: When your Mp4 and Mp3 file name contains spaces must use the following examples. Otherwise it will fail synthesis files.

mp4file = string.Format(@"""{0}""", savepath + RemoveIllegalPathCharacters(video.Title) + video.VideoExtension);

dev45 commented 8 years ago

Ho Again Friends! I have this problem I Couldn't download any url could you help me to solve this problem? i'm develop in c#

moiseyo commented 8 years ago

a sample Processed Line : http://www.youtube.com/watch?v=NNEK1hYg-bk

Sequence contains no elements at System.Linq.Enumerable.First[TSource](IEnumerable1 source) at YoutubeApplication.Program.DownloadAudio(IEnumerable1 videoInfos) at YoutubeApplication.Program.processFilelist(String fname)

Thank you

Moisey O

moiseyoys@gmail.com [image: linkedinbutton] https://www.linkedin.com/in/oysgeltm The view changes only for the leading dog.

On Tue, Aug 9, 2016 at 2:12 AM, dev45 notifications@github.com wrote:

Ho Again Friends! I have this problem I Couldn't download any url could you help me to solve this problem? i'm develop in c#

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/flagbug/YoutubeExtractor/issues/155#issuecomment-238462870, or mute the thread https://github.com/notifications/unsubscribe-auth/AFEYqmjgu9Pl-mXbTgVk12Rs6BdqJnpFks5qeBpYgaJpZM4GqwBm .

dev45 commented 8 years ago

i have another question bro. the problem persist the program can't download any video and mp3 too.

i want to know if it's problem of youtube extractor.dll and how can be solved that problem.

Thank's Bro.

moiseyo commented 8 years ago

I'm not an API developer

Moisey O

moiseyoys@gmail.com [image: linkedinbutton] https://www.linkedin.com/in/oysgeltm The view changes only for the leading dog.

On Thu, Aug 11, 2016 at 5:38 PM, dev45 notifications@github.com wrote:

i have another question bro. How i can solve that problem?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/flagbug/YoutubeExtractor/issues/155#issuecomment-239300951, or mute the thread https://github.com/notifications/unsubscribe-auth/AFEYqhrp_FuiFbcuXbitBHFT-vvrpgz4ks5qe5Y_gaJpZM4GqwBm .

JdidDahmane commented 7 years ago

I have problème the Instance AudioDownloader don't have a using or a assembly hi is not exist library for this " i have youtubeExtractor " and I have using youtubeextractor; pleaaase heeelp me

moiseyo commented 7 years ago

resentfully I have a lot of problem with .NET in windows 10 event I use vs 2013 and .net 4.51

I had to switch to two other languages

For mp3 from YouTube download https://rg3.github.io/youtube-dl/

and clean empty directories to java http://stackoverflow.com/questions/26017545/delete-all-empty-folders-in-java

You you find solution in .NET let me know pls

Thank you for you attention

Moisey O

moiseyoys@gmail.com [image: linkedinbutton] https://www.linkedin.com/in/oysgeltm The view changes only for the leading dog.

On Mon, Apr 17, 2017 at 10:31 AM, JdidDahmane notifications@github.com wrote:

I have problème the Instance AudioDownloader don't have a using or a assembly hi is not exist library for this " i have youtubeExtractor " and I have using youtubeextractor; pleaaase heeelp me

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/flagbug/YoutubeExtractor/issues/155#issuecomment-294496446, or mute the thread https://github.com/notifications/unsubscribe-auth/AFEYquhobSR0FArddDlINQ15n7_ldc2oks5rw3fOgaJpZM4GqwBm .