yapingcat / gomedia

golang library for rtmp, mpeg-ts,mpeg-ps,flv,mp4,ogg,rtsp
MIT License
383 stars 66 forks source link

ts转mp4发生了panic,数组越界 #19

Closed orestonce closed 1 year ago

orestonce commented 2 years ago

import ( "fmt" "io/ioutil" "os"

"bytes"
"strconv"

"github.com/yapingcat/gomedia/codec"
"github.com/yapingcat/gomedia/mp4"
"github.com/yapingcat/gomedia/mpeg2"

)

func main() { tsfile := 01389.ts // input mp4filename := "output-gomedia.mp4" // output mp4file, err := os.OpenFile(mp4filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666) if err != nil { fmt.Println(err) return } defer mp4file.Close()

muxer, err := mp4.CreateMp4Muxer(mp4file)
if err != nil {
    panic(err)
}

vtid := muxer.AddVideoTrack(mp4.MP4_CODEC_H264)
atid := muxer.AddAudioTrack(mp4.MP4_CODEC_AAC)

buf, err := ioutil.ReadFile(tsfile)
if err != nil {
    panic(err)
}
var audio_timestamp uint64 = 0
aac_sampleRate := -1
demuxer := mpeg2.NewTSDemuxer()
demuxer.OnFrame = func(cid mpeg2.TS_STREAM_TYPE, frame []byte, pts uint64, dts uint64) {
    if cid == mpeg2.TS_STREAM_AAC {
        codec.SplitAACFrame(frame, func(aac []byte) {
            if aac_sampleRate == -1 {
                adts := codec.NewAdtsFrameHeader()
                adts.Decode(aac)
                aac_sampleRate = codec.AACSampleIdxToSample(int(adts.Fix_Header.Sampling_frequency_index))
                fmt.Println("Got aac sample rate:", aac_sampleRate)
            }
            err = muxer.Write(atid, aac, audio_timestamp, audio_timestamp)
            audio_timestamp += uint64((1024 * 1000 / aac_sampleRate)) //每帧aac采样固定为1024。aac_sampleRate 为采样率
            if err != nil {
                panic(err)
            }
        })
    } else if cid == mpeg2.TS_STREAM_H264 {
        err = muxer.Write(vtid, frame, uint64(pts), uint64(dts))
        if err != nil {
            panic(err)
        }
    } else {
        panic("unkwon cid " + strconv.Itoa(int(cid)))
    }
}
for idx := 0; idx < 4; idx++ {
    fmt.Println("read file ", idx)
    err = demuxer.Input(bytes.NewReader(buf))
    if err != nil {
        panic(err)
    }
}
err = muxer.WriteTrailer()
if err != nil {
    panic(err)
}

}

* ts样例:
[01389.ts.zip](https://github.com/yapingcat/gomedia/files/9158140/01389.ts.zip)
* 报错信息:

read file 0 Got aac sample rate: 44100 panic: runtime error: index out of range [3] with length 3

goroutine 1 [running]: github.com/yapingcat/gomedia/codec.H265NaluType(...) /root/go/pkg/mod/github.com/yapingcat/gomedia@v0.0.0-20220717141418-916ca463aae9/codec/util.go:95 github.com/yapingcat/gomedia/mpeg2.(TSDemuxer).flush.func1({0xc0000c9e17, 0x9dd, 0x0}) /root/go/pkg/mod/github.com/yapingcat/gomedia@v0.0.0-20220717141418-916ca463aae9/mpeg2/ts-demuxer.go:158 +0xa9 github.com/yapingcat/gomedia/codec.SplitFrameWithStartCode({0xc0000c9800, 0xc0001ce006, 0xaa}, 0xc000088b30) /root/go/pkg/mod/github.com/yapingcat/gomedia@v0.0.0-20220717141418-916ca463aae9/codec/util.go:66 +0xbc github.com/yapingcat/gomedia/mpeg2.(TSDemuxer).flush(0xc000088ea0) /root/go/pkg/mod/github.com/yapingcat/gomedia@v0.0.0-20220717141418-916ca463aae9/mpeg2/ts-demuxer.go:152 +0x173 github.com/yapingcat/gomedia/mpeg2.(*TSDemuxer).Input(0xc000088ea0, {0x4e9fa0, 0xc00008e210}) /root/go/pkg/mod/github.com/yapingcat/gomedia@v0.0.0-20220717141418-916ca463aae9/mpeg2/ts-demuxer.go:139 +0x4a5 main.main() /root/work/min/main.go:67 +0x44d

yapingcat commented 2 years ago

不好意思 是上次修改引入的bug, 已经修复,请更新至最新版本测试

orestonce commented 2 years ago

可以了

hasnhasan commented 2 years ago

While saving as mp4 from rtmp at certain intervals, a time problem arises in the mp4 file. I was using -vsync 0 when I recorded using FFMPEG. How should we proceed here?

yapingcat commented 2 years ago

sorry ,could you explain to me time problem in detail ?

xm0625 commented 1 year ago

最新的commit好像又出现了

xm0625 commented 1 year ago

1e3c4f5af3fc4b4eb10b5ce3566e713a.ts.zip

orestonce commented 1 year ago

@hasnhasan Can you provide a minimal example for reproducing your problem ?

xm0625 commented 1 year ago

已经解决了。见issus-> #41

orestonce commented 1 year ago

@xm0625 好的