FFmpeg介绍和使用

安装FFmpeg

在Mac上通过brew安装FFmpeg

brew tap homebrew-ffmpeg/ffmpeg
brew options homebrew-ffmpeg/ffmpeg/ffmpeg

brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-chromaprint --with-decklink --with-fdk-aac --with-game-music-emu --with-libbluray --with-libbs2b --with-libcaca --with-libgsm --with-libmodplug --with-librsvg --with-libsoxr --with-libssh --with-libvidstab --with-libvmaf --with-libxml2 --with-opencore-amr --with-openh264 --with-openjpeg --with-openssl --with-openssl@1.1 --with-rav1e --with-rtmpdump --with-rubberband --with-speex --with-srt --with-tesseract --with-two-lame --with-webp --with-xvid --with-zeromq --with-zimg --HEAD

验证是否安装成功

ffmpeg -version

使用FFmpeg

视频截帧

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os/exec"
)

func main() {
	filename := "http://www.xxxxxxx.com/yyyy.mp4"
	width := 640
	height := 360
	cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "10", "-s", fmt.Sprintf("%dx%d", width, height), "-f", "singlejpeg", "-")
	var buffer bytes.Buffer
	cmd.Stdout = &buffer
	if cmd.Run() != nil {
		panic("could not generate frame")
	}

	ioutil.WriteFile("./frame.jpg", []byte(buffer.String()), 0666)
}