func (rec *Recorder) Init(caster *broker.Broker, folderpath string, framerate int, triggerScript string) {
	os.MkdirAll(folderpath+"raw/", 0700)

	converter := Converter{}
	converter.Framerate = framerate
	converter.TriggerScript = triggerScript
	converter.Init(rec, folderpath)
	if rec.checkFfmpeg() {
		go converter.convertFolder(folderpath)
	}

	extension := ".h264"
	stream := caster.Subscribe()
	defer caster.Unsubscribe(stream)
	numHeaders := 0

	var f *os.File
	var fileName string
	var startTime time.Time

	buf := [][]byte{}
	i := 0
	frameOffset := 0
	startedFile := false
	for {
		x := <-stream

		if rec.RequestedRecord {
			if time.Now().Before(rec.StopTime) {
				if !startedFile {
					fileName = getFilename(fileName)
					f, _ = os.Create(folderpath + "raw/" + fileName + extension)
					startTime = time.Now()
					frameOffset = i
				}

				startedFile = true

				for _, v := range buf {
					_, err := f.Write(v)
					if err != nil {
						log.Println(err)
					}
				}
				buf = buf[:0]
				numHeaders = 0
				i = 0
			} else if startedFile {
				f.Close()
				go func(highlightTime time.Time, startTime time.Time, frameOffset int) {
					converter.CacheItem(fileName, highlightTime.Sub(startTime).Seconds()+float64(frameOffset)/float64(framerate)-.25)
					converter.convertFile(fileName)
				}(rec.HighlightTime, startTime, frameOffset)

				go rec.Maintenance(folderpath)

				startedFile = false
			}
		}

		if x.([]byte)[4] == 39 { // always start with SPS header
			if numHeaders == 2 {
				buf = buf[i:]
				numHeaders = 0
				i = 0
			}
			numHeaders++
		}

		buf = append(buf, x.([]byte))
		i++
	}
}
// Init initializes the raspivid recorder. folderpath must include the trailing slash
// When recording is triggered by (rec.StopTime > now), up to numHeaders Iframes will be
// saved before the trigger
Init initializes the raspivid recorder. folderpath must include the trailing slash
When recording is triggered by (rec.StopTime > now), up to numHeaders Iframes will be
saved before the trigger
// Init initializes the raspivid recorder . folderpath must include the trailing slash // When recording is triggered by ( rec . StopTime > now ) up to numHeaders Iframes will be // saved before the trigger
func (rec *Recorder) Init(caster *broker.Broker, folderpath string, framerate int, triggerScript string) {
	os.MkdirAll(folderpath+"raw/", 0700)
	converter := Converter{}
	converter.Framerate = framerate
	converter.TriggerScript = triggerScript
	converter.Init(rec, folderpath)
	if rec.checkFfmpeg() {
		go converter.convertFolder(folderpath)
	}
	extension := ".h264"
	stream := caster.Subscribe()
	defer caster.Unsubscribe(stream)
	numHeaders := 0
	var f *os.File
	var fileName string
	var startTime time.Time
	buf := [][]byte{}
	i := 0
	frameOffset := 0
	startedFile := false
	for {
		x := <-stream
		if rec.RequestedRecord {
			if time.Now().Before(rec.StopTime) {
				if !startedFile {
					fileName = getFilename(fileName)
					f, _ = os.Create(folderpath + "raw/" + fileName + extension)
					startTime = time.Now()
					frameOffset = i
				}
				startedFile = true
				for _, v := range buf {
					_, err := f.Write(v)
					if err != nil {
						log.Println(err)
					}
				}
				buf = buf[:0]
				numHeaders = 0
				i = 0
			} else if startedFile {
				f.Close()
				go func(highlightTime time.Time, startTime time.Time, frameOffset int) {
					converter.CacheItem(fileName, highlightTime.Sub(startTime).Seconds()+float64(frameOffset)/float64(framerate)-.25)
					converter.convertFile(fileName)
				}(rec.HighlightTime, startTime, frameOffset)
				go rec.Maintenance(folderpath)
				startedFile = false
			}
		}
		if x.([]byte)[4] == 39 { 
			if numHeaders == 2 {
				buf = buf[i:]
				numHeaders = 0
				i = 0
			}
			numHeaders++
		}
		buf = append(buf, x.([]byte))
		i++
	}
}
func ( rec * Recorder ) Init ( caster * broker . Broker , folderpath string , framerate int , triggerScript string ) { os . MkdirAll ( folderpath + " " , 0700 ) converter := Converter { } converter . Framerate = framerate converter . TriggerScript = triggerScript converter . Init ( rec , folderpath ) if rec . checkFfmpeg ( ) { go converter . convertFolder ( folderpath ) } extension := " " stream := caster . Subscribe ( ) defer caster . Unsubscribe ( stream ) numHeaders := 0 var f * os . File var fileName string var startTime time . Time buf := [ ] [ ] byte { } i := 0 frameOffset := 0 startedFile := false for { x := <- stream if rec . RequestedRecord { if time . Now ( ) . Before ( rec . StopTime ) { if ! startedFile { fileName = getFilename ( fileName ) f , _ = os . Create ( folderpath + " " + fileName + extension ) startTime = time . Now ( ) frameOffset = i } startedFile = true for _ , v := range buf { _ , err := f . Write ( v ) if err != nil { log . Println ( err ) } } buf = buf [ : 0 ] numHeaders = 0 i = 0 } else if startedFile { f . Close ( ) go func ( highlightTime time . Time , startTime time . Time , frameOffset int ) { converter . CacheItem ( fileName , highlightTime . Sub ( startTime ) . Seconds ( ) + float64 ( frameOffset ) / float64 ( framerate ) - .25 ) converter . convertFile ( fileName ) } ( rec . HighlightTime , startTime , frameOffset ) go rec . Maintenance ( folderpath ) startedFile = false } } if x . ( [ ] byte ) [ 4 ] == 39 { if numHeaders == 2 { buf = buf [ i : ] numHeaders = 0 i = 0 } numHeaders ++ } buf = append ( buf , x . ( [ ] byte ) ) i ++ } }
Init initializes the raspivid recorder.
Init initializes the raspivid recorder .
// always start with SPS header