Can You help me with this code?

Bambam

New member
Joined
Dec 15, 2022
Messages
3
Programming Experience
1-3
Hello everyone.



i wanted to make a Youtube mp3 downloader. Got a code that i found from stackoverflow. I will add the other feetures anyway. but, i couldn't get it running. i want it to be console application btw.


Youtube to mp3:
using VideoLibrary;

using MediaToolkit;





private void SaveMP3(string SaveToFolder, string VideoURL, string MP3Name)

{

    string source = @"C:\";

    var youtube = YouTube.Default;

    var vid = youtube.GetVideo(https://www.youtube.com/watch?v=bjiSnexdZ88);

    string videopath = Path.Combine(source, vid.FullName);

    File.WriteAllBytes(videopath, vid.GetBytes());



    var inputFile = new MediaFile { Filename = Path.Combine(source, vid.FullName) };

    var outputFile = new MediaFile { Filename = Path.Combine(source, $"{MP3Name}.mp3") };



    using (var engine = new Engine())

    {

        engine.GetMetadata(inputFile);





        engine.Convert(inputFile, outputFile);

    }





    File.Delete(Path.Combine(source, vid.FullName));



}
 
On the technical side: What errors are you running into? What have you tried to fix those errors?

On the legal side: are you sure you are not violating the YouTube content owners' copyrights by downloading?
 
For the legality side. i won't share this app with anyone. jjust i wanna learn how to deal with it. done something simillar with Python though. There bunch of sites do this already.

Since too mutch error codes existed and my Vs is in Turkish. i got translation done within Google translation, hope you don'T mind it.
Code:
"
Severity Code Description Project File Line Hide Status
Error CS8370 'high-level statements' feature is not available in C# 7.3. Please use language version 9.0 or higher. ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 5 Enabled
Error CS0106 'private' modifier is not valid for this item ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 5 Enabled
Error CS1525 Invalid expression term 'string' ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 10 Enabled
Error CS1003 Syntax error, expected ',' ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 10 Enabled
Error CS1026) expected ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 10 Enabled
Error CS0103 Name 'videopath' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 10 Enabled
Error CS0103 Name 'Path' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 10 Enabled
Error CS0841 Unable to use local variable 'vid' before expression ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 10 Enabled
Error CS0103 Name 'File' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 11 Enabled
Error CS0103 Name 'videopath' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 11 Enabled
Error CS0246 Type 'MediaFile' or namespace name not found (are you missing a using directive or assembly reference?) ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 13 Enabled
Error CS0103 Name 'Path' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 13 Enabled
Error CS0246 Type 'MediaFile' or namespace name not found (are you missing a using directive or assembly reference?) ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 14 Enabled
Error CS0103 Name 'Path' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 14 Enabled
Error CS0103 Name 'File' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 25 Enabled
Error CS0103 Name 'Path' does not exist in current context ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 25 Enabled
Warning CS8321 Local variable 'SaveMP3' defined but never used ConsoleApp2 C:\Users\Someone\source\repos\ConsoleApp2\Program.cs 5 Enabled"
 
Last edited by a moderator:
Well it looks like you just dropped the code straight into a .CS file. You need to follow the basic C# programming rules and put methods inside classes.

It also looks like you didn't include the Nuget packages for those libraries you are using. If you look at the SO question and answers where you pulled that code from, you'll see that they tell you to install those Nuget packages.
 
If you were using .NET Core as your build target, you might have had half a chance of just dropping in the code into a .CS file, but you would still need to import the Nuget packages. .NET Core lets you use top-level statements just like the error messages indicates.
 
If you were using .NET Core as your build target, you might have had half a chance of just dropping in the code into a .CS file, but you would still need to import the Nuget packages. .NET Core lets you use top-level statements just like the error messages indicates.


Nuget packages are installed already. did the code work for You? can You check it out please? but, one thing i didn't understand as far as i see the guys posted it on sov as working process.
 
Take a look at the code as rendered by the forum software. See how the same words are colored differently all over the place?

That's a clue to one of the mistakes... The syntax highligher colors differently according to whether it thinks you're "inside a string" or "inside code"
 
@Bambam : How much C# programming experience do you have? If not a lot, why are you tackling this problem with out first getting some of the fundamentals down first?

Anyway here's code that compiles:
C#:
using VideoLibrary;
using MediaToolkit;
using MediaToolkit.Model;

class Program
{
    private void SaveMP3(string SaveToFolder, string VideoURL, string MP3Name)
    {
        string source = @"C:\";
        var youtube = YouTube.Default;
        var vid = youtube.GetVideo("https://www.youtube.com/watch?v=bjiSnexdZ88");
        string videopath = Path.Combine(source, vid.FullName);
        File.WriteAllBytes(videopath, vid.GetBytes());

        var inputFile = new MediaFile { Filename = Path.Combine(source, vid.FullName) };
        var outputFile = new MediaFile { Filename = Path.Combine(source, $"{MP3Name}.mp3") };

        using (var engine = new Engine())
        {
            engine.GetMetadata(inputFile);
            engine.Convert(inputFile, outputFile);
        }

        File.Delete(Path.Combine(source, vid.FullName));
    }
}

The changes that I made were:
  1. Lines 5-6, 26: Put the method into a class. Even if you had top-level statements in C# 9, the private access modifier still would not have made sense. It looks like the intent of the posters in that SO post was for code to live in a class. Also given the age of the post, C# 9 still didn't exist so they wouldn't have had top level statements available to them back in 2016 or 2018.
  2. Line 11: Put string in quotes.
  3. Line 3: Import another namespace so that the compiler can find where MediaFile class exists. Alternatively, you could use fully qualified names like in another SO post that has similar code.
In general, you shouldn't just copy and paste code without first understanding what does and what context the code is supposed to be used it.
 
Back
Top Bottom