Exemplo n.º 1
0
func (self *SimpleH264Writer) prepare() (err error) {
	if self.mdatWriter, err = atom.WriteAtomHeader(self.W, "mdat"); err != nil {
		return
	}

	if len(self.SPS) == 0 {
		err = fmt.Errorf("invalid SPS")
		return
	}

	if len(self.PPS) == 0 {
		err = fmt.Errorf("invalid PPS")
		return
	}

	if self.Width == 0 || self.Height == 0 {
		var info *atom.H264SPSInfo
		if info, err = atom.ParseH264SPS(self.SPS[1:]); err != nil {
			return
		}
		self.Width = int(info.Width)
		self.Height = int(info.Height)
	}

	self.sampleIdx = 1

	self.sample = &atom.SampleTable{
		SampleDesc: &atom.SampleDesc{
			Avc1Desc: &atom.Avc1Desc{
				DataRefIdx:           1,
				HorizontalResolution: 72,
				VorizontalResolution: 72,
				Width:                self.Width,
				Height:               self.Height,
				FrameCount:           1,
				Depth:                24,
				ColorTableId:         -1,
				Conf:                 &atom.Avc1Conf{},
			},
		},
		TimeToSample: &atom.TimeToSample{},
		SampleToChunk: &atom.SampleToChunk{
			Entries: []atom.SampleToChunkEntry{
				{
					FirstChunk:   1,
					SampleDescId: 1,
				},
			},
		},
		SampleSize: &atom.SampleSize{},
		ChunkOffset: &atom.ChunkOffset{
			Entries: []int{8},
		},
		SyncSample: &atom.SyncSample{},
	}
	self.sampleToChunk = &self.sample.SampleToChunk.Entries[0]

	return
}
Exemplo n.º 2
0
Arquivo: test.go Projeto: louisyoo/mp4
func ProbeFile(filename string) (err error) {
	var osfile *os.File
	if osfile, err = os.Open(filename); err != nil {
		return
	}

	var finfo os.FileInfo
	if finfo, err = osfile.Stat(); err != nil {
		return
	}

	dumper := &atom.Dumper{}
	var moov *atom.Movie

	lr := &io.LimitedReader{R: osfile, N: finfo.Size()}
	for lr.N > 0 {
		var ar *io.LimitedReader

		var cc4 string
		if ar, cc4, err = atom.ReadAtomHeader(lr, ""); err != nil {
			log.Println("read atom failed")
			return
		}

		if cc4 == "moov" {
			if moov, err = atom.ReadMovie(ar); err != nil {
				log.Println("read '%s' atom failed", cc4)
				return
			}
			if false {
				atom.WalkMovie(dumper, moov)
			}
		} else {
			fmt.Println("atom:", cc4)
		}

		if _, err = atom.ReadDummy(lr, int(ar.N)); err != nil {
			return
		}
	}

	if moov == nil {
		err = fmt.Errorf("'moov' atom not found")
		log.Println("'moov' atom not found")
		return
	}

	if len(moov.Tracks) > 0 {
		track := moov.Tracks[0]
		record := getAVCDecoderConfRecordByTrack(track)

		if record != nil && len(record.SPS) > 0 {
			sps := record.SPS[0]
			if len(sps) > 1 {
				sps = sps[1:]
				log.Println(hex.Dump(sps))
				var info *atom.H264SPSInfo
				if info, err = atom.ParseH264SPS(sps); err != nil {
					return
				}
				log.Println(info)
			}
		}
	}

	return
}