代码拉取完成,页面将自动刷新
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)
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。