2010-10-26 75 views

回答

4

這是我的代碼獲取的幀,並生成從所述視頻圖像...

// get the duration and a random place within that 
$cmd = "ffmpeg -i " . $videoPath . " 2>&1"; 
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) { 
    $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4]; 
    $second = rand(1, ($total - 1)); 
} 
exec($cmd); 

// get the screenshot 
exec("ffmpeg -i " . $videoPath . " -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg " . $imageOutput . " 2>&1"); 

$第二變量是0和總的持續時間之間的隨機數。 和第二個exec()是從選定的幀創建一個圖像文件。

$ imageOutput是生成圖像的絕對路徑位置。 例如:/home/ariawan/image-generated.jpg

+1

ariawan,非常感謝你的幫助人。我能夠在文件夾中獲取圖像,但它是空的0字節。這是我有: – Ciprian 2010-10-27 13:04:30

+1

我不知道發生了什麼,但我正在工作的服務器已重新啓動,現在一切正常。非常感謝! – Ciprian 2010-10-28 06:41:27

+0

好的..很棒... – ariawan 2010-10-28 15:14:30

2

我使用PHP和FFMPEG得到視頻的持續時間。

$cmd = "ffmpeg -i " . $videoPath . " 2>&1"; 
    if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) { 
     $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4]; 
    } 
    exec($cmd); 

print_r()要查看的$ time變量。 確保你的機器上安裝了ffmpeg .. 希望這會有所幫助。

+0

謝謝,我會試試看。我知道ffmpeg已安裝。 – Ciprian 2010-10-26 06:57:43

+0

我不知道如何做到這一點。我創建了一個變量$ videoPath ='skittles.flv';我用print_r(time)調用$ time變量;結果是Array() – Ciprian 2010-10-26 07:01:30

+0

嘗試使用ffmpeg -i /path/path/videofile.flv。它在運行嗎? – ariawan 2010-10-26 07:09:10

7

還有一個FFMPEG PHP擴展ie。 apt-get install php5-ffmpeg然後

 
$movie = new ffmepg_movie("path/to/movie.flv"); 
$duration_in_seconds = $movie->getDuration(); 

這對我以前的工作。該擴展適用於檢索元數據並測試上傳的文件是否爲FLV等。

+1

然後除以60獲得分鐘和模60得到其餘秒 – 2010-10-26 07:59:58

+0

很酷......我也會嘗試這個選項。非常感謝你! – Ciprian 2010-10-28 06:45:53

3

我使用的是getID3 PHP library,用普通的舊版PHP編寫,沒有任何依賴關係。

它不僅會以秒爲單位給出.flv電影的持續時間,而且會將其轉換爲minute:seconds格式。下面是用v一個代碼示例1.7.9(的getid3最新穩定版):

<?php 

// getId3 library uses deprecated eregi_* functions 
// which generate errors under PHP 5.3 - so I excluded them 
error_reporting(E_ALL^E_DEPRECATED); 

// just for debugging/sample 
header('Content-Type: text/plain'); 

// include the getid3 base class in order to use the lib 
require_once('./lib/getid3.php'); 

// path to your .flv file 
$filename = './sample.flv'; 

$getID3 = new getID3(); 
$fileInfo = $getID3->analyze($filename); 

// echoes something like 127.8743 
print 'Playtime in seconds: ' . $fileInfo['playtime_seconds']; 

print chr(10); 

// echoes something like: 2:07 
print 'Playtime in minute:seconds format: ' . $fileInfo['playtime_string']; 
+0

謝謝你。我感謝您的幫助。但是我最終使用了第二個建議。任何想法如何抓住縮略圖? – Ciprian 2010-10-27 09:14:47

+0

+1爲getid3因​​爲它沒有依賴關係 – 2010-10-27 09:49:28

+0

@ciprian如何抓住縮略圖應該是一個單獨的問題。 – 2010-10-27 09:50:13

1

謝謝你的幫助。我也想從flv文件生成縮略圖。我正在嘗試使用此代碼來完成此操作。但我似乎做錯了什麼。

$cmd = "ffmpeg -i /home/web1/wpmu.site.com/htdocs/wp-content/blogs.dir/7/files/2010/10/4973_3.flv -f image2 -vframes 1 -ss 00:00:01 -y -s 100x100 /home/web1/wpmu.site.com/htdocs/wp-content/blogs.dir/7/files/2010/10/4973_3.png"; 

exec($cmd, $output, $return); 

我將它包含在test.php頁面上。它應該只是通過刷新頁面來運行?我自己添加了png文件的路徑。這有什麼問題?

由於

+0

我已經爲您的生成縮略圖問題添加了另一個答案。我也附上了代碼,但我在ffmpeg上使用的選項是jpeg。 – ariawan 2010-10-27 09:48:26