0

What I am trying to achieve:

  1. Click a YouTube link in an app
  2. This URL is used by my youtube-dl script to download the video
  3. youtube-dl is configured to download the highest quality video and audio, then use ffmpeg to combine them into one file

What I have so far:

I use Finicky as my default browser. This gives me control over what browsers open certain links based on rules I write for it. The long and short of the tool is that it detects YouTube links, converts them from their usual form (e.g. https://www.youtube.com/watch?v=XxXxXxXXX_X) to the shortened, share-friendly form (e.g. https://youtu.be/XxXxXxXXX_X), then sends this to an AppleScript.

The AppleScript is saved as an app and is below.

#!/bin/zsh
on open location input
    do shell script "youtube-dl  " & input
end open location

This runs the shell script youtube-dl https://youtu.be/XxXxXxXXX_X. When I type this exact command into Terminal everything works perfectly: youtube-dl does it's thing, including doing something with ffmpeg to combine the downloaded files. However, when this AppleScript runs it's like ffmpeg isn't involved. I get a video file and an audio file.

I think my issue is in my AppleScript, but I am out of my depths with it. What can I do to make my AppleScript run things just like they run in Terminal?

1
  • 1
    I've rolled back your edit, please post your solution as an answer below instead.
    – nohillside
    Commented Feb 11, 2021 at 12:40

3 Answers 3

5

What can I do to make my AppleScript run things just like they run in Terminal?

Well, you could tell the Terminal app to run your command:

tell application "Terminal" to do script "youtube-dl  " & input

But, I don't recommend this route, because it will actually open a Terminal window!

There are a whole host of differences between do shell script and typing commands into the Terminal. For starters, do shell script commands are run via sh, rather than the more advanced bash or zsh (And, your "#!/bin/zsh" shabang line at the top will have no effect in an Applescript!).

As to your actual problem, it sounds like youtube-dl can't find your copy of ffmpeg—probably because another difference between the Terminal app and do shell script is your PATH is set differently. Try specifying the location of your ffmpeg binary with youtube-dl's --ffmpeg-location flag.

If it helps to have a reference to look at, here is an Applescript I use to download videos from my web browser with youtube-dl. Note that I've been tweaking this for many years, so it has accumulated a lot of error checking, and code for compatibility with different browsers. Don't be intimidated! https://gist.github.com/Wowfunhappy/c2c8fc097a8b4f47430811fc1d0da041

3

osascript

#!/usr/bin/osascript
on open location input
    do shell script "/usr/local/bin/youtube-dl  " & quoted form of input
end open location

If run as a script, the hash-bang of your script should be the path to osascript. This is the process that runs AppleScript (an Open Scripting Architecture language) on macOS.

Quote the URL passed into youtube-dl to ensure it appears as a single argument to the tool.

Use the full path to youtube-dl to ensure the tool is found. Use which youtube-dl in Terminal to find the path.

Command Line Arguments

If the AppleScript is being passed the URL as the first command line argument, use the argv handler:

#!/usr/bin/osascript
on run argv
   do shell shell script "/usr/local/bin/youtube-dl " & quoted form of (item 1 of argv)
end run

If this is case, consider removing the script entirely and passing the argument directly to youtube-dl.

Post Processing

youtube-dl has the ability to post-process downloads. You can control this using a youtube-dl configuration file.

To specify the ffmpeg binary to be used add the following line to either a file at /etc/youtube-dl.conf or ~/.config/youtube-dl/config:

--ffmpeg-location /usr/local/bin/ffmpeg
1

This looks an awful lot like a path problem - you have a script youtube-dl and the program /usr/local/bin/youtube-dl - in your AppleScript, try giving it the full path to your script.

3
  • Can you expand on what exactly I need to change, or where I can find the full path? I'm new to AppleScript. Thank you for the answer!
    – Dillon
    Commented Feb 7, 2021 at 4:31
  • This is just a guess based on the info you've shared, but I believe if you change do shell script "youtube-dl " & input to do shell script "~/youtube-dl " & input you should be good to go. Note that "~/" means "in your home directory", so if your script is in another directory, replace "~/" with whatever that directory is.
    – iapitus
    Commented Feb 7, 2021 at 5:06
  • For more info about the environment in AppleScript using "do shell script" stuff, check out this doc
    – iapitus
    Commented Feb 7, 2021 at 5:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .