Skip to content

Instantly share code, notes, and snippets.

@thalesmg
Created October 12, 2020 17:59
Show Gist options
  • Save thalesmg/e29960e8fce8dcc78a0a899cf4d4ab89 to your computer and use it in GitHub Desktop.
Save thalesmg/e29960e8fce8dcc78a0a899cf4d4ab89 to your computer and use it in GitHub Desktop.
Conversor de webp animado para mp4
#!/usr/bin/env stack
{-
stack --resolver lts-16.18 script
--package turtle
--package text
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Control.Monad (forM_)
import Data.Functor (($>))
import Data.Maybe (fromMaybe)
import Data.Text (pack)
import qualified Text.Printf as PF
import Turtle as T
convert :: T.FilePath -> Shell ()
convert infile = do
let bfile = basename infile
bdir = dirname infile
Right tinfile = toText infile
[n] :: [Integer] <- fmap (match decimal . lineToText)
$ sed ("Number of frames: " $> "")
$ grep (has "frames")
$ inproc "webpinfo" ["-summary", tinfile] empty
dir <- using (mktempdir "/tmp" "tmg-webp2mp4.XXXXXXXX")
let outfilei i ext = let Right o = toText $ dir </> bfile <.> i <.> ext in o
let outfile ext = let Right o = toText $ dir </> bfile <.> ext in o
forM_ [1..n] $ \i' -> do
let i = pack . PF.printf "%04d" $ i'
procs "webpmux" ["-get", "frame", i, tinfile, "-o", outfilei i "webp"] empty
procs "dwebp" [outfilei i "webp", "-o", outfilei i "png"] empty
let outPattern = let Right o = toText $ dir </> bfile <.> "%04d" <.> "png" in o
procs "ffmpeg" ["-framerate", "16", "-i", outPattern, "-vf", "format=yuv420p", outfile "mp4"] empty
let final = bdir </> bfile <.> "mp4"
cp (fromText (outfile "mp4")) final
parser :: Parser [T.FilePath]
parser = some (argPath "input" "arquivo(s) de entrada")
main :: IO ()
main = do
opts <- options "conversor de webp para mp4" parser
forM_ opts $ sh . convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment