perl yt-dlp获取仅音频链接->ffmpeg管道音频->ffplay

vaqhlq81  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(320)

好的,我有一个Perl脚本,我试图弄清楚为什么它会抛出一个io错误。首先,我使用yt-dlg来获得只有音频的链接--这和预期的一样,我可以在浏览器中导航到链接。然后,我在Perl中打开一个ffmpeg管道,然后尝试读取ffmpeg的输出,最后,如果我能让这个工作,我将处理ffmpeg输出,然后发送到命名管道。
当我处理从yt-dlp获得的链接数据时,问题来自ffmpeg,我认为它与while循环有关,但我不确定是什么。我有一个名为“input”的命名管道。我使用以下命令调用ffmpeg:

#/usr/bin/perl
use strict;
use warnings;

my $file = /path/to/named/pipe
my $read_len = 1024;

open (my $SOURCE_AUDIO, '-|', "ffmpeg -y -i \'$link\' -map 0 -c copy -ac 2 -f opus -ar 48000 pipe:1");
binmode($SOURCE_AUDIO);

# process the ffmpeg output.i have a function i want to implement here, 
# but i need to be able to parse ffmpeg output in set read lengths

while( read($SOURCE_AUDIO, my $buf, $read_len)){
print $file $buf;
};

但是在回放结束之前,在音频流结束附近的某处FFMPEG抛出如下错误:

[tls @ 0x5d0de00] Error in the pull function..2kbits/s speed=1.21x
[tls @ 0x5d0de00] IO error: Connection reset by peer
[tls @ 0x5d0de00] The specified session has been invalidated for some reason.
    Last message repeated 1 times
https://rr3---sn-(truncated): Input/output error
size=    1021kB time=00:01:18.36 bitrate= 106.7kbits/s speed=1.21x
video:0kB audio:1012kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.808163%

我不确定是什么原因导致它提前结束,或者是什么原因导致它被终止。我可以下载该文件并重新编码它(如果需要),然后用ffplay完美地播放它,但我不能,对于我的生命,解析ffmpeg输出并将其写入一个命名管道。任何帮助肯定将不胜感激。谢谢
P.S.我正在使用最新更新的windows 11和WSL的内置Perl:

This is perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi
(with 50 registered patches, see perl -V for more detail)

Copyright 1987-2019, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
vlju58qv

vlju58qv1#

我能够通过让yt-dlp处理下载来绕过这个错误,然后将yt-dlp输出通过管道传输到ffmpeg。然后我能够在while循环中读取ffmpeg输出:

#!/usr/bin/perl
use warnings;
use strict;

my $query = shift;

open my $file, '+>', "/home/user/pipes/input" or die $!;
binmode($file);

# system("yt-dlp -q -f bestaudio[acodec=opus] ytsearch:'$query' -o - | ffmpeg -hide_banner -loglevel error -i pipe: -map 0 -c copy -ac 2 -f opus -ar 48000 pipe: | ffplay -i pipe:");

open(my $SOURCE_AUDIO, '-|', "yt-dlp -q -f bestaudio[acodec=opus] ytsearch:'$query' -o - | ffmpeg -hide_banner -loglevel error -i pipe: -map 0 -c copy -ac 2 -f opus -ar 48000 pipe:");
binmode($SOURCE_AUDIO);

while ( <$SOURCE_AUDIO> ) {
    print $file $_;
    print length($_) . "\n"; # prints length of current data stream captured from ffmpeg
}

正如我所期望的那样工作。

相关问题