No title

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
-- TODO
-- <geekosaur> I would also note you're still doing questionable things;
--     reading from the handles should be done in separate threads, and
--     you should waitForProcess in the main thread.

import System.IO (IOMode ( ReadMode), Handle, hPutStrLn, stderr, stdout, hClose, openFile)
import System.Process
import System.IO.Temp
import System.Directory
import System.Exit (ExitCode(..))

main :: IO ()
main = do
  videoh <- openFile "DSCF2022.AVI" ReadMode
  withSystemTempDirectory "stills" $ \tmpDir -> do
    genStills videoh tmpDir
    let dir <- return tmpDir
    getDirectoryContents dir >>= print

genStills :: Handle -> FilePath -> IO ()
genStills videoh tmpDir = do
    let videoStdin = UseHandle videoh
    let fullPath = tmpDir ++ "/DSCF2022.AVI-%03d.jpg"
    (_, Just hout, Just herr, pHandle) <-
      createProcess (proc "ffmpeg" ["-i","-","-r","3","-f","image2", fullPath]) {std_in = videoStdin, std_out = CreatePipe, std_err = CreatePipe}
    exit <- waitForProcess pHandle
    hClose videoh
    case exit of
      ExitSuccess -> do
        isDirectory <- doesDirectoryExist tmpDir
        hPutStrLn stdout $ "successfully wrote video stills to "  ++ tmpDir
      ExitFailure code -> do
        hPutStrLn stderr $ "video2stills script exited with non-zero exit code: " ++ show code