6

我正在使用 ffmpeg-light、JuicyPixels 和光泽度来显示带有 Haskell 的视频。我想找到我正在自动播放的视频的元数据,但我还没有找到这样做的方法。

我想访问视频的分辨率和帧速率等元数据。

你能帮助我吗?

编辑:

我已经尝试过您的解决方案@CRDrost,但视频现在以 2 倍正常速度播放。我假设函数 imageReaderTime 给出了错误的时间戳。

编辑2:

播放速度异常是ffmpeg-light库中的一个bug。我在 github 存储库中打开了一个问题。

我更新的代码:

import Graphics.Gloss
import Codec.FFmpeg
import Codec.FFmpeg.Juicy
import Codec.Picture
import Control.Applicative
import Data.Maybe
import Graphics.Gloss.Juicy
import Control.Monad
-- import System.IO.Unsafe (unsafePerformIO)-- for debugging purposes

resolution :: (Int,Int)
resolution = (640, 360)

frameCount :: Int
frameCount = 100

main :: IO ()
main = do
    initFFmpeg
    (getFrame, cleanup) <- imageReaderTime "big_buck_bunny.mp4"
    frames <- replicateM frameCount $ nextFrame getFrame
    cleanup
    animate (InWindow "Nice Window" resolution (10,10)) white (frameAt frames)

nextFrame :: IO (Maybe (Image PixelRGB8, Double)) -> IO (Picture, Float)
nextFrame getFrame = mapSnd realToFrac . mapFst fromImageRGB8 . fromJust <$> getFrame

frameAt :: [(Picture, Float)] -> Float -> Picture
frameAt list time = fst . head . dropWhile ((< time) . snd) $ list

mapFst :: (a -> c) -> (a, b) -> (c, b)
mapFst f (a, b) = (f a, b) -- applies f to first element of a 2-tuple

mapSnd :: (b -> c) -> (a, b) -> (a, c)
mapSnd f (a, b) = (a, f b) -- applies f to the second element of a 2-tuple
4

1 回答 1

0

(a) 我认为 void cleanup是多余的并且可以cleanup正常工作,但我喜欢你不是 100% 确定该IO ()值的确切作用。

我看不到读取 FPS 的直接方法,但imageReaderTime会在几秒钟内产生时间戳,这将为您提供一个很好的指标。要传播时间戳,您需要修改:

nextFrame :: IO (Maybe (Image PixelRGB8, Double)) -> IO (Double, Picture)
nextFrame getFrame = fmap fromImageRGB8 . swap . fromJust <$> getFrame

然后你会说:

stampedFrames <- replicateM frameCount $ nextFrame getFrame
let (tstamps, frames) = unzip stampedFrames
let approx_fps = fromIntegral (length tstamps) / (maximum tstamps - minimum tstamps)

最后,您可以将approx_fps其作为参数传递给frameAt,它必须使用Double而不是使用Float某些类型强制函数。

但是,对于您正在做的事情,最好的方法是:

frameAt :: [(Double, Picture)] -> Double -> Picture
frameAt list time = snd . head . dropWhile ((< time) . fst) $ list

这将获取列表,删除第一个元素(时间戳)小于请求时间的所有元素,然后返回之后出现的第一对的第二个元素(图片)。无需猜测 FPS。

于 2015-12-18T20:38:18.717 回答