2013-05-13 113 views
0

我有一个管网站和IM使用此功能来生成视频文件ImageMagick的创建大拇指

$thumbwidth = 240; //thumb width 
$thumbheight = 180; //thumb height 

$imagick_command = "-modulate 110,102,100 -sharpen 1x1 -enhance"; 

shell_exec("$ffmpeg_path -ss $first -i \"".$row[file]."\" -vcodec mjpeg -vframes 1 -an -f rawvideo -s ".$thumbwidth."x".$thumbheight." \"$image\""); 

shell_exec("/usr/local/bin/mogrify $imagick_command $image"); 

下面是大拇指导致大拇指,这个形象是正是我需要的,没有边界,等等。

enter image description here

但有时取决于视频我有这样

enter image description here

大拇指

请告诉我删除从大拇指这个黑色的空间,但需要保持拇指大小为240x180

+0

我只是测试我的版本的ffmpeg的具有固定大小调整'的ffmpeg -ss 1 -i GOPR9876.MP4 -vcodec MJPEG -vframes 1 -an -f -s rawvideo 400x100 “foo3.jpg”'我没有任何黑色边框。这很奇怪。您的源视频是否设置为变形或其他? – Danack 2013-05-14 13:52:21

+0

甚至更​​有趣,实际的视频黑色边框? – Danack 2013-05-14 13:54:30

回答

1

您需要的最好办法:

  1. 与ffmpeg的保持纵横比,从而使调整图像它不添加任何边界。下面是-vf scale=".$thumbwidth.":trunc(ow/a/2)*2

  2. 将图像调整为所需的确切大小。下面是-resize ".$thumbwidth."x".$thumbheight."\!

因此,新的命令集应该是这样的:

$thumbwidth = 240; //thumb width 
$thumbheight = 180; //thumb height 

$imagick_command = "-modulate 110,102,100 -sharpen 1x1 -enhance -resize ".$thumbwidth."x".$thumbheight."\!"; 

shell_exec("$ffmpeg_path -ss $first -i \"".$row[file]."\" -vcodec mjpeg -vframes 1 -an -f rawvideo -vf scale=".$thumbwidth.":trunc(ow/a/2)*2 \"$image\""); 

shell_exec("/usr/local/bin/mogrify $imagick_command $image"); 

测试上的ffmpeg从去年九月构建与设置为实际值的参数,使其更易于阅读:

ffmpeg -ss 1 -i GOPR9876.MP4 -vcodec mjpeg -vframes 1 -an -f rawvideo -vf scale=240:trunc\(ow/a/2\)*2 "foo.jpg" 
+0

嗯,这行有一些问题 'shell_exec(“$ ffmpeg_path -ss $ first -i \”“。$ row [file]。“\”-vcodec mjpeg -vframes 1 -an -f rawvideo -vf scale =“。$ thumbwidth。”:trunc(ow/a/2)* 2 \“$ image \”“);' logs说:创建第二个缩略图#1 ...失败!无法创建缩略图! – 2013-05-14 12:06:15

+0

在我逃过()圆括号之后,它适用于我。 – Danack 2013-05-14 12:20:57

+0

是的,我已经试过'scale = 240:trunc \(ow/a/2 \)* 2',但不知道这里的问题是什么 – 2013-05-14 12:41:23

0

使用ffmpeg比例过滤器创建溢出240x180像素区域的缩略图。

想象一下,您有一个320x256像素的视频。要获得240的宽度,您需要以0.75的因子进行缩放。要获得180的高度,您需要按0.70进行缩放。如果您最大限度地利用两个因素,您将获得尺寸为240x192的缩略图,使目标区域溢出而没有任何黑色边框。

接下来做中心作物来获得完美的240x180缩略图,上下移动6px。

如果视频高度大于其宽度,则数学是相同的。调整大小后,您将获得一个高度为180并且宽度稍大的缩略图,并且作物将占据它的中心部分,使其完美无瑕。

有关ffmpeg,你可以在一个过滤器链使用规模和作物过滤器:

-vf scale='iw*max(240/iw\, 180/ih):-1', crop=240:180` 

你应该建立从国外输入的命令行字符串时使用escapeshellarg()。这里是PHP代码,你可以使用:

<?php 

// static configuration - safe 
$thumbwidth = 240; 
$thumbheight = 180; 
$ffmpeg_path = 'ffmpeg'; 

// foreign input - unsafe 
$first = '00:00:30'; 
$row = array('file' => '/home/goran/File Name.avi'); 
$image = '/home/goran/File Name.jpg'; 

$cmd = sprintf('%s ' . 
    '-ss %s -i %s -vcodec mjpeg -vframes 1 -an -f rawvideo ' . 
    '-vf scale=\'iw*max(%d/iw\, %d/ih):-1\', crop=%d:%d '. 
    '%s', 
    $ffmpeg_path, 
    escapeshellarg($first), escapeshellarg($row['file']), 
    $thumbwidth, $thumbheight, $thumbwidth, $thumbheight, 
    escapeshellarg($image)); 

shell_exec($cmd); 
+0

你的意思是从命令行运行如下命令: 'ffmpeg -ss 50 -i 2.flv -vcodec mjpeg -vframes 1 -an -f rawvideo -vf scale ='iw * max(240/iw \,180/ih): - 1'-vf crop ='240:180'“1.jpg”' ? – 2013-05-22 02:20:36

+0

是的。它工作吗?我有ffmpeg版本1.0.7。 – 2013-05-22 08:23:22

+0

是的拇指创建,但再次与黑色的边界...嗯..也许我需要创建拇指像在YouTube上一样的维度,如果我能看到youtube生成16:9拇指..? – 2013-05-22 14:20:19