1 回答

TA貢獻1831條經驗 獲得超10個贊
GO / CGO有一些特質,你在這里碰到:
type OutputFormat C.struct_AVOutputFormat
是 go 類型聲明,而不是別名。將其視為薄包裝而不是別名可能會有所幫助。因此,C.struct_AVOutputFormat字段不會導出,這就是您獲得OutputFormat != C.struct_AVOutputFormat
ERROR: outputF.audio_codec undefined (cannot refer to unexported field or method audio_codec)
如果該字段被調用Audio_codec它將符合go對導出標識符的定義,我們可以訪問它,但事實并非如此。
有一種方法可以解決這個問題,但我建議在繼續之前三思而后行,因為它使用不安全的指針,并且您的程序可能會在運行時失去可移植性和/或穩定性。如果您想了解更多信息,這是不安全指針的良好介紹。
現在,如果您確實確定要執行此操作,解決方案是將指針轉換為不安全的指針,然后將其轉換為指向 的指針。請注意,這需要您加載 FFMPEG 標頭才能獲得OutputFormat
C.struct_AVOutputFormat
C.struct_AVOutputFormat
//#cgo pkg-config: libavformat
//#include <libavformat/avformat.h>
import "C"
import (
"fmt"
"unsafe"
"github.com/giorgisio/goav/avformat"
)
func getOutput() *avformat.OutputFormat {
// calls out to avformat and gets an OutputFormat back
}
func main() {
outputF := getOutput()
coutput := *(*C.struct_AVOutputFormat)(unsafe.Pointer(outputF))
fmt.Println(coutput.audio_codec) // This should now work
}
警告:我還沒有測試cgo包配置和導入是否正確,但這適用于一個簡單的C庫,我站起來嘗試一下。<libavformat/avformat.h>
- 1 回答
- 0 關注
- 133 瀏覽
添加回答
舉報