Using youtube-dl on WD MyCloud EX2 Ultra

I’m getting more and more fed up with all the ads on youtube and because I can’t find a reliable way to chromecast local videos from my phone via NewPipe I looked for a way to download videos straight from youtube to my WD MyCloud EX2 Ultra network drive. I already installed Plex on the device, so maybe there is a way to add the functionality to it.

Unfortunately, there are no apps on the WD store to do this so naturally I decided to roll my own.

I can’t find any documentation about apps on the device, so I first tried to go the old school way, install youtube-dl on the device directly and make sure it works at all. Here’s a short rundown of the steps necessary to download your first youtube video:

  1. Enable SSH access
  2. Login via SSH
  3. Download youtube-dl
  4. Make sure everything works by downloading a video
youtube-dl -f best https://www.youtube.com/watch?v=2lAe1cqCOXo

This should download the video to your current directory. You can make sure it is there by executing ls -la.

Now, how can I use this via my phone? Sure, I can always login via SSH using TermBot. But I’d like to have a simple web interface to start the download – that was the original idea when I started looking for an app. To make some progress, I wrote this hacky PHP script and put it into the directory /var/www/youtube:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
// disable any timeouts, download could take a long time
define('BASE_DIR', '/mnt/HD/HD_a2/Public/Shared Videos');
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.css" rel="stylesheet"/>
    case 'GET':
    <form method="post" action="index.php">
        <div class="mb-3">
            <label for="url" class="form-label">Youtube URL</label>
            <input type="url" name="url" class="form-control" placeholder="https://www.youtube.com/watch?v=..."/>
        <button type="submit" class="btn btn-primary">Abschicken</button>
    case 'POST':
        if (array_key_exists('url', $_POST)) {
            $url = $_POST['url'];
            <div><a href="<?= $url ?>">Open in browser</a></div>
            $outputTemplate = '"' . BASE_DIR . '/%(uploader)s/%(title)s-%(id)s.%(ext)s' . '"';
            if (strpos($url, 'playlist') !== false) {
                // URL is a playlist, use different template
                $outputTemplate = '"' . BASE_DIR . '/%(uploader)s/%(playlist_title)s/%(title)s-%(id)s.%(ext)s' . '"';
            $cmd = '/usr/local/bin/youtube-dl '
                . implode(' ', ['-f', 'best', '--no-call-home', '--no-check-certificate',
                        '-o', $outputTemplate,
            <div>Using the following command:</div>
            <div><code><?= $cmd ?></code></div>
            <hr />
            $handle = popen($cmd, 'r');
            while (!feof($handle)) {
                echo fread($handle, 8096);
        } else {
            ?>No URL given.<?
<script src="js/bootstrap.js"></script>

The actual youtube-dl command is marked in lines 40-43.

This script will display a simple input box for the youtube URL and one single button to start the download. There is still an issue that I do not understand: the output is not streamed in chunks to the client, but only displayed after the fact. This means the page will keep loading until the video is fully downloaded – which could take a while if you download a complete playlist.

But, it works. I can now share the URL of a youtube video on my phone, download it to my network drive, watch it via Plex without ads. Good enough for the moment.

Eine Antwort auf „Using youtube-dl on WD MyCloud EX2 Ultra“

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.