Using youtube-dl on WD MyCloud EX2 Ultra
Von Carsten
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:
- Enable SSH access
- Login via SSH
- Download youtube-dl
- 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
:
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// disable any timeouts, download could take a long time
set_time_limit(0);
define('BASE_DIR', '/mnt/HD/HD_a2/Public/Shared Videos');
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/bootstrap.css" rel="stylesheet"/>
</head>
<body>
<?
switch ($_SERVER['REQUEST_METHOD']) {
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=..."/>
</div>
<button type="submit" class="btn btn-primary">Abschicken</button>
</form>
<?
break;
case 'POST':
if (array_key_exists('url', $_POST)) {
$url = $_POST['url'];
?>
<h1><strong>Downloading</strong></h1>
<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,
$url]);
?>
<div>Using the following command:</div>
<div><code><?= $cmd ?></code></div>
<hr />
<h2>Output</h2>
<pre><?
flush();
$handle = popen($cmd, 'r');
while (!feof($handle)) {
echo fread($handle, 8096);
flush();
}
pclose($handle);
?></pre>
<div><strong>Fertig.</strong></div><?
} else {
?>No URL given.<?
}
break;
}
?>
<script src="js/bootstrap.js"></script>
</body>
</html>
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.