Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
PHP FFmpeg video aspect ratio problem [SOLVED]
29 août 2011, par Herr Kaleuni compiled the new version of FFMPEG and the padding commands have been deprecated.
As i try to get familiar with the new -vf pad= commands, i want to ask, how can i
convert a video without changing it's aspect ratio.I've checked numerous solutions from stackoverflow, nothing seemed to work.
Can someone, please post a working PHP example or cmd line. I would be VERY happy.Please note that the videos in question, could be 4:3 and also be 16:9
Let's say, i convert a 16:9 video to 640x480 format. It will need some bars at
the top and at the bottom. That is what i want to do.Thanks
EDIT:
I solved the problem on my own. The FFmpeg documentation is a little bit weird so
you have to experiment yourself a little bit.
The padding formula is like:$pad_horizontal = $target_width + $pad_left + $pad_right; $pad_vertical = $target_height; // blah $command .= " -vf pad=$pad_horizontal:$pad_vertical:". $pad_left .":". $pad_top .":black";
Pay special attention at the $pad_vertical part since the paddings there are better
not added so that the padding calculation of ffmpeg is not broken.Here is the full source code to the demo
<? /*********************************************************************************** get_dimensions() Takes in a set of video dimensions - original and target - and returns the optimal conversion dimensions. It will always return the smaller of the original or target dimensions. For example: original dimensions of 320x240 and target dimensions of 640x480. The result will be 320x240 because converting to 640x480 would be a waste of disk space, processing, and bandwidth (assuming these videos are to be downloaded). @param $original_width: The actual width of the original video file which is to be converted. @param $original_height: The actual height of the original video file which is to be converted. @param $target_width: The width of the video file which we will be converting to. @param $target_height: The height of the video file which we will be converting to. @param $force_aspect: Boolean value of whether or not to force conversion to the target's aspect ratio using padding (so the video isn't stretched). If false, the conversion dimensions will retain the aspect ratio of the original. Optional parameter. Defaults to true. @return: An array containing the size and padding information to be used for conversion. Format: Array ( [width] => int [height] => int [padtop] => int // top padding (if applicable) [padbottom] => int // bottom padding (if applicable) [padleft] => int // left padding (if applicable) [padright] => int // right padding (if applicable) ) ***********************************************************************************/ function get_dimensions($original_width,$original_height,$target_width,$target_height,$force_aspect) { if(!isset($force_aspect)) { $force_aspect = true; } // Array to be returned by this function $target = array(); $target['padleft'] = 0; $target['padright'] = 0; $target['padbottom'] = 0; $target['padtop'] = 0; // Target aspect ratio (width / height) $aspect = $target_width / $target_height; // Target reciprocal aspect ratio (height / width) $raspect = $target_height / $target_width; if($original_width/$original_height !== $aspect) { // Aspect ratio is different if($original_width/$original_height > $aspect) { // Width is the greater of the two dimensions relative to the target dimensions if($original_width < $target_width) { // Original video is smaller. Scale down dimensions for conversion $target_width = $original_width; $target_height = round($raspect * $target_width); } // Calculate height from width $original_height = round($original_height / $original_width * $target_width); $original_width = $target_width; if($force_aspect) { // Pad top and bottom $dif = round(($target_height - $original_height) / 2); $target['padtop'] = $dif; $target['padbottom'] = $dif; } } else { // Height is the greater of the two dimensions relative to the target dimensions if($original_height < $target_height) { // Original video is smaller. Scale down dimensions for conversion $target_height = $original_height; $target_width = round($aspect * $target_height); } //Calculate width from height $original_width = round($original_width / $original_height * $target_height); $original_height = $target_height; if($force_aspect) { // Pad left and right $dif = round(($target_width - $original_width) / 2); $target['padleft'] = $dif; $target['padright'] = $dif; } } } else { // The aspect ratio is the same if($original_width !== $target_width) { if($original_width < $target_width) { // The original video is smaller. Use its resolution for conversion $target_width = $original_width; $target_height = $original_height; } else { // The original video is larger, Use the target dimensions for conversion $original_width = $target_width; $original_height = $target_height; } } } if($force_aspect) { // Use the target_ vars because they contain dimensions relative to the target aspect ratio $target['width'] = $target_width; $target['height'] = $target_height; } else { // Use the original_ vars because they contain dimensions relative to the original's aspect ratio $target['width'] = $original_width; $target['height'] = $original_height; } return $target; } function get_vid_dim($file) { $command = '/usr/bin/ffmpeg -i ' . escapeshellarg($file) . ' 2>&1'; $dimensions = array(); exec($command,$output,$status); if (!preg_match('/Stream #(?:[0-9\.]+)(?:.*)\: Video: (?P
.*) (?P [0-9]*)x(?P [0-9]*)/',implode("\n",$output),$matches)) { preg_match('/Could not find codec parameters \(Video: (?P .*) (?P [0-9]*)x(?P [0-9]*)\)/',implode("\n",$output),$matches); } if(!empty($matches['width']) && !empty($matches['height'])) { $dimensions['width'] = $matches['width']; $dimensions['height'] = $matches['height']; } return $dimensions; } $command = '/usr/bin/ffmpeg -i ' . $src . ' -ab 96k -b 700k -ar 44100 -f flv -s ' . '640x480 -acodec mp3 '. $video_output_dir . $video_filename . ' 2>&1'; define( 'VIDEO_WIDTH', '640' ); define( 'VIDEO_HEIGHT', '480' ); $src_1 = getcwd() .'/'. 'test_video1.mpeg'; $video_filename1 = 'video1.flv'; $src_2 = getcwd() .'/'. 'test_video2.mp4'; $video_filename2 = 'video2.flv'; $src_3 = getcwd() .'/'. 'test_video3.mp4'; $video_filename3 = 'video3.flv'; convert_video( $src_1, $video_filename1 ); convert_video( $src_2, $video_filename2 ); convert_video( $src_3, $video_filename3 ); function convert_video( $src = '', $video_filename = '' ) { $video_output_dir = getcwd() .'/'; @unlink ( $video_output_dir . $video_filename ); $original = get_vid_dim($src); $target = get_dimensions( $original['width'], $original['height'], VIDEO_WIDTH, VIDEO_HEIGHT, TRUE ); echo ' '; print_r( $original ); echo '
'; echo ''; print_r( $target ); echo '
'; $target_width = $target['width']; $target_height = $target['height']; $pad_left = $target['padleft']; $pad_right = $target['padright']; $pad_bottom = $target['padbottom']; $pad_top = $target['padtop']; $pad_horizontal = $target_width + $pad_left + $pad_right; $pad_vertical = $target_height; // + $pad_top + $pad_bottom; $command = '/usr/bin/ffmpeg -i ' . $src; // $command .= " -s {$target_width}x{$target_height} "; $command .= " -vf pad=$pad_horizontal:$pad_vertical:". $pad_left .":". $pad_top .":black"; $command .= ' -ab 96k -b 700k -ar 44100'; $command .= ' -f flv '; $command .= ' -qscale 4'; $command .= ' -ss 30'; $command .= ' -t 5'; $command .= ' -ac 2 -ab 128k -qscale 5 '; $command .= ' ' . $video_output_dir . $video_filename; exec( $command, $output, $status ); echo ''; print_r( $command ); echo '
'; if ( $status == 0 ) { echo '
Convert OK.
'; } else { echo ''; print_r( $output ); echo '
'; } echo '
'; echo '
'; } ?>Thank you and have fun :)
-
Decode h264 video with csharp
28 août 2011, par john bowringI am looking for a way to decode h264 (or indeed any video format) using c#. The ultimate goal is to be able to decode the images and very strictly control the playback in real time. The project I am working on is a non-linear video art piece where the HD footage is required to loop and edit itself on the fly, playing back certain frame ranges and then jumping to the next randomly selected frame range seamlessly.
I have created an app which reads image files (jpegs) in from the disk and plays them on screen in order, I have total control over which frame is laoded and when it is displayed but at full HD res it takes slightly longer than I want to load the images from hard drive (which are about 500k each), I am thinking that using a compressed video format would be smaller and therefore faster to read and decode into a particular frame however I cannot find any readily avaiable way to do this.
Are there any libraries which can do this? i.e. extract an arbitrary frame from a video file and serve it to my app in less time than it takes to show the frame (running at 25fps), I have looked into the vlc libraries and wrappers for ffmpeg but I don't know which would be better or if there would be another even better option. Also I dont know which codec would be the best choice as some are keyframe based making arbitrary frame extraction probably very difficult.
Any advice welcome, thanks
-
How do I convert a sequence of images into a movie that the filename does not start from 1 ?
27 août 2011, par ViqueI have a sequence of images to be converted into a movie. The only problem is that, I wanted to start converting image number 120 until 240 into a movie using ffmpeg but I don't know the command line for that. Is there any possible command line for this kind of case?
-
Rails 3.1 asset pipeline needs restart if new file created ?
26 août 2011, par Reed G. LawI have a server that's taking video clips with a webcam and placing them in
#{Rails.root}/app/assets/videos
. The problem is I can't see newly created video clips until the server is restarted. Is there a workaround for this?Here's the code in the controller:
@file_name = Time.now.strftime("%Y-%m-%d_%H-%M-%S-%p") + ".mp4" system("ffmpeg -f video4linux2 -s 320x240 -t 10 -i /dev/video0 #{Rails.root.to_s}/app/assets/videos/#{@file_name}")
And in the view:
-
RTP H263 frames to FFMPEG
25 août 2011, par Pierluigi CifaniI've been trying to develop an RTP demuxed based on RFC 2190 to pass to the FFMPEG (libavcodec) H263 packets in Mode-A.
However I can't seem to make the FFMPEG happy with the I-frames I am feeding it with. With fragmented frames (such as a large I-frame) I am rebuilding the whole frame following RFC 2190 and the "Marker" rules and passing it to the Libavcodec layer. However video looks buggy and it looks that I have to add some kind of header to the beginning of each frame before passing it to the Libavcodec layer. Errors like this are coming out all the time:
illegal dc 128 at 0 3 illegal ac vlc code at 0x3 Error at MB: 69 illegal ac vlc code at 4x15 Error at MB: 349 concealing 379 DC, 379 AC, 379 MV errors
I have previously built an H264 over RTP demuxer and I had to add
0x00 0x00 0x00 0x01 0x21
header to every fragment of I Frame in order for it to be decoded correctly. But I can't find anywhere documentation on how to do this with H263.Maybe you could lend me a hand?
Cheers and thanks a lot