SHARE
TWEET

Untitled

a guest Aug 14th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "image"
  6.     "io"
  7.     "log"
  8.     "os"
  9.  
  10.     "github.com/nareix/joy4/av/avutil"
  11.     "github.com/nareix/joy4/cgo/ffmpeg"
  12.     "github.com/nareix/joy4/format"
  13. )
  14.  
  15. func init() {
  16.     format.RegisterAll()
  17. }
  18.  
  19. func main() {
  20.     if len(os.Args) < 2 {
  21.         log.Fatalln("Provide a path to a video file to analyze.")
  22.     }
  23.  
  24.     file, err := avutil.Open(os.Args[1])
  25.     if err != nil {
  26.         log.Fatal(err)
  27.     }
  28.  
  29.     streams, _ := file.Streams()
  30.     var vdecoder *ffmpeg.VideoDecoder
  31.     var streamIdx int8
  32.     for idx, stream := range streams {
  33.         if stream.Type().IsVideo() {
  34.             vdecoder, err = ffmpeg.NewVideoDecoder(stream)
  35.             if err != nil {
  36.                 log.Fatal(err)
  37.             }
  38.             streamIdx = int8(idx)
  39.         }
  40.     }
  41.     if vdecoder == nil {
  42.         log.Fatalln("Didn't find a video stream in the file")
  43.     }
  44.  
  45.     pktC := make(chan []byte, 5)
  46.     go func() {
  47.         for {
  48.             if pkt, err := file.ReadPacket(); err != nil {
  49.                 if err == io.EOF {
  50.                     break
  51.                 }
  52.                 log.Fatal(err)
  53.             } else if pkt.Idx == streamIdx {
  54.                 pktC <- pkt.Data
  55.             }
  56.         }
  57.         file.Close()
  58.         close(pktC)
  59.     }()
  60.  
  61.     for pktData := range pktC {
  62.         img, err := vdecoder.Decode(pktData)
  63.         if err != nil {
  64.             log.Fatal(err)
  65.         }
  66.         if img != nil {
  67.             handleFrame(img)
  68.         }
  69.     }
  70.     // TODO: flush out any remaining frames
  71.     // the binding library doesn't support sending zero-byte frames in
  72.     // close(frameC)
  73. }
  74.  
  75. var frameIdx int = 0
  76.  
  77. func handleFrame(frame *ffmpeg.VideoFrame) {
  78.     frameIdx++
  79.     fmt.Printf("{\"frame\":%d,\"hasMarker\":%v}\n", frameIdx, containsSyncMarker(frame.Image))
  80.     frame.Free()
  81. }
  82.  
  83. func containsSyncMarker(img image.YCbCr) bool {
  84.     var width = img.Bounds().Dx()
  85.     var height = img.Bounds().Dy()
  86.     var widthToCheck = int(float32(width) * 0.08)
  87.     var heightToCheck = int(float32(height) * 0.08)
  88.  
  89.     for x := 0; x < widthToCheck; x++ {
  90.         for y := height - heightToCheck; y < height; y++ {
  91.             r, g, b, _ := img.At(x, y).RGBA()
  92.             // log.Println(r>>8, g>>8, b>>8)
  93.             if r>>8 > 10 || g>>8 < 240 || b>>8 > 10 {
  94.                 return false
  95.             }
  96.         }
  97.     }
  98.  
  99.     return true
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top