Ffmpeg and php to get image from video and convert video to ogg

I have a website for video hosting and ffmpeg has been successfully installed on my local server. Everything works as a whole, but I can’t get the duration of the video and don’t know how to convert the video to ogg format. I can convert videos to mp4, but I'm not sure that the same code can also convert to ogg.

Another thing is that I can get a thumbnail from the video at the beginning of the video, but I want it in 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.''); 
+6
source share
2 answers

Since you actually have three questions, let me go through this step by step, but first of all, I tested this with some information about the version and the included ffmpeg build functions.

 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.

ffprobe

ffprobe collects information from multimedia streams and prints it humanly and machine readable ...

So, to get the duration with ffprobe, you can 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 as to indicate that there is no need to use the grep utility. And since it is a Unix-based tool, and therefore it is not present on all operating systems, its proposal is absolutely correct.

So, here is an example without using grep , which can be found on the next page 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>"; 

Icon


There is an example on the ffmpeg docs page that explains how to extract multiple thumbnails from the ffmpeg video call with the following options:

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

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

If you want to extract only a limited number of frames, you can use the above command in combination with the -vframes or -t options or in combination with -ss to start extraction from a specific point in time.

To extract a single sketch from a specific point in time, you will have to use the -ss command in combination with -vframes , which is an alias for -frames:v . Here is an example of how you will do this:

 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 remember the following information about the -ss .

Please note that in most formats it is not possible to search accurately, therefore ffmpeg will search for the closest search point to the position. When transcoding and -accurate_seek is enabled (by default), this additional segment between the search point and the position will be decoded and discarded. When a stream is being copied or when -noaccurate_seek is used, it will be saved.

Also make sure the output path is writable. Otherwise, you will receive an error message:

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

Theora and Vorbis Encoding


This is the only thing I could not verify due to the fact that in my assembly ffmpeg is not

--enable-libtheora

as you can find out from the above information. You basically need to compile ffmpeg to allow the necessary libraries to allow and decode Theora video format and Vorbis format.

Here is some information to get you started.

Theora and Vorbis Coding Quick Start Guide

If you have a custom assembly with the specified libraries, you can 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 a link to where you can find most of this information.

Hope this helps.

+6
source

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 details here https://www.ffmpeg.org/documentation.html

+3
source

All Articles