How to use Golang and FFmpeg to extract video frames
Abstract:
This article introduces how to use Golang and FFmpeg to extract video frames, and gives specific code examples. Through this method, each frame can be easily extracted from the video and subsequently processed and analyzed.
The following is a sample code that shows how to use Golang to call the FFmpeg command to extract video frames:
package main import ( "log" "os" "os/exec" ) func main() { inputFile := "input.mp4" // 输入视频文件 outputDir := "frames/" // 输出目录,存放提取出的视频帧 // 创建输出目录 err := os.MkdirAll(outputDir, os.ModePerm) if err != nil { log.Fatal(err) } // 构造FFmpeg命令 cmd := exec.Command("ffmpeg", "-i", inputFile, "-vf", "fps=1", outputDir+"frame-%03d.jpg") // 执行命令 err = cmd.Run() if err != nil { log.Fatal(err) } log.Println("视频帧提取完成!") }
The function of the above code is to put the specified video file (input.mp4) into Each frame is extracted and saved as a jpg format image file, stored in the frames/ directory. Among them, the "-vf fps=1" parameter means extracting one frame per second.
go run filename.go
on the command line to start extracting video frames. The extraction process may take some time, depending on the size and frame rate of the video. The above is the detailed content of How to extract video frames using Golang and FFmpeg. For more information, please follow other related articles on the PHP Chinese website!