最近公司项目测试阶段需要测试数据源,主要是将一个视频文件(标准H264)通过修改分辨率,添加水印,设置自定义GOP,设置profile格式为baseline,将视频中的B帧去除掉,只留下I帧和P帧,最后将文件中的ES流提取出来生成三千个不同的文件。我主要是将文件按照指定的IP区间添加不同的水印,文件名称按照IP区间来定义
命令行:
修改视屏分辨率:
ffmpeg -i input -vf scale=iw/2:-2 output
视屏添加字幕:(srt文件方式)
ffmpeg.exe -i test.avi -vf subtitles=test.srt out.avi
添加文字水印
ffmpeg.exe -i h264Test.mp4 -vf "drawtext=fontfile=arial.ttf:text='27.8-SD':x=1:y=1:fontsize=50:fontcolor=yellow:shadowy=2" drawtext.mp4
设置baseline:
ffmpeg.exe -i 1231.mp4 -vcodec libx264 -x264opts keyint=10:min-keyint=10 -an -profile:v baseline -level 3.0 WLL.mp4
提取ES流
ffmpeg.exe -i WLL.mp4 -vcodec copy -f rawvideo -vbsf h264_mp4toannexb final.raw
代码实现:
CString strDestPath, tmpPath, DestFileName("\\Check_");
int pos = strSrcFile.ReverseFind('\\');
strDestPath = strSrcFile.Mid(0, pos);
tmpPath = strSrcFile.Mid(0, pos);
DestFileName += strSrcFile.Mid(pos+1);
strDestPath += DestFileName;
CString Suffix;
Suffix = DestFileName.Mid(DestFileName.Find("."));
//修改分辨率大小为320*240
param = " -i " + strSrcFile + " -vf scale=320:240 -t 60 " + strDestPath;
ShellExe(param);
CString temp,Mark,base;
temp.Format("%d%s", i, Suffix);
if (Sufix.IsEmpty())
Mark.Format("%s.%d", basenum, i);
else
Mark.Format("%s.%d_%s", basenum, i, Sufix);
basenum += temp;
//添加水印
param = " -i "+ strDestPath + " -vf \"drawtext=fontfile=arial.ttf:text = '" + Mark +"':x=1:y=1:fontsize=50:fontcolor=yellow:shadowy=2\" " + tmpPath + "\\" + basenum;
ShellExe(param);
//将视频转换成baseline模式,并且将GOP设置为10
CString baseline = "baseline_";
baseline += basenum;
param = " -i " + tmpPath + "\\" + basenum + " -vcodec libx264 -x264opts keyint=10:min-keyint=10 -an -profile:v baseline -level 3.0 " + tmpPath + "\\" + baseline;
ShellExe(param);
//提取ES流
CString fileName = basenum.Left(basenum.Find("."));
if (Sufix.IsEmpty())
{
fileName += ".raw";
}
else
{
fileName += "-";
fileName += Sufix;
fileName += ".raw";
}
param = " -i "+ tmpPath + "\\" + baseline + " -vcodec copy -f rawvideo -vbsf h264_mp4toannexb " + tmpPath + "\\" + fileName;
ShellExe(param);