tv.go (836B)
1 package main 2 3 import ( 4 "os" 5 "log" 6 "time" 7 "net/http" 8 "github.com/kbinani/screenshot" 9 "github.com/pixiv/go-libjpeg/jpeg" 10 ) 11 12 func snap() { 13 14 for { 15 16 img, err := screenshot.CaptureDisplay(0) 17 if err != nil { 18 panic(err) 19 } 20 21 fileName := "www/tv.jpg.tmp" 22 file, _ := os.Create(fileName) 23 defer file.Close() 24 25 jpeg.Encode(file, img, &jpeg.EncoderOptions{ 26 Quality: 50, 27 OptimizeCoding: false, 28 ProgressiveMode: false, 29 DCTMethod: jpeg.DCTIFast}) 30 31 os.Rename(fileName, "www/tv.jpg") 32 33 log.Println("screenshot taken") 34 35 time.Sleep(1000 * time.Millisecond) 36 37 } 38 } 39 40 func main() { 41 42 go snap() 43 44 log.Println("we're live!") 45 log.Println("ctrl-c for emergency shutdown") 46 47 http.Handle("/", http.FileServer(http.Dir("www"))) 48 http.ListenAndServe(":8888", nil) 49 50 } 51