Solved

FFMPEG Quality & Thumbnail

Posted on 2008-06-09
3
1,817 Views
Last Modified: 2009-12-10
Hi Guys,

I have a 2 part question.

#1. I have a script that works fine to convert uploaded video to an flv file using ffmpeg but the quality isn't great. I have tried increasing the bitrate from the default of 200kb/s to 700kb/s which seems to make the converted video much worse. What options should i use to get a better quality video output?

Here is the line of code im using:
$output = exec ("ffmpeg -i source.ext -s qvga -ab 128 -y output.flv);

#2. I have another line of code that then creates a thumbnail from the converted video but what i would like is the code to take a thumbnail from the central location of the video rather than just the start. How would i do that without knowing the length of the video to divide it?

Here is the current thumbnail code:
$output2 = exec ("ffmpeg -y -i output.flv -r 3.0 -sameq -f image2 -an output.jpg");

Thanks.
0
Comment
Question by:jwatsonPICTS
3 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 21745898
To improve quality, try increasing the frame rate via the -r option:

 $output = exec ("ffmpeg -i source.ext -s qvga -r 24 -ab 128 -y output.flv");

or

 $output = exec ("ffmpeg -i source.ext -s qvga -r 30 -ab 128 -y output.flv");

Below is a solution to finding the total number of frames in a file, which you could use to determine the middle frame:

http://lists.mplayerhq.hu/pipermail/ffmpeg-user/2006-August/003884.html (Note that the language is not PHP, but you should be able to get the general idea).
0
 
LVL 5

Accepted Solution

by:
nacker2000 earned 250 total points
ID: 21749732
Hi,

The code below should seek to the middle of the file an output a JPEG. I have tested this and it does work. Somethings to be aware of are that the function for getting the FLV duration I got from this forum: http://flixforums.com/archive/index.php/t-149.html and on it its says that depending on the file size the function could take a while, so the thumbnail should only be produced once, after the file has been uploaded.

Also the -ss command for ffmpeg moves the position in the file to the second that is passed. Some builds of ffmpeg seem to have an issue with -ss, but it wokred fine on mine so you will just have to try it and see.
<?php
	function GetFLVDuration($file){
		// get contents of a file into a string
		if(file_exists($file)){
			$handle = fopen($file, "r");
			$contents = fread($handle, filesize($file));
			fclose($handle);
		
			if(strlen($contents) > 3){
				if(substr($contents,0 , 3) == "FLV"){
					$taglen = hexdec(bin2hex(substr($contents, strlen($contents)-3)));
					if(strlen($contents) > $taglen){
						$duration = hexdec(bin2hex(substr($contents, strlen($contents) - $taglen, 3)));
						return $duration;
					}
				}
			}
		}
		return 0;
	}
	
	// get duration and divide by 2 to get middle
	$duration = number_format((GetFLVDuration('output.flv') / 1000) / 2);
	
	// check for error
	if($duration<0){
		$duration = 0;
	}
	
	//save out thumbnail
	$output2 = exec('ffmpeg -y -i output.flv -r 1 -vframes 1 -sameq -f image2 -ss ' . $duration . ' -an output.jpg');
?>

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 26025736
nacker2000, Can you suggest any useful tips to my question related to ffmpeg ?
http://www.experts-exchange.com/Programming/Multimedia_Programming/Video_Editing/Q_24967652.html

Thanks
--Raj
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question