Skip to content

Commit

Permalink
添加vp9
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyiming748 committed Apr 7, 2024
1 parent a93f272 commit a902757
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
5 changes: 3 additions & 2 deletions conf.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[main]
# mission = i&v
#mission = video
mission = vp9
# mission = OGG
# mission = audio
# mission = image
mission = rotate
# mission = rotate
# mission = resize
# mission = avmerger
# mission = extractAAC
Expand Down Expand Up @@ -33,7 +34,7 @@ txt = txt
folder = /Users/zen/git
image = /Users/zen/Downloads/图片助手(ImageAssistant)_批量图片下载器/telegra.ph
audio = /mnt/e/Virtual Machines
video = /mnt/d/git/AVmerger/.telegram
video = /mnt/d/video/conv/swimsuit/h264
gif = /Users/zen/Downloads/Fugtrup collection/archive/webp
txt = /Users/zen/Downloads/tele
bilibili = /mnt/d/git/processAll/merge/downloads
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ func main() {
threads = util.GetVal("thread", "threads")
slog.Debug("开始视频处理进程", slog.String("根目录", root), slog.String("pattern", pattern), slog.String("进程数", threads))
processVideo.ProcessAllVideos2H265(root, pattern, threads)
case "vp9":
pattern = util.GetVal("pattern", "video")
pattern = strings.Join([]string{pattern, strings.ToUpper(pattern)}, ";")
root = util.GetVal("root", "video")
threads = util.GetVal("thread", "threads")
slog.Debug("开始视频处理进程", slog.String("根目录", root), slog.String("pattern", pattern), slog.String("进程数", threads))
processVideo.ProcessAllVideos2VP9(root, pattern, threads)
case "audio":
pattern = util.GetVal("pattern", "audio")
pattern = strings.Join([]string{pattern, strings.ToUpper(pattern)}, ";")
Expand Down
99 changes: 99 additions & 0 deletions processVideo/vp9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package processVideo

import (
"fmt"
"log/slog"
"os"
"os/exec"
"processAll/GetAllFolder"
"processAll/GetFileInfo"
"processAll/mediaInfo"
"processAll/replace"
"processAll/util"
"runtime"
"strings"
)

func ProcessAllVideos2VP9(root, pattern, threads string) {
folders := GetAllFolder.List(root)
folders = append(folders, root)
for i, folder := range folders {
slog.Debug(fmt.Sprintf("获取全部子文件夹,正在处理第个 %d/%d 文件夹", i+1, len(folders)))
ProcessVideos2VP9(folder, pattern, threads)
runtime.GC()
}
}

func ProcessVideos2VP9(dir, pattern, threads string) {
infos := GetFileInfo.GetAllFileInfo(dir, pattern)
for i, info := range infos {
slog.Debug(fmt.Sprintf("获取全部文件,正在处理第个 %d/%d 文件", i+1, len(dir)))
ProcessVideo2VP9(info, threads)
runtime.GC()
}
}

func ProcessVideo2VP9(in GetFileInfo.BasicInfo, threads string) {
defer func() {
if err := recover(); err != nil {
slog.Error("处理视频失败", slog.Any("错误", err))
} else {
slog.Info("处理视频成功", slog.String("文件名", in.FullPath))
//if err = os.RemoveAll(in.FullPath); err != nil {
// slog.Warn("删除失败", slog.Any("源文件", in.FullPath), slog.Any("错误", err))
//} else {
// slog.Debug("删除成功", slog.Any("源文件", in.FullName))
//}
}
}()
mi, ok := in.MediaInfo.(mediaInfo.VideoInfo)
if ok {
slog.Debug("断言视频mediainfo结构体成功", slog.Any("MediainfoVideo结构体", mi))
} else {
slog.Warn("断言视频mediainfo结构体失败")
}
slog.Info("获取帧数", slog.Int("当前视频帧数", mi.VideoFrameCount))
//slog.Debug("文件信息", slog.Any("info", in))
if strings.Contains(in.FullPath, "h265") {
slog.Debug("跳过当前已经在h265目录中的文件", slog.String("文件名", in.FullPath))
return
}
prefix := in.PurgePath // 输入文件的纯路径
slog.Debug("perfix", slog.String("perfix", prefix))

slog.Debug("fullname", slog.String("fullname", in.FullName))
middle := "VP9"
if err := os.Mkdir(strings.Join([]string{prefix, middle}, string(os.PathSeparator)), 0777); err != nil {
if strings.Contains(err.Error(), "file exists") {
slog.Debug("输出文件夹已存在")
}
} else {
slog.Debug("创建输出文件夹")
}
dstPurgeName := replace.ForFileName(in.PurgeName) // 输入文件格式化后的新文件名
out := strings.Join([]string{in.PurgePath, middle, string(os.PathSeparator), dstPurgeName, ".mp4"}, "")
defer func() {
if err := recover(); err != nil {
slog.Warn("出现错误", slog.String("输入文件", in.FullPath), slog.String("输出文件", out))
}
}()
slog.Debug("", slog.String("out", out), slog.String("extName", in.PurgeExt))
mp4 := strings.Replace(out, in.PurgeExt, "mp4", -1)
slog.Debug("调试", slog.String("输入文件", in.FullPath), slog.String("输出文件", mp4))
cmd := exec.Command("ffmpeg", "-threads", threads, "-i", in.FullPath, "-c:v", "libvpx-vp9", "-crf", "31", "-c:a", "libvorbis", "-ac", "1", "-map_chapters", "-1", "-threads", threads, mp4)
if mi.VideoWidth > 1920 && mi.VideoHeight > 1920 {
slog.Warn("视频大于1080P需要使用其他程序先处理视频尺寸", slog.Any("原视频", in))
ResizeVideo(in, threads)
return
}
slog.Info("生成的命令", slog.String("command", fmt.Sprint(cmd)))
slog.Info("视频信息", slog.Int("视频帧数", mi.VideoFrameCount), slog.String("比特率", mi.VideoBitRate))
util.ExecCommand(cmd)
slog.Debug("视频编码运行完成")
if s_size, d_size, diff, err := util.GetDiffFileSize(in.FullPath, mp4); err != nil {
slog.Warn("文件优化大小计算出错")
} else {
fmt.Println(s_size, d_size, diff)
}
slog.Debug("本次转码完成")
}

0 comments on commit a902757

Please sign in to comment.