首先,视频文件直播如下一条命令就可以了
代码语言:txt复制cuiwei@weideMacBook-Pro ~ % ffmpeg -re -i ~/Movies/aaa.mp4 -vcodec copy -acodec aac -b:a 96k -f flv "推流地址"
接下来我们让ChatGPT完善一下,
- 一个目录下有多个视频文件,依次播放每个文件,全部播放完时,从头播放
目录结构
代码语言:txt复制cuiwei@weideMacBook-Pro ffmpeg % ls
202306092337.mp4 202307081641.mp4 main.go
main.go
代码语言:txt复制package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
func isVideoFile(path string) bool {
ext := strings.ToLower(filepath.Ext(path))
return ext == ".mp4" || ext == ".mov" || ext == ".avi" || ext == ".mkv" // 添加更多视频文件扩展名
}
func findVideoFiles(dir string) []string {
var videoFiles []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && isVideoFile(path) {
videoFiles = append(videoFiles, path)
}
return nil
})
if err != nil {
fmt.Println("Error walking directory:", err)
}
return videoFiles
}
func main() {
directoryPath := "/Users/cuiwei/GolandProjects/demo/ffmpeg"
videoFiles := findVideoFiles(directoryPath)
if len(videoFiles) > 0 {
fmt.Println("Video files found:")
//fmt.Println(videoFiles[0])
currentIndex := 0
for {
videoFile := videoFiles[currentIndex]
cmd := exec.Command("ffmpeg", "-re", "-i", videoFile, "-vcodec", "copy", "-acodec", "aac", "-b:a", "96k", "-f", "flv", "推流地址")
// 启动 FFmpeg 进程
err := cmd.Start()
if err != nil {
fmt.Println("Error starting FFmpeg:", err)
return
}
// 等待 FFmpeg 进程结束
err = cmd.Wait()
if err != nil {
fmt.Println("Error waiting for FFmpeg:", err)
}
fmt.Printf("Video %s has finished streaming.n", videoFile)
// 切换到下一个视频
currentIndex
if currentIndex >= len(videoFiles) {
currentIndex = 0 // 从头开始
}
// 添加延迟,以便在切换视频之前等待一段时间
time.Sleep(5 * time.Second) // 5秒延迟,你可以根据需要调整
}
} else {
fmt.Println("No video files found in the directory.")
}
}