2013-09-25 51 views
3

使用this api我已經設法下載流數據,但我無法弄清楚如何解析它。我已經看過RMTP格式,但它似乎不匹配。下載twitch.tv流的第一幀

from livestreamer import Livestreamer 

livestreamer = Livestreamer() 

# set to a stream that is actually online 
plugin = livestreamer.resolve_url("http://twitch.tv/froggen") 
streams = plugin.get_streams() 
stream = streams['mobile_High'] 
fd = stream.open() 
data = fd.read() 

我上傳的數據here的一個例子。

理想情況下,我不必將它解析爲視頻,我只需要第一個關鍵幀作爲圖像。任何幫助將不勝感激!

更新:好的,我得到OpenCV的工作,它的作品抓住我有一個隨機視頻文件的第一幀。但是,當我使用與流數據相同的代碼時,它生成了nonsense image

+0

我設法打開與OpenCV庫文件(Python代碼:'捕獲= cv2.VideoCapture( 「下載/(uploadMB.com)stream.bin」)')我設法脫身的圖像它(3頻道,1280x720),但它看起來像一個五彩的混亂。你確定有效的關鍵幀在文件中嗎? – Igonato

+1

我剛剛嘗試下載[流數據的第一個1 MB](http://www.uploadmb.com/dw.php?id=1380209204),並使用'cv2.VideoCapture',然後'capture.read',但它返回False/None。你是如何得到一張圖片的? – doeke

回答

3

好吧,我想通了。確保寫入二進制數據,並且OpenCV能夠解碼第一個視頻幀。由此產生的圖像有R和B頻道切換,但很容易糾正。下載大約300 kB似乎足以確保完整的圖像在那裏。

import time, Image 

import cv2 
from livestreamer import Livestreamer 

# change to a stream that is actually online 
livestreamer = Livestreamer() 
plugin = livestreamer.resolve_url("http://twitch.tv/flosd") 
streams = plugin.get_streams() 
stream = streams['mobile_High'] 

# download enough data to make sure the first frame is there 
fd = stream.open() 
data = '' 
while len(data) < 3e5: 
    data += fd.read() 
    time.sleep(0.1) 
fd.close() 

fname = 'stream.bin' 
open(fname, 'wb').write(data) 
capture = cv2.VideoCapture(fname) 
imgdata = capture.read()[1] 
imgdata = imgdata[...,::-1] # BGR -> RGB 
img = Image.fromarray(imgdata) 
img.save('frame.png') 
# img.show() 
+0

我試圖完成相同的任務,但對capture.read()的調用不返回任何內容。我已經用你上傳的bin試過了,但是我得到了相同的None結果。有任何想法嗎? – sheitan

+1

由於ffmpeg(視頻解碼器)無法正常訪問,因此無法調用bin文件的read(),因此打開文件失敗。見http://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows和類似的。我通過從這裏下載和安裝opencv進行修復:http://www.lfd.uci.edu/~gohlke/pythonlibs/ –