-
Notifications
You must be signed in to change notification settings - Fork 29
/
Transcode.hs
55 lines (49 loc) · 1.62 KB
/
Transcode.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Main where
import System.Environment (getArgs)
import qualified Codec.FFmpeg as FF
import qualified Codec.FFmpeg.Encode as FF
import Codec.Picture
type Frame = Image PixelRGB8
usage :: String
usage =
unlines [ "Usage: transcode inputFile outputFile [outputFormat] [outputWidth] [outputHeight]"
, " Example: transcode rtmp://localhost/app/one rtmp://localhost/app/two flv 640 480"
, ""
, " Copies the content from inputFile to outputFile using H264."
, " Defaults:"
, " outputFormat=flv"
, " outputWidth=640"
, " outputHeight=480"
]
main :: IO ()
main = do
args <- getArgs
FF.initFFmpeg
FF.setLogLevel FF.avLogDebug
case args of
[from, to] -> copy from to "flv" 640 480
[from, to, format, w, h] -> copy from to format (read w) (read h)
_ -> error usage
copy :: FilePath -> FilePath -> String -> Int -> Int -> IO ()
copy from to format w h = do
let ep = (FF.defaultH264 (fromIntegral w) (fromIntegral h))
-- { FF.epFormatName = Just format }
-- TODO: get this working again
(getFrame, cleanup) <- FF.imageReader (FF.File from)
putFrame <- FF.imageWriter ep to
loop getFrame cleanup putFrame (\x -> return x)
loop :: IO (Maybe Frame)
-> IO cleanup
-> (Maybe Frame -> IO ())
-> (Frame -> IO Frame)
-> IO cleanup
loop getFrame finishReading putFrame editFrame = do
maybeFrame <- getFrame
case maybeFrame of
Nothing -> do
putFrame Nothing
finishReading
Just x -> do
x' <- editFrame x
putFrame (Just x')
loop getFrame finishReading putFrame editFrame