问题背景:h265格式的视频转码之后ios存在兼容问题
原转码命令:
/usr/local/bin/ffmpeg -y -i /data/tmp/1672385812000-tks3wa.mp4 -vf scale=720:1281 -r 30 /data/tmp/1672385812000-tks3wa_720.mp4 -hide_banner
解决办法:
// 原视频格式为hvc,ios只支持hvc,经过scale转换分辨率后视频格式为avc,ios不支持avc,-vcode libx265将视频转为hev,再将hev转为hvc(avc和hvc不能直接转换)
//ffmpeg -y -i /Users/xiaoyq/Downloads/1672817751000-nxxr5f.mp4 -vcodec libx265 -vf scale=360:720 -r 30 /Users/xiaoyq/Downloads/1672817751000-nxxr5f_720.mp4 -hide_banner
//ffmpeg -i '/Users/xiaoyq/Downloads/1672817751000-nxxr5f_720.mp4' -c:v copy -tag:v hvc1 -c:a copy 'output-hvc1.mp4'
./ffprobe -v error -select_streams v -show_entries stream=codec_tag_string -of default=noprint_wrappers=1:nokey=1 ./1672817751000-nxxr5f.mp4
查看视频格式
h264的用原来的处理流程;
新增一个h265的处理流程
代码:
func (client *Client) Transfer() (string, error) {
global.Log.Info("client: ", client)
if client.height%2 != 0 {
client.height += 1
}
height := client.codeRate * client.height / client.width
rateCmd := joinRateCmd(client.codeRate, height)
targetName := parseTargetName(client.tempPath, client.fileName, client.codeRate)
cmdArgumentProbe := []string{"-v", "error", "-select_streams", "v", "-show_entries", "stream=codec_tag_string", "-of", "default=noprint_wrappers=1:nokey=1", client.fileName}
cmd := exec.Command("ffprobe", cmdArgumentProbe...)
global.Log.Info(cmd)
stout, err := cmd.CombinedOutput()
if strings.TrimSpace(string(stout)) != "avc1" {
// 原视频格式为hvc,ios只支持hvc,经过scale转换分辨率后视频格式为avc,ios不支持avc,-vcode libx265将视频转为hev,再将hev转为hvc(avc和hvc不能直接转换)
cmdArguments := []string{"-y", "-i", client.fileName, "-vcodec", "libx265", "-vf", rateCmd, "-r", "30", targetName, "-hide_banner"}
cmd = exec.Command("ffmpeg", cmdArguments...)
global.Log.Info(cmd)
err = cmd.Run()
targetNewName := parseTargetNameNew(client.tempPath, client.fileName, client.codeRate)
cmdArgumentsNew := []string{"-i", targetName, "-c:v", "copy", "-tag:v", "hvc1", "-c:a", "copy", targetNewName}
cmd = exec.Command("ffmpeg", cmdArgumentsNew...)
global.Log.Info(cmd)
err = cmd.Run()
return targetNewName, err
} else {
cmdArguments := []string{"-y", "-i", client.fileName, "-vf", rateCmd, "-r", "30", targetName, "-hide_banner"}
cmd = exec.Command("ffmpeg", cmdArguments...)
global.Log.Info(cmd)
cmd.Run()
return targetName, err
}
}
一定注意要用数组,字符串拼接无法识别命令!!!
很好的参考资料
阮一峰 很细致明了
ffprobe 视频 查看视频的参数信息。
https://blog.csdn.net/zqx1205/article/details/115375075
https://blog.csdn.net/yinshipin007/article/details/127291422。
这两篇教程讲的很好,我也是看到这个之后逐渐摸索出的解决方法。
https://www.cnblogs.com/faithlocus/p/15559269.html
https://www.bilibili.com/video/BV1EQ4y1S7KP/?spm_id_from=333.788 视频教程