【问题标题】:Perl not playing video with vlc pluginPerl 不使用 vlc 插件播放视频
【发布时间】:2015-12-29 06:24:56
【问题描述】:

我正在尝试在 vlc-plugin 的帮助下播放视频文件。

这是我的代码:-

#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;

my $file ='/home/abhishek/Videos/lua.mp4';
my $size = -s "$file";
my $begin=0;
my $end=$size;
(my $name, my $dir, my $ext) = fileparse($file, qr/\.[^.]*/);

open (my $fh, '<', $file)or die "can't open $file: $!";
binmode $fh;

print "Content-Type: application/x-vlc-plugin \n";
print "Cache-Control: public, must-revalidate, max-age=0";
print "Pragma: no-cache" ;
print "Accept-Ranges: bytes";
print "Content-Length:  $end - $begin\n\n";
print "Content-Range: bytes $begin'-'$end'/'$size";
print "Content-Disposition: inline; filename=\"$name$ext\"\n";
print "Content-Transfer-Encoding: binary";
print "Connection: close"; 

my $cur=$begin;
seek($fh,$begin,0);
while(!eof($fh) && $cur < $end)
{
    my $buf=1024*16;
    read $fh, $buf, $end-$cur;
    $cur+=1024*16;
}
close $fh;

这是我正在写的访问日志

127.0.0.1 - - [29/Dec/2015:11:39:31 +0530] "GET /cgi-bin/download.cgi HTTP/1.1" 200 484 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0"
127.0.0.1 - - [29/Dec/2015:11:39:32 +0530] "GET /cgi-bin/download.cgi HTTP/1.1" 200 447 "-" "(null)"

当我从apache 网站检查这意味着什么时,这就是我得到的

If no content was returned to the client, this value will be "-". To log "0" for no content, use %B instead.

没有内容返回给客户端。它返回 null。 我无法弄清楚出了什么问题。任何帮助将不胜感激。

请建议我应该怎么做才能播放视频,但我不确定这是正确的方法吗?

提前致谢

【问题讨论】:

    标签: apache perl vlc


    【解决方案1】:

    我发现您正在打印的标题存在许多重大问题:

    print "Content-Type: application/x-vlc-plugin \n";
    

    此 MIME 类型主要用于 &lt;embed&gt; 标记以调用 VLC。此文件类型的正确 MIME 类型可能是 video/mp4

    print "Cache-Control: public, must-revalidate, max-age=0";
    print "Pragma: no-cache" ;
    

    这些标题以及后面的许多其他标题都缺少终端换行符 (\n)。这会导致它们一起运行,导致意想不到的结果。

    print "Accept-Ranges: bytes";
    

    除了没有换行符之外,此标头还告诉浏览器此资源支持范围请求。但是,您的脚本实际上并没有实现这一点,这会导致浏览器非常混乱。

    print "Content-Length:  $end - $begin\n\n";
    

    Content-Length 必须是表示资源总长度的单个数字(例如,Content-Length: $size)。此外,这里有 两个 换行符,这将导致以下所有标题都被视为内容的一部分。

    print "Content-Range: bytes $begin'-'$end'/'$size";
    

    此标头通常用于范围请求,但您尚未完全实现此功能,因此此标头只会混淆问题。

    print "Content-Transfer-Encoding: binary";
    

    此标头在这里毫无意义——它主要用于电子邮件。把它放在一边。

    print "Connection: close"; 
    

    Web 服务器将根据需要设置此标头。 CGI 脚本不应生成它。

    您还缺少需要跟在最后一个标题后面的双换行符。

    【讨论】:

    • 这些内容打印应该由一个带有sub的perl模块来标准化它,我记得我很难让它工作,现在我们仍然面临同样的问题,必须有一个模块存在今天
    【解决方案2】:

    我懒得让它完全工作,这里有一些建议 不适合 cmets

    绝对会让你前进。

    # format is in the detail
    my $content_length = $end - $begin;
    print "Content-Type: text/html\r\n";
    print "Content-Type: application/x-vlc-plugin\r\n";
    print "Cache-Control: public, must-revalidate, max-age=0\r\n";
    print "Pragma: no-cache\r\n" ;
    print "Accept-Ranges: bytes\r\n";
    print "Content-Length: $content_length", "\r\n";
    print "Content-Range: bytes ${begin}-${end}/${size}\r\n";
    print "Content-Disposition: inline; filename=\"$name$ext\"\r\n";
    print "Content-Transfer-Encoding: binary\r\n";
    print "Connection: close\r\n";
    print "\r\n";
    ################################
    #
    # flush is needed
    #
    # ##############################
    use IO::Handle;
    STDOUT->autoflush;
    
    my $cur=$begin;
    seek($fh,$begin,0);
    while(!eof($fh) && $cur < $end)
    {
        my $buf=1024*16;
        read $fh, $buf, $end-$cur;
        $cur+=1024*16;
        ##############################
        #   I suspect you will need this
        #
        ###############################
        print $buf;
    }
    close $fh;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      相关资源
      最近更新 更多