0

This is an improvement to previously discussed: Constantly check folder for specific file type and run terminal command with new filename as an argument

I used to love Automator for such tasks, but found in this task passing arguments with filenames in Automator steps failing for me. I'm open to good script as well.

I have an Intel and M2 Macs, old one is portable and I found M2 is recoding (lower res and crop) video faster than Intel would even with sending results over WiFi. Puzzling myself to do following

  1. Samba share on M2 accessible over WiFi

  2. Either edit (if was emptied in step 4) or new file queue.txt with YouTube link. Folder MMi is needed in step 4 for correct work

  3. A nifty programs entr and yt-dlp looking at updates in queue.txt and if there is new - downloading 720p .webm video as example https://www.youtube.com/watch?v=WO2b03Zdu4Q

    ls queue.txt | entr yt-dlp -q -f "best[height<=720]" --batch-file queue.txt
    
  4. yt-dlp conveniently adds .part to file in download progress not to interfere with following steps, once it's completed a script (thanks to @nohillside) picks up encoding:

    while :; do
      ls . | find . -depth 1 -type f -iname "*.webm" -exec sh -c '
        f="$1"
        mv -- "$f" MMi/
        handbrakecli --encoder x264 --width 800 --format av_mp4 --optimize --non-anamorphic --rate 30 -i "MMi/$f" -o "MMi/${f%.webm}.mp4" 
        rm "MMi/$f"
        cat /dev/null > yt.txt
        ' _ {} \;
    done
    
  5. As it's finished, need to rsync it back to Intel Mac over WiFi (ssh keys are already exchanged)

    rsync MMi/*.mp4 user@intel_mac/MMi
    

Is there a way to combine everything in one script, other than maintaining three "daemon", maybe launchctl on a .plist or python program?

Are there guides or templates to get this script running as a daemon on macOS?

9
  • 1
    Sure, this can be combined. Where exactly are you struggling?
    – nohillside
    Commented Jan 5 at 18:42
  • yt-dlp runs on entr event of updated queue.txt file and do not progress to encoding. Run the while in detached in another terminal. Want to create an service to do launch this in background on Mac boot. Commented Jan 5 at 18:45
  • 1
    yt-dlp can download/convert to mp4 directly, so the whole loop isn‘t even necessary. And once the file is downloaded, just have it rsynced back.
    – nohillside
    Commented Jan 5 at 19:03
  • 1
    You can also just string them together into one script
    – nohillside
    Commented Jan 5 at 19:43
  • 1
    Also, for your scenario I would go with just one script in the M2 Mac which does all the processing and uses scp to copy the result to the Intel machine, and then call that script via ssh and with the URL as a parameter. This would avoid all the folder watching and polling.
    – nohillside
    Commented Jan 6 at 6:46

1 Answer 1

1

After some digging and tries came up with following solution.

I created in my network attached folder a file com.ppervoy.yt-dlp.plist with following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.ppervoy.yt-dlp</string>

  <key>WorkingDirectory</key>
  <string>/Volumes/HDD/</string>

  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/yt-dlp</string>
    <string>--format</string>
    <string>22</string>
    <string>--batch-file</string>
    <string>/Volumes/HDD/_queue.txt</string>
  </array>

  <key>KeepAlive</key>
  <true/>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/Volumes/HDD/yt-dlp.err</string>

  <key>StandardOutPath</key>
  <string>/Volumes/HDD/yt-dlp.out</string>
</dict>
</plist>

Important to have WorkingDirectory. Each word of your ProgramArguments in your command line should be in separate <string>. Path to yt-dlp is required - try which yt-dlp for yours. Nice to have StandardError and StandardOut while setting up, later may change to /dev/null.

Do sudo launchctl load com.ppervoy.yt-dlp.plist (might require chown root:wheel) and you're good to add youtube links to _queue.txt file.

BONUS: you can play a video directly from youtube with ffplay: yt-dlp "https://www.youtube.com/watch?v=rOKSMcLtY1A" -o - 2>/dev/null | ffplay - &>/dev/null This works for live broadcasts as well!

You must log in to answer this question.

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