jjzjj

php - ffmpeg 和 php 从视频中获取图像并将视频转换为 ogg

coder 2024-01-03 原文

我有一个视频托管站点,并已在我的本地服务器上成功安装了 ffmpeg。一切正常,但我无法获得视频持续时间,也不知道如何将视频转换为 ogg 格式。我可以将视频转换为 mp4,但不确定是否同样的代码也可以转换为 ogg

还有一件事是,我可以在视频开始时从视频中获取缩略图,但我想在 50 秒后获取。

   $base = basename($uploadfile, $safe_file['ext']);
                $new_file = $base.'mp4';
                $new_image = $base.'jpg';
                $new_image_path = $live_img.$new_image;
                $new_flv = $live_dir.$new_file;

  equire 'vendor/autoload.php';
        //ececute ffmpeg generate mp4
        exec('ffmpeg -i '.$uploadfile.' -f mp4 -s 896x504 '.$new_flv.'');
        //execute ffmpeg and create thumb
        exec('ffmpeg  -i '.$uploadfile.' -f mjpeg -vframes 71 -s 768x432 -an '.$new_image_path.''); 

最佳答案

由于您实际上将三个问题合二为一,所以让我逐步完成此过程,但首先是关于我测试过的 ffmpeg 构建版本和启用功能的一些信息。

ffmpeg version 2.6.2 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
  configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvo-aacenc --enable-libvidstab
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Hyper fast Audio and Video encoder

时长


因为您安装了 ffmpeg,您可能还安装了 ffprobe

ffprobe

ffprobe gathers information from multimedia streams and prints it in human- and machine-readable fashion...

因此,要使用 ffprobe 获取持续时间,您可以执行以下操作:

exec('ffprobe -i input-file.mp4 -show_format | grep duration 2>&1', $result);
echo "<pre>";
var_dump($result[0]);  // string(19) "duration=104.022000"
echo "</pre>";

-- 更新--

LordNeckbeard 非常友善地指出这里没有必要使用 grep 实用程序。由于它是基于 Unix 的工具,因此并非在所有操作系统上都存在,因此他的建议是绝对正确的。

所以这是一个没有使用 grep 的例子,可以在下面的 http://trac.ffmpeg.org/wiki/FFprobeTips 中找到。页面。

exec('ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input-file.mov 2>&1', $result);
echo "<pre>";
var_dump($result[0]);  // string(11) "5634.401068"
echo "</pre>";

缩略图


ffmpeg 文档页面上有一个示例,它解释了如何使用以下参数从调用 ffmpeg 的视频中提取多个缩略图:

ffmpeg -i foo.avi -r 1 -s WxH -f image2 foo-%03d.jpeg

This will extract one video frame per second from the video and will output them in files named foo-001.jpeg, foo-002.jpeg, etc. Images will be rescaled to fit the new WxH values.

If you want to extract just a limited number of frames, you can use the above command in combination with the -vframes or -t option, or in combination with -ss to start extracting from a certain point in time.

要从某个时间点提取一个缩略图,您必须将 -ss 命令与 -vframes 结合使用,它是 - 的别名帧:v。 这是您将如何执行此操作的示例:

exec('ffmpeg -i input.mp4 -r 1 -ss 00:00:50 -vf scale=1024:-1 -vframes 1 output.jpeg 2>&1', $result);
echo "<pre>";
print_r($result);
echo "</pre>";

但请注意有关-ss 命令的以下信息。

Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.

还要确保您的输出路径是可写的。否则你会得到一个类似这样的错误:

[image2 @ 0x35ae280] Could not open file : /output/path/to/your/file

Theora 和 Vorbis 编码


这是我唯一无法测试的东西,因为我构建的 ffmpeg 没有

--enable-libtheora

根据以上信息可以看出是flag。您基本上需要编译 ffmpeg 以使所需的库能够编码和解码为 Theora 视频格式和 Vorbis 音频格式。

这里有一些信息可以帮助您开始。

A Brief Theora and Vorbis Encoding Guide

如果您使用上述库进行自定义构建,您应该能够执行以下操作:

exec('ffmpeg -i input.mkv -codec:v libtheora -qscale:v 7 -codec:a libvorbis -qscale:a 5 output.ogv 2>&1', $result);
echo "<pre>";
print_r($result);
echo "</pre>";

Here是可以找到其中大部分信息的链接。

希望这对您有所帮助。

关于php - ffmpeg 和 php 从视频中获取图像并将视频转换为 ogg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847790/

有关php - ffmpeg 和 php 从视频中获取图像并将视频转换为 ogg的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  3. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  4. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  5. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  6. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  7. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  8. ruby-on-rails - Ruby url 到 html 链接转换 - 2

    我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.

  9. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  10. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

随机推荐