`

音视频编码转换-ffmpeg

    博客分类:
  • java
阅读更多
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、若出现error while loading shared libraries: 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);
分享到:
评论

相关推荐

    研究论文-FFMPEG的音视频格式转换设计.pdf

    视频格式转换的需求普遍存在,通过FFMPEG这个开源免费的软件...实验结果表明:FFMPEG软件可以很好地实现视频格式转换,并可按需求改变其中的视频编码方式和音频编码方式,可灵活地转换视频格式,并具有很强的可移植性。

    ffmpeg-0.5.rar_FFmp_ffmpeg_ffmpeg 音视频_ffmpeg-0_windows ffmpeg-0

    ffmpeg-0.5编码包,适合于windows下的音视频开发,可以进行视频转换和编解码

    QT+FFmpeg实现音视频格式转换

    利用QT和FFmpeg实现类似于格式工厂的功能,进行简单的音视频处理。

    ffmpeg-4.2.1-win32-dev

    它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Bellard 发起的,现在由 Michael Niedermayer 主持。可以...

    IOS 音视频 ffmpeg 录音并播放

    通过调用FFmpeg库的接口,我们可以实现音频的录制和播放,并能够对音频进行格式转换、编码解码等操作。 开发环境搭建 在开始开发之前,我们需要搭建适合iOS平台的开发环境。我们将使用Xcode作为主要的集成开发环境...

    ffmpeg-4.2-win64-static.zip

    它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Bellard 发起的,现在由 Michael Niedermayer 主持。可以...

    ffmpeg-4.3.1(linux-mac-window).7z

    它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Be【ffmpeg-4.3.1(linux-mac-window)】 1、ffmpeg-4.3.1-...

    Qt 基于FFmpeg的视频播放器 - QtFFmpegPlayer

    FFmpeg支持众多音视频编码格式,如MP3、AAC、AC3、H.264、MPEG-4等。它可以将不同格式的音视频文件转换为其他格式,从而满足不同设备和平台的需求。除了转换格式,FFmpeg还可以进行音视频的剪切、合并、裁剪、旋转等...

    ffmpeg-git-amd64-static.tar.xz

    它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Bellard 发起的,现在由 Michael Niedermayer 主持。可以...

    ffmpeg-20191101-53c21c2-win64-static.zip

    它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。 FFmpeg在Linux平台下开发,但它同样也...

    ffmpeg-4.2.1-win32-shared.zip

    它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Bellard 发起的,现在由 Michael Niedermayer 主持。可以...

    ffmpeg-win64.zip

    ffmpeg库,音视频编解码,FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。它包括了目前领先的音/视频编码库libavcodec。

    FFMPEG 常用命令.pdf

    1. 分离音视频 2. 解复用 3. 视频转码 4. 视频封装 5. 视频剪切 6. 视频录制 7.叠加水印 8.将MP3转换为PCM数据 9. 推送RTP流、接收RTP流并存为ts文件 10. ffmpeg 编码 11. ffmpeg 解码 12. 截取 YUV 13....

    ffmpeg-20200315-win64-static.rar

    它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Bellard 发起的,现在由 Michael Niedermayer 主持。可以...

    ffmpeg2023-12-14版本 windows版

    它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。 FFmpeg在Linux平台下开发,但它同样也...

    win10 64位安装ffmpeg的免安装ZIP包

    它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。 FFmpeg在Linux平台下开发,但它同样也...

    ffmpeg-4.2.3-win32-static.zip

    它包括了目前领先的音/视频编码库libavcodec。 FFmpeg是在 Linux 下开发出来的,但它可以在包括 Windows 在内的大多数操作系统中编译。这个项目是由 Fabrice Bellard 发起的,现在由 Michael Niedermayer 主持。可以...

    Linux服务器安装ffmpeg+libx264+libmp3lame

    ffmpeg是一个很强大的音视频处理工具,官网是:http://ffmpeg.org/ 官网介绍ffmpeg是:一个完整的、跨平台的解决方案,可以记录、转换和传输音频和视频。ffmpeg既可以播放视频,也提供命令行工具来处理视频,另外...

    ffmpeg 手册合集,全面介绍ffmpeg

    音视频转换:FFmpeg可以将视频文件从一种格式转换为另一种格式,支持几乎所有流行的视频和音频格式。 解码和编码:FFmpeg包含许多不同的解码器和编码器,可以用于转换视频和音频数据。 复用和解复用:FFmpeg可以...

    FFmpeg_0.8_win32_SDK

    它提供了录制、转换以及流化音视频的完整解决方案。 FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。它包括了目前领先的音/视频编码库libavcodec等。 libavformat :用于各种音...

Global site tag (gtag.js) - Google Analytics