音视频编码转换-ffmpeg
1.前言
由于最近在做ios与android端需要做amr至mp3的转换,在网上搜了下资料,基本都是用ffmpeg做音视频类转换
2.ffmpeg下载安装
2.1、下载ffmpeg。
下载网址:http://www.ffmpeg.org/download.html
2.2、解压缩
tar -zxvf ffmpeg-2.0.1.tar.gz
2.3、编辑profile文件:
vi/etc/profile
在文件末尾加上两句话:
export FFMPEG_HOME=/usr/local/ffmpeg export PATH=$FFMPEG_HOME/bin:$PATH
2.4、配置安装路径之类的:
./configure --enable-shared --prefix=/usr/local/ffmpeg --enable-shared 参数据说是允许其编译产生动态库,在以后的编程中要用到这个几个动态库,我也没考证,就直接用了。 如果出现异常,提示因为缺少yasm,需要添加参数,再执行以下命令: ./configure --enable-shared --disable-yasm --prefix=/usr/local/ffmpeg 如果执行结果不对,可以根据提示信息,并查看帮助,解决问题 ./configure --help
2.5、编译安装
make make install
2.6、安装之后在/usr/local/ffmpeg会看到有三个目录
bin 执行文件目录 lib 静态,动态链接库目录 include 编程用到的头文件
2.7、为了防止执行程序找不到库文件,
可以将/usr/local/ffmpeg/lib目录设置到LD_LIBRARY_PATH环境变量
8、若出现errorwhileloadingsharedlibraries:libavdevice.so.52的错误
修改/etc/ld.so.conf 在最后一行加上/usr/local/ffmpeg/lib ldconfig -v 并修改 /usr/local/ffmpeg/lib目录下的文件权限为777
3.shell脚本命令转换
./ffmpeg -i source.amr dest.mp3
4.ffmpeg自带转换
/** * useFfmpegToMp3 * * @param ffmpeg * @param source * @param target */ public static void useFfmpegToMp3(String ffmpeg, String source, String target){ Runtime run = null; try { long start=System.currentTimeMillis(); run = Runtime.getRuntime(); //设置文件运行权限 run.exec(new String[] { "/bin/chmod", "755", ffmpeg}); String runCmd = ffmpeg + " -i " + source + " " + target; //运行命令 Process proc = run.exec(runCmd); proc.getOutputStream().flush(); proc.getOutputStream().close(); proc.getInputStream().close(); LOGGER.info("Dest File Size:{}", new File(target).length()); long end=System.currentTimeMillis(); LOGGER.info("useFfmpegToMp3 Complete:{} -> {}, cost ms:{}", source, target, end-start); } catch (Exception e) { LOGGER.error("useJaveToMp3 Exception: ", e); } finally{ //run调用lame解码器最后释放内存 run.freeMemory(); } }
5.Jave转换
下载地址:http://www.sauronsoftware.it/projects/jave/download.php
/** * jave 转换mp3 * * @param source * @param target */ public static void useJaveToMp3(String source, String target) { try { long start=System.currentTimeMillis(); File sourceFile = new File(source); File targetFile = new File(target); AudioAttributes audio = new AudioAttributes(); Encoder encoder = new Encoder(); audio.setCodec("libmp3lame"); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp3"); attrs.setAudioAttributes(audio); encoder.encode(sourceFile, targetFile, attrs); LOGGER.info("Dest File Size:{}", targetFile.length()); long end=System.currentTimeMillis(); LOGGER.info("useJaveToMp3 Complete:{} -> {}, cost ms:{}", source, target, end-start); }catch (Exceptio){ LOGGER.error("useJaveToMp3 Exception: ", e); } }
备注:
如果自带的FFmpeg转码报错,编译最新的ffmpeg,执行就可以了
官方示例:
//From a generic AVI to a youtube-like FLV movie, with an embedded MP3 audio stream: File source = new File("source.avi"); File target = new File("target.flv"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(64000)); audio.setChannels(new Integer(1)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec("flv"); video.setBitRate(new Integer(160000)); video.setFrameRate(new Integer(15)); video.setSize(new VideoSize(400, 300)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("flv"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); //Next lines extracts audio informations from an AVI and store them in a plain WAV file: File source = new File("source.avi"); File target = new File("target.wav"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("pcm_s16le"); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("wav"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); // Next example takes an audio WAV file and generates a 128 kbit/s, stereo, 44100 Hz MP3 file: File source = new File("source.wav"); File target = new File("target.mp3"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(128000)); audio.setChannels(new Integer(2)); audio.setSamplingRate(new Integer(44100)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp3"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); // Next one decodes a generic AVI file and creates another one with the same video stream of the source and a re-encoded low quality MP3 audio stream: File source = new File("source.avi"); File target = new File("target.avi"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(56000)); audio.setChannels(new Integer(1)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec(VideoAttributes.DIRECT_STREAM_COPY); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("avi"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); //Next one generates an AVI with MPEG 4/DivX video and OGG Vorbis audio: File source = new File("source.avi"); File target = new File("target.avi"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libvorbis"); VideoAttributes video = new VideoAttributes(); video.setCodec("mpeg4"); video.setTag("DIVX"); video.setBitRate(new Integer(160000)); video.setFrameRate(new Integer(30)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mpegvideo"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); // A smartphone suitable video: File source = new File("source.avi"); File target = new File("target.3gp"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libfaac"); audio.setBitRate(new Integer(128000)); audio.setSamplingRate(new Integer(44100)); audio.setChannels(new Integer(2)); VideoAttributes video = new VideoAttributes(); video.setCodec("mpeg4"); video.setBitRate(new Integer(160000)); video.setFrameRate(new Integer(15)); video.setSize(new VideoSize(176, 144)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("3gp"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs);
相关推荐
cherayliu 2020-05-11
ShoppingChen 2020-05-29
PGzxc 2020-03-27
chancle 2020-02-22
ShoppingChen 2020-02-22
ShoppingChen 2020-02-19
83096129 2020-02-15
cherayliu 2020-02-14
83096129 2020-01-08
PGzxc 2019-12-30
yuuuuy 2019-12-30
dingwun 2020-11-16
wangdaren 2020-08-15
wqiaofujiang 2020-07-05
PGzxc 2020-07-04
ShoppingChen 2020-06-25
cherayliu 2020-06-17