Youtube to MP3

Post your working scripts, libraries and tools for AHK v1.1 and older
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Youtube to MP3

08 Nov 2017, 12:36

Hello,
I made this simple Youtube to MP3 downloader and decided to share it :)
Note: Works only with IE 11 or newer

Code: Select all

#NoEnv
#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%

pwb := ComObjCreate("InternetExplorer.Application")
pwb.Visible := false


Gui, +AlwaysOnTop
Gui, Show, x200 y750 w465 h153, Youtube downloader by kovacevica00
Gui, Color, White
Gui, Font, s15, Courier New
Gui, Add, Text, x21 y16 w422 h27 +0x200, Please enter youtube song URL below:
Gui, Add, StatusBar
Gui, Font
Gui, Add, Edit, x11 y73 w442 h21 vURL,
Gui, Add, Button, Default x178 y107 w109 h22, OK
Gui, Add, Progress, vDProgress x11 y96 w442 h9 -Smooth
GuiControl, Focus, URL
Return

GuiClose:
	pwb.quit()
    ExitApp
return


F9:: ; To automatically select and copy URL from browser
clipboard =
Send, ^l
Sleep, 300
Send, ^c
If Clipboard = 
{
	Send, ^l
	Sleep, 500
	Send, ^c
}
ControlClick, OK, Youtube downloader by kovacevica00
Return

OnClipboardChange:
if RegExMatch(Clipboard, "^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/watch\?v=([^&]+)")
{
	GuiControl,, URL, %Clipboard%
	GuiControl, Focus, OK
}
Return

ButtonOK:
Gui, Submit, NoHide
if RegExMatch(URL, "^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/watch\?v=([^&]+)")
{
	If IsObject(pwb)
	{
		GuiControl, Disable, URL
		GuiControl, Disable, OK
		SB_SetText("preparing download..")
		GuiControl, , DProgress, 10
		pwb.Navigate("https://ytmp3.cc/")
		while pwb.busy or pwb.ReadyState !=4
			sleep, 100
		pwb.document.getElementById("input").value := URL
		pwb.document.getElementById("submit").click()
		while ((Inprogress := pwb.document.getElementById("file").href) = "https://ytmp3.cc/")
		{
			if ((CurrentStatus := pwb.document.getElementById("progress").innertext) = "checking video  ")
			{
				SB_SetText(CurrentStatus)
				GuiControl, , DProgress, 20
			}
			else if ((CurrentStatus := pwb.document.getElementById("progress").innertext) = "loading video  ")
			{
				SB_SetText(CurrentStatus)
				GuiControl, , DProgress, 30
			}
			else if ((CurrentStatus := pwb.document.getElementById("progress").innertext) = "converting video  ")
			{
				SB_SetText(CurrentStatus)
				GuiControl, , DProgress, 40
			}
			sleep, 100
		}
		GuiControl, , DProgress, 50
		DirectUrl := pwb.document.getElementById("file").href
		SongName := pwb.document.getElementById("title").innertext
		SongName := strReplace(SongName,"|") ; Removes "|" as it is unsupported character
		SongName := strReplace(SongName,":") ; Removes ":" as it is unsupported character
		GuiControl, , DProgress, 70
		SB_SetText("downloading, please wait...")
		IfNotExist, %A_MyDocuments%\Music
			FileCreateDir, %A_MyDocuments%\Music
		URLDownloadToFile, %DirectUrl%,%A_MyDocuments%\Music\%SongName%.mp3
		SB_SetText("download successful!")
		GuiControl, , DProgress, 100
		GuiControl,, URL
		GuiControl, Enable, URL
		GuiControl, Enable, OK
		GuiControl, Focus, URL
	}
	else
	{
		MsgBox,0, Error. Internet explorer failed to initialize. Relaunching.
		Reload
	}
	
}
else
{
	SB_SetText("Invalid URL")
	GuiControl,,URL
	GuiControl, Focus, URL
}
Sleep, 1500
GuiControl, , DProgress, 0
SB_SetText("")
return
Last edited by HIAC on 14 Nov 2017, 19:35, edited 55 times in total.
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Youtube to MP3

08 Nov 2017, 14:45

Very nice ! Thanks for sharing, i use http://convert2mp3.net/en/ but that takes a bit of work ( but it enters info in the tags).
Newbx1000

Re: Youtube to MP3

08 Nov 2017, 15:11

How can I modify this for use with Chrome?
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

08 Nov 2017, 17:45

noname wrote:Very nice ! Thanks for sharing, i use http://convert2mp3.net/en/ but that takes a bit of work ( but it enters info in the tags).
Glad that you like it! Yeah, i started off with that site first too, but this one turned out to be easier a lot.. :thumbup:
Newbx1000 wrote:How can I modify this for use with Chrome?
Unfortunately, only IE support automation like this. However, it runs in background so it shouldn't matter :|
User avatar
Wickster
Posts: 21
Joined: 19 Apr 2014, 23:02

Re: Youtube to MP3

08 Nov 2017, 19:41

I cant get this to work :( I have IE 11, windows 10. Not sure why it wont....its pretty straight forward and simple to use LOL. I push F9 and nothing happens at all. I thought maybe I was doing something wrong :/

URL copied to clipboard, push F9 it saves to my docs correct ? LOL
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

08 Nov 2017, 19:52

Wickster wrote:I cant get this to work :( I have IE 11, windows 10. Not sure why it wont....its pretty straight forward and simple to use LOL. I push F9 and nothing happens at all. I thought maybe I was doing something wrong :/

URL copied to clipboard, push F9 it saves to my docs correct ? LOL
Uhmmm.. :think: Maybe try changing the hotkey to ctrl+m for example? I had a laptop that didn't register function keys on AHK scripts..

Code: Select all

^m::
And no, you shouldn't copy the url, just select, and after pressing the hotkey, script will copy it and do rest of the stuff
User avatar
Wickster
Posts: 21
Joined: 19 Apr 2014, 23:02

Re: Youtube to MP3

08 Nov 2017, 21:36

keep getting please select song URL now. and Im on it. Ive tried IE, Chrome, FF.
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

08 Nov 2017, 22:30

Updated again, check it up
Just press F8 while listening to youtube song, no selection is needed anymore.
User avatar
Wickster
Posts: 21
Joined: 19 Apr 2014, 23:02

Re: Youtube to MP3

08 Nov 2017, 23:33

i can see it selecting the url now, and then it says "incorrect url" while im listening. jeez what am i doing wrong
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Youtube to MP3

09 Nov 2017, 03:29

Not all videos/songs can be downloaded or have the correct format , the ahk code works flawless ( except the download folder coded is non existent on my system....) on win10 pro .
You can try downloading a link that works https://www.youtube.com/watch?v=fMR4fp4A2Ew or paste your link into http://convert2mp3.net/en/ website.If there is a problem like a restriction or format ( not available as mp3) it will report it and a possible solution like changing to mp4 download.

Best of luck :)
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

09 Nov 2017, 10:03

noname wrote:Not all videos/songs can be downloaded or have the correct format , the ahk code works flawless ( except the download folder coded is non existent on my system....) on win10 pro .
You can try downloading a link that works https://www.youtube.com/watch?v=fMR4fp4A2Ew or paste your link into http://convert2mp3.net/en/ website.If there is a problem like a restriction or format ( not available as mp3) it will report it and a possible solution like changing to mp4 download.

Best of luck :)
That's true, however I didn't fail to download any song yet..
Wickster wrote:i can see it selecting the url now, and then it says "incorrect url" while im listening. jeez what am i doing wrong
I've improved the script now, as well as the regex code which determines whether youtube link is valid, try again please
Last edited by HIAC on 09 Nov 2017, 10:07, edited 1 time in total.
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Youtube to MP3

09 Nov 2017, 10:06

Interesting, but I prefer to use http://www.convyoutube.com/ with the video url: http://www.convyoutube.com/watch?v=RandomStr

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
Wickster
Posts: 21
Joined: 19 Apr 2014, 23:02

Re: Youtube to MP3

09 Nov 2017, 11:30

I've improved the script now, as well as the regex code which determines whether youtube link is valid, try again please

OK so now atleast were getting somewhere, I had to run the script as admin, then when i use it it works but its only saves 4KB mp3. it has the song names and everything but theyre unplayable. obviously wrong size. sorry for causing so many issues for you. XD just thought this would be handy to have.
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

09 Nov 2017, 11:48

Wickster wrote:
I've improved the script now, as well as the regex code which determines whether youtube link is valid, try again please

OK so now atleast were getting somewhere, I had to run the script as admin, then when i use it it works but its only saves 4KB mp3. it has the song names and everything but theyre unplayable. obviously wrong size. sorry for causing so many issues for you. XD just thought this would be handy to have.
Interesting, did you try with few different songs? No problem, you're actually helping me to improve myself. :D
User avatar
Wickster
Posts: 21
Joined: 19 Apr 2014, 23:02

Re: Youtube to MP3

09 Nov 2017, 11:57

HIAC wrote: Interesting, did you try with few different songs? No problem, you're actually helping me to improve myself. :D

yup, 5 different songs and 2 browsers chrome and ff
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

09 Nov 2017, 15:35

Completely redesigned and improved the script, hopefully it's gonna work fine this time.
User avatar
Wickster
Posts: 21
Joined: 19 Apr 2014, 23:02

Re: Youtube to MP3

09 Nov 2017, 15:51

HIAC wrote:Completely redesigned and improved the script, hopefully it's gonna work fine this time.
works faster i can tell you that. but still only downloads songs at 4kb. fml >_>
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

09 Nov 2017, 18:47

OK SHOULD BE FIXED NOW! :lol:
User avatar
Wickster
Posts: 21
Joined: 19 Apr 2014, 23:02

Re: Youtube to MP3

09 Nov 2017, 19:08

HIAC wrote:OK SHOULD BE FIXED NOW! :lol:

:superhappy: :superhappy: well whatever you did, it works just fine now. :) just have to change a few things. thanks for all the hard work! :superhappy: :superhappy:
HIAC
Posts: 111
Joined: 11 Oct 2017, 17:59
Location: Republic of Serbia

Re: Youtube to MP3

09 Nov 2017, 23:42

Wickster wrote:
HIAC wrote:OK SHOULD BE FIXED NOW! :lol:

:superhappy: :superhappy: well whatever you did, it works just fine now. :) just have to change a few things. thanks for all the hard work! :superhappy: :superhappy:
Glad to hear that. :superhappy:
I improved script, and will probably continue doing it, so please keep yourself updated regularly.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 126 guests