鲲鹏移植实践之ffmpeg-4.2.1编译安装及使用
1. 简介
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。
官网:http://ffmpeg.org
2. 环境信息
类别 | 子项 | 版本 | 获取地址(方法) |
华为云 华为云 | 虚拟机 | KC1 | |
OS | EulerOS | 2.0 (SP8) | |
Kernel | 4.19.36 | ||
软件包 | ffmpeg | 4.2.1 | https://ffmpeg.org/releases/ffmpeg-4.2.1.tar.bz2 |
3.编译安装
源码获取
mkdir -p /opt/app cd /opt/app/ wget https://ffmpeg.org/releases/ffmpeg-4.2.1.tar.bz2 |
编译安装
tar -xvf ffmpeg-4.2.1.tar.bz2 cd ffmpeg/ mv ffmpeg-4.2.1 ffmpeg ./configure Make make install |
测试
执行以下命令
./ffmpeg |
结果如下:
4.使用ffmpeg
使用ffmpeg完成对视频切割
编写脚本
vim test.py
把mp4格式的视频截为2分钟一段的小视频
# -*- coding:UTF-8 -*- #!/usr/bin/python import string
import os import time import re import math import sys from optparse import OptionParser
print "Test by zzz start..." parser = OptionParser() parser.add_option("-i", "--input", dest="input",action="store_true",help="input x y for each file by user") parser.add_option("-q", "--quality", dest="q",action="store",help="input xvid q arg",default="24") parser.add_option("-v", "--vcodec", dest="vcodec",action="store",help="input video codec",default="x264") parser.add_option("-n", "--noaudio", dest="an",action="store_true",help="no audio") parser.add_option("-f", "--fileType", dest="fileType",action="store",help="",default="mp4") parser.add_option("-o", "--ogg", dest="ogg",action="store_true",help="user ogg instead of aac",default="") parser.add_option("-3", "--mp3", dest="mp3",action="store_true",help="user mp3 instead of aac",default="") #源文件 parser.add_option("-s", "--src", dest="srcD",action="store",help="source dir",default="/opt/test/videoin") #生产文件 parser.add_option("-t", "--target", dest="targetD",action="store",help="target dir",default="/opt/test/videoout") parser.add_option("-w", "--workdir", dest="workdir",action="store",help="work dir",default="/opt/test/video") parser.add_option("-e", "--split", dest="split",action="store_true",help="split to multiple file with size") parser.add_option("-d", "--splitsize", dest="splitsize",action="store",help="split to multiple file with size",default="2")#Minutes parser.add_option("-j", "--prefix", dest="prefix",action="store",help="target file name prefix",default="")
(options, args) = parser.parse_args()
if options.srcD==None or options.srcD[0:1]=='-': print 'srcD Err, quit' exit() if options.targetD==None or options.targetD[0:1]=='-': print 'targetD Err, quit' exit() if options.fileType==None or options.fileType[0:1]=='-': print 'workdir Err, quit' exit() for name in files: name= name.replace('[','''\[''') newname =name[0: name.rindex('.')] print "Test newname: " + newname print "Test name: " + name cmd ='cd '+options.workdir+'; mkdir -p ffm; rm -f ffm/ffm.txt ; sh -c "(/opt/app/ffmpeg/ffmpeg -i '+options.srcD+'/' +name+ ' >& ffm/ffm.txt)"; grep Duration ffm/ffm.txt' print cmd (si, so, se) = os.popen3(cmd) t=so.readlines() print t reg='''Duration\:\s(\d+)\:(\d+)\:([\d\.]+)''' duration=0#second for line in t: result = re.compile(reg).findall(line) for c in result: print 'split file to',options.splitsize,'minutes, Duration:',c[0],c[1],c[2] duration = int(c[0])*3600 + int(c[1])*60+float(c[2]) nameLength=int(math.log(int(duration / (int(options.splitsize)*60)) )/math.log(10)) + 1 print nameLength for i in range(int(duration / (int(options.splitsize)*60)) + 1): print i _t = '' if duration>int(options.splitsize)*60*(i+1): _t = str(int(options.splitsize)*60) else: _t = str(duration-int(options.splitsize)*60*i) cmd ='sh -c "' + "cd "+options.workdir+";touch ffm/output.log;(/opt/app/ffmpeg/ffmpeg -y -i "+options.srcD+"/"+name+" -codec: copy -ss "+str(i*int(options.splitsize)*60)+" -t "+_t+" "+options.targetD+"/"+options.prefix+newname+'_'+string.replace(('%'+str(nameLength)+'s')%str(i),' ','0')+"."+options.fileType + ' >& ffm/output.log)"' print cmd (si, so, se) = os.popen3(cmd) for line in se.readlines() :#???? print line |
执行脚本完成切割
python test.py
切割结果
- 点赞
- 收藏
- 关注作者
评论(0)