goffmpeg/examples/simple-transcode/main.go

76 lines
1.7 KiB
Go

package main
import (
"fmt"
"log"
"os"
"git.kingecg.top/kingecg/goffmpeg/pkg/ffmpeg"
)
// Simple transcoding example using goffmpeg library
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: simple-transcode <input> <output>")
fmt.Println("Example: simple-transcode input.mp4 output.flv")
os.Exit(1)
}
inputURL := os.Args[1]
outputURL := os.Args[2]
// Open input file
ic := ffmpeg.AllocFormatContext()
defer ic.Free()
if err := ic.OpenInput(inputURL); err != nil {
log.Fatalf("Failed to open input %s: %v", inputURL, err)
}
defer ic.Close()
// Find stream info
if err := ic.FindStreamInfo(); err != nil {
log.Fatalf("Failed to find stream info: %v", err)
}
// Dump input format info
fmt.Printf("Input: %s\n", inputURL)
ic.DumpFormat(0, inputURL, false)
// Find video stream
videoStreams := ic.VideoStreams()
if len(videoStreams) == 0 {
log.Fatal("No video stream found in input")
}
vs := videoStreams[0]
fmt.Printf("Video stream index: %d\n", vs.Index())
// Get codec parameters
cp := vs.CodecParameters()
fmt.Printf("Codec type: %d, Codec ID: %d\n", cp.CodecType(), cp.CodecID())
// Create output context
of := ffmpeg.GuessFormat("", outputURL)
if of == nil {
log.Fatalf("Failed to guess output format")
}
ofc, err := ffmpeg.AllocOutputContext(outputURL, of)
if err != nil {
log.Fatalf("Failed to allocate output context: %v", err)
}
defer ofc.Free()
// Copy stream from input to output
_, err = ofc.AddStream(nil)
if err != nil {
log.Fatalf("Failed to add stream: %v", err)
}
fmt.Printf("\nOutput: %s\n", outputURL)
fmt.Println("Transcoding setup complete. Use the library APIs to process frames.")
_ = vs // vs is used for reference
}