简体   繁体   English

FFmpeg(视频缩略图)在命令行中运行,但在PHP脚本中不起作用

[英]FFmpeg (Thumbnail from video) works in command line, but not in PHP script

I looked at the similar questions, and the solutions there do not work in my case. 我查看了类似的问题,那里的解决方案在我的案例中不起作用。 Anyone has any idea? 任何人有任何想法? I am trying to capture a thumbnail from a given video file. 我试图从给定的视频文件中捕获缩略图。

Later, my goal is to capture a thumbnail from live streaming video file. 后来,我的目标是从直播视频文件中捕获缩略图。

Below is my current set up. 以下是我目前的设置。

Linux Ubuntu 16.04 Linux Ubuntu 16.04

PHP version 7.0 PHP版本7.0

Nginx Version 1.10.3 Nginx版本1.10.3

FFmpeg Version FFmpeg 2.8.11 FFmpeg版本FFmpeg 2.8.11

I installed FFmpeg through below commands in my Ubuntu server. 我在我的Ubuntu服务器中通过以下命令安装了FFmpeg。

apt-get update
apt-get install ffmpeg

When I type the below command line in the terminal, it does the conversion fine. 当我在终端中键入以下命令行时,它可以很好地进行转换。

ffmpeg -i /etc/nginx/html/test.mp4 -ss 00:00:01 -vframes 1 -vf scale=240:120 /etc/nginx/html/Share/ffmpeg/thumbnails/test.png;

In order to use it in php file, I confirmed the location of ffmpeg binaries by I typing in the terminal, which ffmpeg , and it returns /usr/bin/ffmpeg 为了在PHP文件使用它,我通过我在终端上,输入确认的ffmpeg二进制文件的位置which ffmpeg ,并将其返回/usr/bin/ffmpeg

I also gave permission to the directory by using chmod 755 command line. 我还使用chmod 755命令行授予了该目录的权限。

Here is what I have in my ffmpeg.php file. 这是我在ffmpeg.php文件中的内容。 I included the full path to my ffmpeg as you can see. 正如您所见,我包括了我的ffmpeg的完整路径。

<?php
header("Content-Type: text/html;charset=UTF-8");

$ffmpeg = "/usr/bin/ffmpeg";
$videoFile="/etc/nginx/html/test.mp4";
$thumbnail= "/etc/nginx/html/Share/ffmpeg/thumbnails/thumbnail1.jpg";

$cmd = "$ffmpeg -i $videoFile -ss 00:00:01 -vframes 1 -vf scale=240:120 $thumbnail 2>&1 &";

$output = shell_exec($cmd);
echo "<pre>".$output."</pre>";
?>

This returns the message like below. 这将返回如下消息。

ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
  configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/etc/nginx/html/Share/test.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    creation_time   : 1970-01-01 00:00:00
    encoder         : Lavf53.24.2
  Duration: 00:00:06.40, start: 0.000000, bitrate: 1321 kb/s
    Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 640x368 [SAR 1:1 DAR 40:23], 932 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 383 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
[swscaler @ 0x1621be0] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '/etc/nginx/html/Share/ffmpeg/thumbnails/thumbnail1.jpg':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf56.40.101
    Stream #0:0(und): Video: mjpeg, yuvj420p(pc), 240x120 [SAR 20:23 DAR 40:23], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
      encoder         : Lavc56.60.100 mjpeg
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg4 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[image2 @ 0x16165a0] Could not open file : /etc/nginx/html/Share/ffmpeg/thumbnails/thumbnail1.jpg
av_interleaved_write_frame(): Input/output error
frame=    1 fps=0.0 q=3.9 Lsize=N/A time=00:00:00.04 bitrate=N/A    
video:8kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Conversion failed!

UPDATED: I also gave permission to ffmpeg by chmod +x $(which ffmpeg) 更新:我还通过chmod + x $(ffmpeg)授权ffmpeg

I tried php /etc/nginx/html/Share/ffmpeg/ffmpeg.php in the terminal, and it also does conversion fine. 我在终端尝试了php /etc/nginx/html/Share/ffmpeg/ffmpeg.php ,它也做了很好的转换。

  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/etc/nginx/html/Share/test.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    creation_time   : 1970-01-01 00:00:00
    encoder         : Lavf53.24.2
  Duration: 00:00:06.40, start: 0.000000, bitrate: 1321 kb/s
    Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 640x368 [SAR 1:1 DAR 40:23], 932 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 383 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
[swscaler @ 0x72dbe0] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '/etc/nginx/html/Share/ffmpeg/thumbnails/thumbnail.jpg':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf56.40.101
    Stream #0:0(und): Video: mjpeg, yuvj420p(pc), 240x120 [SAR 20:23 DAR 40:23], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
      encoder         : Lavc56.60.100 mjpeg
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg4 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
frame=    1 fps=0.0 q=3.9 Lsize=N/A time=00:00:00.04 bitrate=N/A    
video:8kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

It seems that you don't have the right permissions for the directory 您似乎没有该目录的权限
/etc/nginx/html/Share/ffmpeg/thumbnails/

That, at least, is what I get from the error message Could not open file : /etc/nginx/html/Share/ffmpeg/thumbnails/thumbnail1.jpg . 至少,这是我从错误消息中得到的Could not open file : /etc/nginx/html/Share/ffmpeg/thumbnails/thumbnail1.jpg

A quick and dirty way to test, if this is the reason for the problem, would be to change the directories permissions level to 777. (of course revert this afterwards) 一种快速而肮脏的测试方法,如果这是问题的原因,那就是将目录权限级别更改为777.(当然,之后还原)

I SOLVED the above issue with great help from @Tek and @AtaurRahman. 我在@Tek和@AtaurRahman的帮助下解决了上述问题。

It was permission issue 这是许可问题

I had to execute the chown / chmod commands to the path 我不得不对路径执行chown / chmod命令

chmod -R 777 'path';
chown -R www-data:www-data 'path';

chown command : changes the user and/or group ownership of for given file chown命令 :更改给定文件的用户和/或组所有权

chmod command : changes file access permissions (read and write access). chmod命令 :更改文件访问权限(读写访问权限)。

I also had the permission problem for sharing folder with my host operating system. 我也有与主机操作系统共享文件夹的权限问题。

As you can see my question above, /etc/nginx/html/Share/~ is where 正如你可以看到我上面的问题, / etc / nginx / html / Share /〜就在哪里

I wanted to write a .jpg file. 我想写一个.jpg文件。 I gave access read/wrtie to everyone to the sharing folder in my host OS, it works perfectly. 向所有人提供了访问read / wrtie到我主机操作系统中的共享文件夹,它运行得很好。 (I am not sure if this causes any security issue later on. If anyone knows about this, feel free to comment below to share) (我不确定这是否会导致以后出现任何安全问题。如果有人知道这一点,请随时在下面评论分享)

I hope this helps someone who is facing the same problem 我希望这可以帮助那些面临同样问题的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM