2009-11-20 462 views
17

我需要爲上傳到運行python的webapp的視頻文件創建縮略圖。使用Python創建來自視頻文件的縮略圖

我該如何解決這個問題......我需要一個基本上可以爲我做這個的庫,或者可以自動從視頻文件(多種格式)中讀取圖像幀。

+1

參見[這個答案] [1]如何在GStreamer和Python做到這一點。 [1]:http://stackoverflow.com/a/16478342/1049318 – 2013-05-11 13:19:12

回答

8

PyMedia的簡單組合以及PIL會做的伎倆爲AVI,ASF,或MPEG文件。 PyMedia允許你提取幀(使用decode()例程),而PIL有一個簡單的縮略圖()例程。

+0

非常感謝。 – Alterlife 2009-12-10 08:42:39

+0

PyMedia在python 3.x上無法使用另外可能的解決方案嗎? – EduardoMaia 2017-09-22 01:48:24

+0

@EduardoMaia現在我可能會從https://libav.org/調用一個外部包,比如ffmpeg或libav。 – 2017-09-26 04:49:45

1

調查PythonMagick,ImageMagick的Python接口。這應該有你需要的。 (聲明:我以前沒有使用Python接口,但我知道ImageMagick的是良好的魔力。)

3

您可以使用Youtube API進行存儲和轉碼,並免費獲取饋送縮略圖。老實說,這是處理在線視頻最簡單的方式,我不僅僅是第三方服務的先令,我是該API的非常高興的用戶,以及我能夠刪除的內部視頻路徑。

10

您可以使用ffvideo

from ffvideo import VideoStream 
pil_image = VideoStream('0.flv').get_frame_at_sec(5).image() 
pil_image.save('frame5sec.jpeg') 
+0

ffvideo無法在python 3.x上工作你有另一種可能的解決方案嗎? – EduardoMaia 2017-09-22 01:48:38

0
import cv2 
vcap = cv2.VideoCapture(filename) 
res, im_ar = vcap.read() 
while im_ar.mean() < threshold and res: 
     res, im_ar = vcap.read() 
im_ar = cv2.resize(im_ar, (thumb_widht, thumb_height), 0, 0, cv2.INTER_LINEAR) 
#to save we have two options 
#1) save on a file 
cv2.imwrite(save_on_filename) 
#2)save on a buffer for direct transmission 
res, thumb_buf = cv2.imencode('.png', im_ar) 
# '.jpeg' etc are permitted 
#get the bytes content 
bt = thumb_buf.tostring() 

「門檻」 是一個整數。當你得到一個視頻幀時,它可以是非常黑,白等,以獲得一些好的縮略圖,你可以指定幀中所有像素的平均值。

2

我無法在OSX Sierra上安裝ffvideo,所以我決定使用ffmpeg。

OSX:

brew install ffmpeg 

的Linux:

apt-get install ffmpeg 

的Python 3代碼:

import subprocess 
video_input_path = '/your/video.mp4' 
img_output_path = '/your/image.jpg' 
subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path])