Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg and php to get an image out of a video and convert video to ogg

Tags:

php

video

ffmpeg

I have a video hosting site and have successfully installed ffmpeg on my local server. Things work overall, but I cannot get the video duration and don't know how to convert videos to the ogg format. I can convert videos to mp4 but am unsure if the same code can also convert to ogg.

One more thing is that I can get a thumbnail out of the video at the start of the video but I want it after 50 seconds.

   $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.''); 
like image 822
sonam Sharma Avatar asked Aug 06 '15 05:08

sonam Sharma


2 Answers

Since you actually have three in one question let me go through this step by step, but first of all some information on the version and enabled features of the ffmpeg build i have tested this with.

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

Duration


Since you have ffmpeg installed you probably have ffprobe as well.

ffprobe

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

So to get the duration using ffprobe you could do the following:

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>";

-- Update --

LordNeckbeard was so kind to point out that there is no need to use the grep utility here. And since it is an Unix based tool and therefor not present on all operating systems, his proposal is absolutely correct.

So here is an example without the use of grep which can be found on the following http://trac.ffmpeg.org/wiki/FFprobeTips page.

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>";

Thumbnail


There is an example on the ffmpeg docs page which explains how to extract several thumbnails from a video calling ffmpeg with the following parameters:

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.

To extract one thumbnail from a certain point in time you would have to use the -ss command in combination with -vframes which is an alias for -frames:v. Here is an example how you would do that:

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>";

But be aware of the following information on the -ss command.

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.

Also make sure that your output path is writable. Otherwise you will get an error looking like this:

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

Theora and Vorbis Encoding


This is the only thing i could not test because of the fact that my build of ffmpeg does not have the

--enable-libtheora

flag as you can tell by the above information. You basically need to compile ffmpeg to enable the needed libraries to be able to en- and decode to the Theora video format and Vorbis audio format.

Here is some information to get you started with that.

A Brief Theora and Vorbis Encoding Guide

If you have a custom build with the mentioned libraries you should be able to do the following:

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 is the link to where most of these information can be found.

Hope this helps.

like image 151
DavidDomain Avatar answered Sep 25 '22 02:09

DavidDomain


For linux:

//video converting...
shell_exec("$ffmpegPath  -i source.mp4 -s 1280x720 -metadata title='Sample' -bufsize 1835k -b:v 1000k -vcodec libx264 -acodec libfdk_aac sample.mp4");//for mp4
shell_exec("$ffmpegPath  -i source.mp4 -s 1280x720 -metadata title='Sample' -bufsize 1835k -b:v 1000k -vcodec libtheora -acodec libvorbis sample.ogg");//for ogg
shell_exec("$ffmpegPath  -i source.mp4 -s 1280x720 -metadata title='Sample' -bufsize 1835k -b:v 1000k -vcodec libvpx-acodec libvorbis sample.webm");//for webm

//Get theumbnail
shell_exec("$ffmpegPath -i input.flv -ss 00:00:14 -vframes 1 sample.png");
//you can change the -ss value you want.
//then if u want to check if you have already install ur ffmpeg just type in your terminal in linux "ffmpeg" see pic below

enter image description here

more info here https://www.ffmpeg.org/documentation.html

like image 44
Grald Avatar answered Sep 27 '22 02:09

Grald