1 Star 0 Fork 1

general252/ebml-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
g711a.go 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
LIN 提交于 2023-08-24 13:30 . rename
package demo
import (
"bytes"
"encoding/binary"
"log"
"math"
"os"
"gitee.com/general252/ebml-go/core"
"gitee.com/general252/ebml-go/core/codecs"
"gitee.com/general252/ebml-go/mkv"
"gitee.com/general252/ebml-go/mkvcore"
"gitee.com/general252/ebml-go/webm"
"gitee.com/general252/go-wav"
)
func G711ADemo() {
var inFilename = "output.g711a"
var outFilename = "outG711a.mkv"
fp, err := os.Open(inFilename)
if err != nil {
return
} else {
defer fp.Close()
}
w, err := os.OpenFile(outFilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Println(err)
return
} else {
defer w.Close()
}
// FFmpeg-n4.4/libavformat/matroskadec.c/"A_MS/ACM"(ff_get_wav_header)
wavFormat := wav.WavFormat{
AudioFormat: wav.AudioFormatALaw,
NumChannels: 1,
SampleRate: 8000,
ByteRate: 64000,
BlockAlign: 1,
BitsPerSample: 8,
}
var codecPrivate bytes.Buffer
binary.Write(&codecPrivate, binary.LittleEndian, wavFormat)
ws, err := webm.NewSimpleBlockWriter(w,
[]*webm.TrackEntry{
{
Name: "Audio",
TrackNumber: 1,
TrackUID: 12345,
CodecID: core.AudioCodecMSACM,
TrackType: core.TrackTypeAudio,
CodecPrivate: codecPrivate.Bytes(),
Audio: &webm.Audio{
SamplingFrequency: float64(wavFormat.SampleRate),
Channels: uint64(wavFormat.NumChannels),
},
},
},
mkvcore.WithSeekHead(true),
mkvcore.WithCues(true),
mkvcore.WithEBMLHeader(mkv.DefaultEBMLHeader),
mkvcore.WithSegmentInfo(mkv.DefaultSegmentInfo),
mkvcore.WithMaxKeyframeInterval(1, math.MaxInt16-5000),
)
if err != nil {
log.Println(err)
return
} else {
defer ws[0].Close()
}
audioStream := ws[0]
var timestamp int64
for {
buff := make([]byte, 320)
n, err := fp.Read(buff)
if err != nil {
break
}
_, _ = audioStream.Write(true, timestamp, buff[:n])
timestamp += 40
}
}
func Wav2G711a() {
// g711a(pcma)压缩一半, 8000*2/25=640 (每秒 320 bytes) 2: S16
inFilename := "UA_444.wav"
fp, err := os.Open(inFilename)
if err != nil {
log.Println(err)
return
} else {
defer fp.Close()
}
fout, _ := os.Create("output.g711a")
defer fout.Close()
_ = codecs.EmitWavReader(fp, func(format *wav.WavFormat, data []byte) {
_, _ = fout.Write(data)
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/general252/ebml-go.git
git@gitee.com:general252/ebml-go.git
general252
ebml-go
ebml-go
v0.20.0

搜索帮助