Youtube-dl in automator action file; video and audio not muxed

applescriptautomatorterminalyoutube

I built an AppleScript to run as a Service for Safari. But I am running into a problem where the audio and video aren't being muxed. This doesn't happen when entering the same command into terminal.

I have a configuration file for youtube-dl:

-f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]'
-o '/Volumes/Godzilla/Video/YouTube Video/%(title)s-%(id)s.%(ext)s'

This is the automator script which uses the current Safari URL as input:

on run {input, parameters}
    try
        do shell script "/usr/local/bin/youtube-dl " & input
        (display notification input with title "YouTube Video Downloaded") beep
        delay 1 --> avoid quit before notice
    on error
        display notification "YouTube Download Failed" -- show error message
    end try
end run

EDIT: OK, I have requested the audio file in the config the question is why are the files not being merged after AppleScript runs youtube-dl?

I changed my AppleScript to this which provided some details to the issue:

on run {input, parameters}
try
    tell application "System Events"
        set pid to do shell script "cd /tmp/; /usr/local/bin/youtube-dl  --newline " & input & " > /tmp/vidstatus 2>&1 & echo $!"
        delay 1
        repeat while ((do shell script "kill -0 " & pid) is "") -- check if pid is still responding
            display dialog "Status: " & (do shell script "tail -n 1 /tmp/vidstatus") -- display last line of output
        end repeat
    end tell
on error
    display notification "YouTube Download Failed" -- show error message
end try end run

This is the message I get from Status:

Status: [youtube] IrxpSFtGX1k: Downloading webpage
[youtube] IrxpSFtGX1k: Downloading video info webpage
WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.

This is odd because they are obviously installed. The same request is working from terminal. I am testing this script from within Automator—could this be the issue?

Best Answer

I did some more digging and found I needed to add the location of ffmpeg binary in the "do shell script" call by adding: --ffmpeg-location /usr/local/bin/ffmpeg

on run {input, parameters}
    try
        tell application "System Events"
            set pid to do shell script "cd /tmp/; /usr/local/bin/youtube-dl  --newline --ffmpeg-location /usr/local/bin/ffmpeg " & input & " > /tmp/vidstatus 2>&1 & echo $!"
            delay 1
            repeat while ((do shell script "kill -0 " & pid) is "") -- check if pid is still responding
                display dialog "Status: " & (do shell script "tail -n 1 /tmp/vidstatus") -- display last line of output
                delay 5
            end repeat
        end tell
    on error
        display notification "YouTube Download Finished" -- show error message
    end try
end run