module FFProbe (
    ffprobe,
    FFProbeData (..),
) where

import Data.Aeson (FromJSON, eitherDecodeStrict)
import Data.Text (pack)
import Data.Text.Encoding (encodeUtf8)
import FFProbe.Data.Chapter (Chapter)
import FFProbe.Data.Format (Format)
import FFProbe.Data.Stream (Stream)
import FFProbe.Exec (execFFProbe)
import GHC.Generics (Generic)

data FFProbeData = FFProbeData
    { FFProbeData -> [Stream]
streams :: [Stream],
      FFProbeData -> [Chapter]
chapters :: [Chapter],
      FFProbeData -> Format
format :: Format
    }
    deriving ((forall x. FFProbeData -> Rep FFProbeData x)
-> (forall x. Rep FFProbeData x -> FFProbeData)
-> Generic FFProbeData
forall x. Rep FFProbeData x -> FFProbeData
forall x. FFProbeData -> Rep FFProbeData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FFProbeData -> Rep FFProbeData x
from :: forall x. FFProbeData -> Rep FFProbeData x
$cto :: forall x. Rep FFProbeData x -> FFProbeData
to :: forall x. Rep FFProbeData x -> FFProbeData
Generic)

instance FromJSON FFProbeData

-- | Runs the ffprobe coomands, and parse its output
ffprobe :: String -> IO (Either String FFProbeData)
ffprobe :: String -> IO (Either String FFProbeData)
ffprobe String
path = do
    Either String String
probeRes <- String -> IO (Either String String)
execFFProbe String
path
    Either String FFProbeData -> IO (Either String FFProbeData)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String FFProbeData -> IO (Either String FFProbeData))
-> Either String FFProbeData -> IO (Either String FFProbeData)
forall a b. (a -> b) -> a -> b
$ case Either String String
probeRes of
        Right String
rawJson -> ByteString -> Either String FFProbeData
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict (Text -> ByteString
encodeUtf8 (Text -> ByteString) -> Text -> ByteString
forall a b. (a -> b) -> a -> b
$ String -> Text
pack String
rawJson)
        Left String
err -> String -> Either String FFProbeData
forall a b. a -> Either a b
Left String
err