sc is a tool to take a screencapture every x seconds https://godoc.org/git.zxq.co/howl/sc

main.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // sc is an utility to take screenshots with spectacle
  2. // (https://github.com/KDE/spectacle) once every a determinate timespan.
  3. // It was was made with the intention to be used for "speedcodings", as in
  4. // "speedpaintings" of code. (speedpaintings: https://www.youtube.com/results?search_query=speedpainting)
  5. // To use sc, simply go get this repository, and assuming you have your GOBIN
  6. // in your PATH, starting it is easy:
  7. //
  8. // $ sc
  9. //
  10. // Want to have some options? Ask no more!
  11. //
  12. // $ sc -h
  13. // Usage of ./sc:
  14. // -curnum int
  15. // The number from which to start counting when taking screenshots. If this is 0 (default) and -d is not passed, currentNumber will be automatically set to the with the highest number matching the format %010.png in the current directory.
  16. // -d Enable to not detect current number from folder.
  17. // -ms int
  18. // The number of milliseconds every which to take a screenshot (overrides -s if set).
  19. // -s int
  20. // The number of seconds every which to take a screenshot. (default 10)
  21. //
  22. // That's pretty much all you need to know!
  23. //
  24. // Making it into a video
  25. //
  26. // A pretty good guide to make a video from an image sequence can be found here:
  27. // https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence
  28. //
  29. // Ideally, to make a video you'd use the -c option, so that you can easily
  30. // then make it to a video with this:
  31. //
  32. // $ ffmpeg -i %010d.png video.webm
  33. package main
  34. import (
  35. "flag"
  36. "fmt"
  37. "io/ioutil"
  38. "os"
  39. "sync/atomic"
  40. "time"
  41. "github.com/fatih/color"
  42. )
  43. const version = "0.2.1"
  44. var (
  45. seconds int
  46. milliseconds int
  47. currentNumber int64
  48. dontDetectCurnum bool
  49. )
  50. func init() {
  51. flag.IntVar(&seconds, "s", 10, "The number of seconds every which to take a screenshot.")
  52. flag.IntVar(&milliseconds, "ms", 0, "The number of milliseconds every which to take a screenshot (overrides -s if set).")
  53. flag.Int64Var(&currentNumber, "curnum", 0, "The number from which to start counting when taking screenshots. If this is 0 (default) and -d is not passed, currentNumber will be automatically set to the with the highest number matching the format %010.png in the current directory.")
  54. flag.BoolVar(&dontDetectCurnum, "d", false, "Enable to not detect current number from folder.")
  55. flag.Parse()
  56. }
  57. func main() {
  58. color.Cyan("sc %s", version)
  59. if currentNumber == 0 && !dontDetectCurnum {
  60. err := detectCurnum()
  61. if err != nil {
  62. color.Red("Error occurred while trying to detect currentNumber from directory: %v", err)
  63. color.Yellow("Assuming currentNumber = 0")
  64. currentNumber = 0
  65. }
  66. currentNumber--
  67. }
  68. var ticker *time.Ticker
  69. if milliseconds != 0 {
  70. ticker = time.NewTicker(time.Millisecond * time.Duration(milliseconds))
  71. } else {
  72. ticker = time.NewTicker(time.Second * time.Duration(seconds))
  73. }
  74. go task()
  75. for _ = range ticker.C {
  76. go task()
  77. }
  78. }
  79. func task() {
  80. name := fmt.Sprintf("%010d.png", atomic.AddInt64(&currentNumber, 1))
  81. fmt.Print(color.YellowString("Saving screenshot %s... ", name))
  82. err := takeScreenshot(name)
  83. if err != nil {
  84. fmt.Println()
  85. fmt.Fprintf(os.Stderr, "%v\n", err)
  86. } else {
  87. color.Green("done!")
  88. }
  89. }
  90. func detectCurnum() error {
  91. files, err := ioutil.ReadDir(".")
  92. if err != nil {
  93. return err
  94. }
  95. for _, f := range files {
  96. var num int64
  97. _, err := fmt.Sscanf(f.Name(), "%010d.png", &num)
  98. if err == nil && num > currentNumber {
  99. currentNumber = num
  100. }
  101. }
  102. if currentNumber != 0 {
  103. currentNumber++
  104. }
  105. color.Yellow("currentNumber detected from directory: %d", currentNumber)
  106. return nil
  107. }