Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG - 60 seconds from any part of video

Tags:

php

video

ffmpeg

What I want to do is create a 60 second FLV from a video that is uploaded. But I dont want to always get the first 60 seconds, if possible I would like to get the middle part of the video. but if not I want to get a random 60 second part of a video file and create the flv.

I am using the following script to make the FLV file

$call="/usr/bin/ffmpeg -i ".$_SESSION['video_to_convert']." -vcodec flv -f flv -r 20 -b ".$quality." -ab 128000 -ar ".$audio."  ".$converted_vids.$name.".flv -y 2> log/".$name.".txt";

$convert = (popen($call." >/dev/null &", "r"));
pclose($convert);

So my question is, how do i get 60 seconds from a video randomly and convert it?

like image 262
RussellHarrower Avatar asked Dec 03 '11 12:12

RussellHarrower


People also ask

How do I set the duration of a FFmpeg video?

Getting the Duration To get the duration with ffprobe , add the -show_entries flag and set its value to format=duration . This tells ffprobe to only return the duration. To convert the above value into minutes and seconds, round it to an integer and divide it by 60.


Video Answer


2 Answers

You can slice a piece of video using this command (1):

ffmpeg -sameq -ss [start_seconds] -t [duration_seconds] -i [input_file] [output_file]

And you can get the video duration using this command (2):

ffmpeg -i <infile> 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//

So just use your favorite scripting language and do this (pseudocode):

* variable start = (max_duration - 60) / 2
* execute system call command (1) with
    [start_seconds] = variable start   # (starts 30s before video center)
    [duration_seconds] = 60            # (ends 30s after video center)
    [input_file] = original filename of video
    [output_file] = where you want the 60-second clip to be saved

In php that would be:

$max_duration = `ffmpeg -i $input_file 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//`;
$start = intval(($max_duration - 60) / 2);
`ffmpeg -sameq -ss $start -t 60 -i $input_file $output_file`;
like image 131
Ben Lee Avatar answered Sep 19 '22 20:09

Ben Lee


This short tutorial describes a way to cut a video using FFMPEG. The basic syntax consists of these switches:

  • -ss [start_seconds] sets the start point in seconds.
  • -t duration tells FFMPEG how long the clip should be.

So your call would look something like this:

$call="/usr/bin/ffmpeg -i ".$_SESSION['video_to_convert']." \
-vcodec flv \
-f flv \
-r 20 \
-b ".$quality." \ 
-ab 128000 \
-ar ".$audio." \
-ss 0 \
-t 60 \
".$converted_vids.$name.".flv -y 2> log/".$name.".txt"

To get the first 60 seconds of video.

As stated in my comment, having a serious look at the Wadsworth Constant would be a good idea for your needs.

like image 30
Bojangles Avatar answered Sep 16 '22 20:09

Bojangles