Efty.Edge

Problem to start a Python script

29 posts in this topic

Hi,

 

Here is my idea of a youtube video downloader workflow.

 

I try to catch the url of the browser and send the url to the youtube-dl script created by Ricardo Garcia Gonzalez (http://rg3.github.io/youtube-dl/index.html).

 

The syntax to download a youtube video in HQ is simple, just: youtube-dl <url>

 

Everthing works fine except one thing. First I dropped the youtube-dl script into the workflow folder. Then i opened a Youtube Video and started my workflow. In the notification center the catched url is shown. But the youtube-dl script/programm didn't start.

 

Please give me some advice. I'm really, really new to scripting ...

 

Thanx in advance

Efty

 

 

#!/bin/bash

# cases that handles fetching the URL them self
case "$1" in
    
    *)
        # all other options need the URL, so lets fetch it
        THEURL=$(sh getURL.sh)
        # if no url then just exit
        if [ -z "$1" ]
            then
            echo "Unable to find a URL"
            exit
        else
           echo $THEURL
           youtube-dl  + $THEURL
        fi       
esac

 

 

 

Share this post


Link to post

Hi,

 

Here is my idea of a youtube video downloader workflow.

 

I try to catch the url of the browser and send the url to the youtube-dl script created by Ricardo Garcia Gonzalez (http://rg3.github.io/youtube-dl/index.html).

 

The syntax to download a youtube video in HQ is simple, just: youtube-dl <url>

 

Everthing works fine except one thing. First I dropped the youtube-dl script into the workflow folder. Then i opened a Youtube Video and started my workflow. In the notification center the catched url is shown. But the youtube-dl script/programm didn't start.

 

Please give me some advice. I'm really, really new to scripting ...

 

Thanx in advance

Efty

 

 

#!/bin/bash

# cases that handles fetching the URL them self
case "$1" in
    
    *)
        # all other options need the URL, so lets fetch it
        THEURL=$(sh getURL.sh)
        # if no url then just exit
        if [ -z "$1" ]
            then
            echo "Unable to find a URL"
            exit
        else
           echo $THEURL
           youtube-dl  + $THEURL
        fi       
esac

 

Try this..

In the Run Script module, leave the language set to /bin/bash.

 

var="{query}"
if [ -z $var ]; then
  echo "No url provided"
else
  youtube-dl "$var"
fi

 

This will run the script if a value is passed. You could always just set your keyword to require an argument and you won't have to check if its empty. This worked for me.

Share this post


Link to post

Hi David!

 

Sorry for my late reply and thank you for your help.

 

But i'm not able to get your script to work with mine. So developed following solution in applescript.

 

#/usr/bin/osascript

try
	tell application "Safari" to tell document 1
		set theLink to (get URL)
	end tell
end try

try
    tell application "Terminal"
        do shell script "python youtube-dl -o ~/Downloads/movie.mp4 " & theLink
    end tell
end try

tell application "Terminal" to quit

return "Download Finished... " 

 

 

This one works really fine, but i have a few questions you can maybe help me with.

 

I would like to use an argument after the keyword to replace the filename 'movie.mp4' with a user defined one. How can i do it?

 

Is it possible the execute the 'do shell script...' without a visible Terminal window?

 

And at last. Is it possible to display a notification in the Notification Center like 'Download started...' at the start of the script which stays visible until the script is finished!? Actual it disappears after 5 sec. as normal.

 

Thank you again.

Efty

Edited by Efty Edge

Share this post


Link to post

Is it possible the execute the 'do shell script...' without a visible Terminal window?

 

David's infinitely more qualified than I am to answer your questions, so I'll take the easy one :-) The do shell script command normally does run without creating a Terminal window, the only reason it's doing it here is because you included it in a tell application "Terminal" block. Try this:

 

#/usr/bin/osascript

try
    tell application "Safari" to tell document 1
	set theLink to (get URL)
    end tell
end try

try
    do shell script "python youtube-dl -o ~/Downloads/movie.mp4 " & theLink
end try

return "Download Finished... "

For the record, if you are using AppleScript to tell Terminal to run a shell script, you can actually do it with do script rather than the full do shell script command :-)

 

EDIT: I'm also not sure what the duplicate try blocks are meant to accomplish. Couldn't you achieve the same thing with just one?

#/usr/bin/osascript

try
    tell application "Safari" to tell document 1
	set theLink to (get URL)
    end tell

    do shell script "python youtube-dl -o ~/Downloads/movie.mp4 " & theLink
end try

return "Download Finished... "
Edited by Subject22

Share this post


Link to post

Hi David!

 

Sorry for my late reply and thank you for your help.

 

But i'm not able to get your script to work with mine. So developed following solution in applescript.

 

#/usr/bin/osascript

try
	tell application "Safari" to tell document 1
		set theLink to (get URL)
	end tell
end try

try
    tell application "Terminal"
        do shell script "python youtube-dl -o ~/Downloads/movie.mp4 " & theLink
    end tell
end try

tell application "Terminal" to quit

return "Download Finished... " 

 

 

This one works really fine, but i have a few questions you can maybe help me with.

 

I would like to use an argument after the keyword to replace the filename 'movie.mp4' with a user defined one. How can i do it?

 

Is it possible the execute the 'do shell script...' without a visible Terminal window?

 

And at last. Is it possible to display a notification in the Notification Center like 'Download started...' at the start of the script which stays visible until the script is finished!? Actual it disappears after 5 sec. as normal.

 

Thank you again.

Efty

 

Try this: download

 

I tried this with several videos and it worked perfectly for me. Give it a youtube link, it downloads the video, names the file the title of the video and saves it to your desktop

Share this post


Link to post

Hi Subject22 and David.

 

Thanx for your help and your advice. I'm a scripting newbie, so i'm very glad and thankful for any help!

 

David - your script works like charm, but i didn't really understand how it works. I promise, i've tried it an hour or two!!! At last i finished mine in Applescript ...

 

 

Efty

Share this post


Link to post

Does this still work? I can't get it to work with the latest version of Alfred. It says download complete suspiciously fast and the file doesn't appear.

Share this post


Link to post

Does this still work? I can't get it to work with the latest version of Alfred. It says download complete suspiciously fast and the file doesn't appear.

 

Chances are you just need to update the youtube-dl script within the workflow. Looky here.

Share this post


Link to post

I'm having the same issue that @justcharlie was having with http://cloud.dferg.us/q4Gt

 

I've updated to the latest youtube-dl and also Python 2.7.13.  Just pops up the OS X notification saying it's done but no file.  youtube-dl works fine when run manually.

 

On 10/11/2014 at 0:18 AM, justcharlie said:

Does this still work? I can't get it to work with the latest version of Alfred. It says download complete suspiciously fast and the file doesn't appear.

 

Share this post


Link to post
1 hour ago, petebocken said:

I've updated to the latest youtube-dl

 

The one installed on your system or the one included in the workflow?

Share this post


Link to post
12 minutes ago, deanishe said:

 

The one installed on your system or the one included in the workflow?

 

The one installed on my system.  I just now realized that the workflow has it's own version installed with it.  I tried replacing the youtube-dl file in the workflow and that doesn't make a difference.  I also tried editing the workflow script to point to mine at /usr/local/bin/youtube-dl and still no go.  Although not sure I know the correct way to have it look outside of the workflow at the file system for that file besides just putting the full path to it.

Share this post


Link to post

Posted (edited)

1 hour ago, petebocken said:

just putting the full path to it

 

Bingo! Unfortunately, Alfred (and all other Mac apps) don't use your shell environment, which includes PATH. They don't look in /usr/local/bin.

 

If you fix the path to point to your Homebrew verison of youtube-dl, the workflow should just keep on working.

 

If it doesn't, I can post an AppleScript that does.

Edited by deanishe

Share this post


Link to post
3 hours ago, deanishe said:

 

Bingo! Unfortunately, Alfred (and all other Mac apps) don't use your shell environment, which includes PATH. They don't look in /usr/local/bin.

 

If you fix the path to point to your Homebrew verison of youtube-dl, the workflow should just keep on working.

 

If it doesn't, I can post an AppleScript that does.

 

 

Ya, not sure what to put in the path to find my local install of youtube-dl:

./youtube-dl -q -o "$uhome/Desktop/%(title)s.%(ext)s" "$url"; echo "Completed";

Share this post


Link to post

If you installed it with Homebrew or by following the official instructions, it's /usr/local/bin/youtube-dl, thus:

/usr/local/bin/youtube-dl -q -o "$uhome/Desktop/%(title)s.%(ext)s" "$url"; echo "Completed";

Share this post


Link to post
5 hours ago, deanishe said:

If you installed it with Homebrew or by following the official instructions, it's /usr/local/bin/youtube-dl, thus:

 


/usr/local/bin/youtube-dl -q -o "$uhome/Desktop/%(title)s.%(ext)s" "$url"; echo "Completed";

 

 

Weird, swear I tried that before.  Works now, thanks!

Share this post


Link to post

Last piece is figuring out how to have it download the full quality (1080p or 4K).  This works just fine when you run youtube-dl from the command line.  It downloads the 4K video (if available) and the audio file and then ffmpeg muxes them together into one file.

 

When you just use /usr/local/bin/youtube-dl -q -o "$uhome/Desktop/%(title)s.%(ext)s" "$url"; echo "Completed"; it downloads a single file with audio and video together but it seems to always choose 720p.

 

If I use /usr/local/bin/youtube-dl -f bestvideo+bestaudio -q -o "$uhome/Desktop/%(title)s.%(ext)s" "$url"; echo "Completed";

It will download the 4K video and audio as separate files but never muxes them together.

 

I also tried adding -f bestvideo+bestaudio --merge-output-format MP4

Share this post


Link to post

Must be some permission issue.  Since I am calling my own youtube-dl script, I would think it would be able to then find ffmpeg and execute.

Share this post


Link to post

So you used exactly the same command in a shell and got a different result? Or you used a (even slightly) different command in the shell and got a different result?

 

In either case, your question now requires knowledge of youtube-dl, not (just) Alfred. I certainly don't have it, David rarely posts anymore, and I don't know about anyone else. As such, I think your best bet for a fast solution is the youtube-dl site. 

 

 

Share this post


Link to post
1 minute ago, deanishe said:

So you used exactly the same command in a shell and got a different result? Or you used a (even slightly) different command in the shell and got a different result?

 

In either case, your question now requires knowledge of youtube-dl, not (just) Alfred. I certainly don't have it, David rarely posts anymore, and I don't know about anyone else. As such, I think your best bet for a fast solution is the youtube-dl site. 

 

 

 

Ya, figured we were getting into the weeds.  Thanks @deanishe

Share this post


Link to post
3 minutes ago, petebocken said:

Must be some permission issue.  Since I am calling my own youtube-dl script, I would think it would be able to then find ffmpeg and execute.

 

Yeah, that could definitely be an issue. Presumably, ffmpeg is in /usr/local/bin or somewhere else Mac apps don't look by default. 

 

IIRC, youtube-dl has an option for passing the path to ffmpeg, but it's been a long time since I used it directly. 

Share this post


Link to post
3 hours ago, petebocken said:

getting into the weeds

 

Never heard that before. Had to look it up. Quite a useful expression. Thanks!

 

I just dug out an old script that wraps youtube-dl. --ffmpeg-location /usr/local/bin/ffmpeg is likely the option you need.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now