java利用ffmpeg把图片转成yuv格式
安装ffmpeg:
安装目录:/usr/share/ffmpeg
创建ffmpeg目录,解压ffmpeg-3.4.1.tar.bz2
tar -xjvf ffmpeg-3.4.1.tar.bz2
下载yasm-1.3.0.tar.gz 解压
tar -xvzf yasm-1.3.0.tar.gz
cd yasm-1.3.0/
./configure
make
make install
编译参数都是默认的,直接安装到系统中即可,安装成功之后继续回到ffmpeg解压后的目录,执行下面命令编译并安装:
./configure --enable-shared --prefix=/usr/share/ffmpeg/ffmpeg
make
make install
执行命令: vim /etc/ld.so.conf.d/ffmpeg.conf 然后添加一行内容: /usr/share/ffmpeg/ffmpeg/lib 之后保存并退出,然后执行 ldconfig 使配置生效,执行 ./ffmpeg -version 显示就正常了。
Java代码:
public class Image2YuvUtils {
public static void exec(String filePath,String fileName,int imgWidth,int imgHeight,String targetPath) {
String name=getYuvName(fileName);
List<String> commend = new ArrayList<String>();
commend.add("/usr/share/ffmpeg/ffmpeg/bin/./ffmpeg");
commend.add("-i");
commend.add(filePath);
commend.add(" -s ");
commend.add(imgWidth+"x"+imgHeight);
commend.add(" -pix_fmt yuv420p ");
commend.add(targetPath+"/"+name+".yuv");
StringBuffer test=new StringBuffer();
for(int i=0;i<commend.size();i++)
test.append(commend.get(i)+" ");
Log.debug("test----"+test.toString());
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(test.toString());
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null);
} catch (IOException e) {
Log.error(e.getMessage(),e);
}
Log.debug("转换成功");
}
public static String getYuvName(String fileName){
if(null==fileName||fileName.length()==0){
UUID uuid = UUID.randomUUID();
return uuid.toString();
}else{
StringBuilder sb = new StringBuilder(fileName).reverse();
return new StringBuilder(sb.substring(sb.indexOf(".")+1,sb.length())).reverse().toString();
}
}
}