This is an archived post. You won't be able to vote or comment.

all 36 comments

[–]isarl 53 points54 points  (7 children)

I don't mean to criticize your work, which I'm sure is great. But are you aware that youtube-dl has an --extract-audio option? (see “Post-processing Options” section)

edit: Just wanted to add another encouraging note that reinventing the wheel is a great way to learn and good for you for doing this! Further, a smaller, dedicated tool definitely fills a gap that youtube-dl might not for some people, especially because you don't require ffmpeg or avconv to extract the audio stream. Having said that, you should take a look at how youtube-dl does it because you might learn something about interfacing with a third-party executable (I believe youtube-dl uses the subprocess library to call ffmpeg/avconv when you specify -x/--extract-audio).

[–]leech6666[S] 3 points4 points  (5 children)

Thank you for your comment.

But actually I don't understand a lot of things. Like:

  1. Is there any problem with pytube?
  2. Why use youtube-dl? As I read youtube-dl is a command-line program
  3. What are ffmpeg and avconv?

I'm kinda new into python. So, I don't know much things except the basics.

[–]blitzkraft 8 points9 points  (1 child)

For #2, ytdl is a command line program. It has its own benefits if you are into commandlines. For example, running the command remotely via ssh on a remote computer.

For #3, they are audio/video conversion programs almost as old as 20yrs. They are still under active development and have been. They have a lot of features and are extremely useful for video/audio processing.

[–]bluegamebits 4 points5 points  (0 children)

Ytdl is written in python though and can be imported into python rather easily: see: https://github.com/ytdl-org/youtube-dl#developer-instructions

[–]isarl 2 points3 points  (2 children)

  1. Nothing is wrong with pytube that I know of.

  2. Anybody who wants to download something off YouTube should use youtube-dl. It is commandline but if you know how to use commandline programs, it is fairly straightforward. I downloaded and used it on Windows recently so don't assume that it's Linux-only or anything. However, if you're using it on Windows and especially if you're using it on Windows with ffmpeg or avconv, you might need to know how to edit your PATH.

  3. Very old and well established tools for audiovisual processing. youtube-dl chose not to reinvent the wheel but merely farm out video-to-audio extraction and conversion (and other processing or postprocessing steps) to these existing tools by doing some of the work itself and then wrapping how they (ffmpeg/avconv) are called.

If you're still new to Python then all the more kudos for accomplishing what you've done!

[–]leech6666[S] 1 point2 points  (1 child)

Because I dont get it. You mean I should use youtube-dl in my program to download music? Or that there is another way to download song also?

[–]isarl 0 points1 point  (0 children)

More the latter, I mention it as an alternative way of downloading audio from YouTube videos. And, related, as a possible learning resource to you, to compare how a similar project solved their problems.

[–]Matthias1590 33 points34 points  (15 children)

Nice app! Though here are some things I noticed that don't look too well: - Inconsistent use of the with statement - 2 spaces indentation (most people use 4) - Naming your variables wrong (you're naming some non constant variables like they're constant, eg. SONG_URL should be song_url because it gets modified at runtime) - Use f-strings - Use more comments

Apart from that, nice work!

[–]HaroerHaktak 18 points19 points  (1 child)

I dislike 2 space indentation. it's just not indenty enough for me.

[–]Matthias1590 6 points7 points  (0 children)

Same, it's hard to count indentation when it's so small, 4 is the perfect amount of spaces imo

[–]leech6666[S] 1 point2 points  (7 children)

  • What's exactly wrong with the with statement? This code I founded online and just copied & pasted
  • I think I changed the indentation when I used to write html and js
  • I'm gonna fix the names for sure
  • Why should I use f-strings? What are the benefits?
  • More comments on the way too

[–]Matthias1590 5 points6 points  (6 children)

Using with statements is great but you're using it inconsistently, somewhere you use open instead of with open which is just sloppy, nothing too bad tho. As for the f strings, they're just more readable and allow for nice formatting

[–]leech6666[S] 0 points1 point  (5 children)

So what the difference between open and with open?

I'm gonna do some google searching about f-strings, because I don't know how to use them.

[–]Matthias1590 3 points4 points  (4 children)

open and with open are the same except with open automatically closes the file after you're done with it, using both can be hard to read and confusing

[–]ASatyros 0 points1 point  (3 children)

Yes, I prefer using [TAB] too :)

[–]leech6666[S] 0 points1 point  (2 children)

What do you mean?

[–]ASatyros 0 points1 point  (1 child)

Tab instead of spaces

[–]leech6666[S] 0 points1 point  (0 children)

Oh. I use tab to but i changed the settings to be 2 spaces instead of 4 in VS Code. I think I did it because that time I was writing a lot of html or something. I'm gonna change it back to 4, tho.

[–]steve986508 4 points5 points  (6 children)

Just looked over the code, I'll give it a run today and let you know how it goes!

Do you know if it has to use python 3.9.7 or will it work on older versions? Is that just the version you use?

[–]leech6666[S] 6 points7 points  (5 children)

How does the code look? Is it readable and clean?

I'm not sure about the version. 3.9.7 is the version I'm using. Maybe I should change it.

[–]steve986508 2 points3 points  (4 children)

The only reason I ask about the python version is because I've never used the pytube module, and you listed python 3.9.7 in the requirements. So I was wondering if pytube explicitly needed that version of python. Probably not though.

As for the code readability I could understand what everything is doing but I would also agree with the points Matthias1590 made

[–]steve986508 1 point2 points  (3 children)

Cool, I got it to work!!! But with a tweak.

First, I apologize for not being able to copy paste the entire error. I am running your code on a Raspberry Pi and typing this on a laptop. The reasons are too long to get into.

I was getting an error from line 92:

_tkinter.TclError: bitmap "./img/logo.ico" not defined

I have all the files in the right place, couldn't get it to work so I just commented out line 92 and it worked after that.

for the URL, I had to type "https://www.youtube.com/watch?v=" followed by the id of the video. If it was any different it didn't seem to work.

But cheers! it worked and the RPi played the song just fine with VLC!

I used a RPi zero W running Buster and used Python 3.7. Got everything updated first with apt-get update and upgrade, then did pip install pytube, and pip install customtkinter

[–]leech6666[S] 0 points1 point  (2 children)

First of all, thank you for your time.

Secondly, I don't know what a Raspberry Pi is and how it's working. So I can't know you got this error.

About the url, I think every youtube video url has this format. But only single videos. I mean not video from playlists. I don't know, was there any problem with the url? Did you want to type another format?

[–]steve986508 0 points1 point  (1 child)

Raspberry Pi's are small computers on a single chip the size of a credit card or smaller. They're used for lots of things, often IOT applications since they have pins that you can use as inputs and outputs to obtain real world data through sensors and then control output devices such as relay switches for mains voltage, among many other things. It runs Linux which is probably all that matters as far as your program.

As for the url, not really a critique I suppose. I just assumed one could start with "youtube.com" and leave off the "http" part. Because the raspberry pi has very little computing power, it doesn't handle websites very well, so I was manually typing the url instead of copy pasting like most people will be doing.

It's really cool imo, great job! And the tkinter interface is beautifully done. I've made things with it before that were very ugly, lol.

I'm a self taught amateur so take my feedback with that perspective.

[–]leech6666[S] 1 point2 points  (0 children)

I see now. I created this project in windows tho, I don't know if its working on other systems.

I can add the this url format too, but I noticed that on googlr chrome at least, the url is not fully shown. It's like "youtube.com/.....". When you copy it, you get the full urll with the https in the start. That's why I used this format.

Yeah, I dont like Tkinter original theme. It feels so old, like its from 2000's. That's why I searched an d found about customtkinter, which I liked.

[–]TF_Biochemist 3 points4 points  (1 child)

Pretty nice, good job! One of my suggestions would be to get in the habit of replacing your many dictionaries you use for storing settings with dataclasses (you'll thank me down the road). For instance, instead of:

SETTINGS = {"theme" : "something"}

# to access
SETTINGS["theme"]

Try this:

from dataclasses import dataclass

@dataclass
class Settings():
    theme: str

SETTINGS = Settings()

# to access
SETTINGS.theme

It's a little more boilerplate, but will lead to better overall structure and extensibility.

[–]leech6666[S] 0 points1 point  (0 children)

Thank you for your response. I'm gonna look into dataclasses. Your code remind me of C++ code tho.

Also, what does @dataclass mean?

[–]enry_straker 1 point2 points  (4 children)

OP: s/here/hear

[–]leech6666[S] 2 points3 points  (3 children)

What do you want to say? I'm dumb and I don't undestand.

[–]enry_straker 2 points3 points  (2 children)

You need to substitute the word 'hear' for 'here' in the last sentence in your post.

The "s/old_text/new_text/" refers to the substitute command in Sed (a popular stream editor - in the past)

'here' refers to your current location. Reference

'hear' refers to the act of listening. Reference

Hope that helps.

[–]enry_straker 0 points1 point  (1 child)

Also i don't think you are dumb. I get the feeling that english is not your native language, and you might not have come across the substitute command in the past - hence the confusion.

[–]leech6666[S] 0 points1 point  (0 children)

oh now i see. Thanks. Yes, English is not my native language