youtube-dl-php icon indicating copy to clipboard operation
youtube-dl-php copied to clipboard

Add yt-dlp support

Open Write opened this issue 2 years ago • 22 comments

I currently tried to change binary path to yt-dlp, it seems to download the video correctly, the "only" problem I found for now is $collection = $yt->download($options); being empty even if the download is successful

Now, i'm not saying it's easy to fix, but I tought I just might ask as yt-dlp fix issues I had for so many websites that I had with youtube-dl so if support for yt-dlp is possible and not too complexe, that would be awesome !

Thanks

Write avatar Oct 06 '21 18:10 Write

I am not familiar with yt-dlp so I don't know what are the differences that the collection is not properly built

norkunas avatar Oct 07 '21 05:10 norkunas

YouTube-dl is going to die, its only a matter of time. He has 875 pull requests, 3900 unresolved tickets, hasn't updated since June. It's over, time to port this to use yt-dlp hah.

yt-dlp is going to be the replacement. I might do a PR if I get desperate enough. for now I'm just using a brittle one liner cmd.

davidsalazar avatar Oct 21 '21 03:10 davidsalazar

I might do a PR

Feel free :) I'm not against it.

norkunas avatar Oct 21 '21 03:10 norkunas

By the way I've just installed yt-dlp to test with at least demo code from readme and it works for me, so for proper debugging I'd need URL's that doesn't work.

norkunas avatar Oct 21 '21 03:10 norkunas

Well no matter which video I try $collections is always empty. But the video is correctly downloaded.

error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';

use YoutubeDl\Options;
use YoutubeDl\YoutubeDl;

$downloadDir = __DIR__ . '/downloads';
$cacheDir = __DIR__ . '/cache';

$binpath = '/usr/local/bin/yt-dlp';
$ffmpeg_location = '/usr/bin/ffmpeg';

$yt = new YoutubeDl();
$yt->setBinpath($binpath);

$url = "https://www.youtube.com/watch?v=CrL5tqFczPE";

$options = 
Options::create()
->downloadPath($downloadDir)
->url($url);

$collection = $yt->download($options);
var_dump($collection);

Returns object(YoutubeDl\Entity\VideoCollection)#11 (1) { ["elements":protected]=> array(0) { } }

As soon as I switch to youtube-dl binary, $collection is correctly populated

Write avatar Oct 24 '21 20:10 Write

Ok, found some small differences with yt-dlp. Could you try to install dev-master version to check if it works for you?

norkunas avatar Oct 25 '21 03:10 norkunas

Works for me ! Thanks a lot

Write avatar Oct 25 '21 07:10 Write

Good. will need to refactor tests to always test both yt-dl ant yt-dlp outputs then will release new version.

norkunas avatar Oct 25 '21 07:10 norkunas

Hey Guys, awesome project. I also found some issues with collections and yt-dlp. First, let me explain what I'm trying to achieve - Get a playlist of videos, without downloading to review them (for example calculated like\dislike proportion and so on). Option --skip-download is working well. But looks like code that reading buffer not adding all videos to the collection, only the last one.

https://github.com/norkunas/youtube-dl-php/blob/615f0c1240a3e3d464e994d2f96f7f58e27de022/src/YoutubeDl.php#L121

And here is line that adding only single(last video to collection)

https://github.com/norkunas/youtube-dl-php/blob/615f0c1240a3e3d464e994d2f96f7f58e27de022/src/YoutubeDl.php#L149

Maybe I just don't understand something, but anyway let me know what do you think. Thanks

UPD. One more here the script is failing in case of --skip-download flag https://github.com/norkunas/youtube-dl-php/blob/615f0c1240a3e3d464e994d2f96f7f58e27de022/src/YoutubeDl.php#L168

Mrlaminat avatar Dec 26 '21 19:12 Mrlaminat

Hi @Mrlaminat could you give a link to playlist you are trying to download? For me it works as expected :)

norkunas avatar Dec 27 '21 04:12 norkunas

I've released new version, could you guys test it if yt-dlp works for you properly? :)

norkunas avatar Dec 27 '21 08:12 norkunas

@norkunas Thank you for your fast reply. I want to use this lib for different purposes, but here you can check with YT links. Tried both, and empty collections in both cases. Actually, I also tried with youtube-dl, and got the same results. I assume that maybe I'm using it in the wrong way.

Options::create()
                ->downloadPath($temporaryStorage)
                ->url('https://www.youtube.com/watch?v=VP195PgSyvw&list=PLxz6_qhdtqWW929SMwKOfqgKaDAXfLqga&index=1')
                ->skipDownload(true)

https://www.youtube.com/watch?v=VP195PgSyvw&list=PLxz6_qhdtqWW929SMwKOfqgKaDAXfLqga&index=1 https://www.youtube.com/playlist?list=PLxz6_qhdtqWW929SMwKOfqgKaDAXfLqga

Not sure is it possible to use this lib for different sources.

Mrlaminat avatar Dec 27 '21 11:12 Mrlaminat

You mean you just want to retrieve all video objects without actually downloading them and that's all?

norkunas avatar Dec 27 '21 11:12 norkunas

You mean you just want to retrieve all video objects without actually downloading them and that's all?

Yes, I'm testing this feature and thinking is it possible or not. Fetch the list of videos with its meta, do business logic, download only necessary videos, and maybe with some delay(queue system). Direct downloading works really well :)

Mrlaminat avatar Dec 27 '21 11:12 Mrlaminat

Ok so the problem was that if download is skipped then the end file could not be set to metadata. Could you check if https://github.com/norkunas/youtube-dl-php/commit/99c2210db4d27bda6615f1453ae3a0d12d76b488 works for you?

norkunas avatar Dec 27 '21 11:12 norkunas

@norkunas Thank you for the fix. Working well with youtube, get the whole playlist in the collection. First I have to mention, maybe not for @norkunas , but for others. Don't use xDebug for debugging. The process is async inside the pipe, and when you debug it slowly, it's saving only the last item from the pipe. The second is preg_match is not perfect in the case of another source. Let me prepare some fix in next hours. @norkunas If you want I can contact you separately. Thank you!

Mrlaminat avatar Dec 27 '21 11:12 Mrlaminat

Contributions are very welcome :)

norkunas avatar Dec 27 '21 11:12 norkunas

~I have a problem where getVideos() is empty when using yt-dlp, is anyone having this too?~

Woops, wasn't using latest version

Ok, the problem is that $video->getFilename() doesn't return mp3 file while specifying you want the audio, it returns a .webm instead

MichaelBelgium avatar Jan 09 '22 11:01 MichaelBelgium

@MichaelBelgium could you please run in debug mode and print the output that the yt-dlp binary responds to you?

norkunas avatar Jan 10 '22 04:01 norkunas

@MichaelBelgium could you please run in debug mode and print the output that the yt-dlp binary responds to you?

Ok so using this code:

$options = Options::create()
    // ->ffmpegLocation('/usr/local/bin/ffmpeg')
    ->output('%(id)s.%(ext)s')
    ->downloadPath(Config::DOWNLOAD_FOLDER)
    ->url($youtubelink)
    ->extractAudio(true)
    ->audioFormat('mp3')
    ->audioQuality('0');

$dl = new YoutubeDl();
$dl->debug(function ($type, $buffer) {
    echo "$type: $buffer";
});

//use yt-dlp if it's installed on the system, faster downloads and lot more improvements than youtube-dl
$ytdlp = (new ExecutableFinder())->find('yt-dlp');
if($ytdlp !== null)
    $dl->setBinPath($ytdlp);

$video = $dl->download($options)->getVideos()[0];

var_dump($video->getFile());
die();

Output:

out: [youtube] _VeNOH9i06I: Downloading webpage
out: [youtube] _VeNOH9i06I: Downloading android player API JSON
out: [info] _VeNOH9i06I: Downloading 1 format(s): 251
out: [info] Writing video metadata as JSON to: download/_VeNOH9i06I.info.json
out: [download] Destination: download/_VeNOH9i06I.webm
out: 
[download]   0.0% of 2.39MiB at 181.65KiB/s ETA 00:13out: 
[download]   0.1% of 2.39MiB at 518.41KiB/s ETA 00:04out: 
[download]   0.3% of 2.39MiB at  1.15MiB/s ETA 00:02 out: 
[download]   0.6% of 2.39MiB at  2.40MiB/s ETA 00:00out: 
[download]   1.3% of 2.39MiB at 855.67KiB/s ETA 00:02out: 
[download]   2.6% of 2.39MiB at  1.03MiB/s ETA 00:02 out: 
[download]   5.2% of 2.39MiB at  1.35MiB/s ETA 00:01out: 
[download]  10.4% of 2.39MiB at  2.01MiB/s ETA 00:01out: 
[download]  20.9% of 2.39MiB at  2.70MiB/s ETA 00:00out: 
[download]  41.8% of 2.39MiB at  2.63MiB/s ETA 00:00out: 
[download]  83.6% of 2.39MiB at  3.68MiB/s ETA 00:00out: 
[download] 100% of 2.39MiB at  3.56MiB/s ETA 00:00  out: 
[download] 100% of 2.39MiB in 00:00               out: 
out: [ExtractAudio] Destination: download/_VeNOH9i06I.mp3
out: Deleting original file download/_VeNOH9i06I.webm (pass -k to keep)
object(SplFileInfo)#12 (2) {
  ["pathName":"SplFileInfo":private]=>
  string(25) "download/_VeNOH9i06I.webm"
  ["fileName":"SplFileInfo":private]=>
  string(16) "_VeNOH9i06I.webm"
}

MichaelBelgium avatar Jan 10 '22 12:01 MichaelBelgium

Ok so yt-dlp outputs as [ExtractAudio] Destination: instead of [download] Destination:. Would you like to contribute and fix this?

norkunas avatar Jan 10 '22 12:01 norkunas

Ok so yt-dlp outputs as [ExtractAudio] Destination: instead of [download] Destination:. Would you like to contribute and fix this?

Sure! I'll try haha

MichaelBelgium avatar Jan 10 '22 12:01 MichaelBelgium