ffmpeg speex转换为mp3或者aac

输入:html

flv格式,视频264编码,音频speex编码ios

-8:[root@andrew ffmpeg-3.0./ffprobe test_speex1.flv
32:ffprobe version 3.0-static32: Copyright (c) 2007-2016 the FFmpeg developers32:
32:  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-16)
32:  configuration: --enable-gpl --enable-nonfree --disable-shared --enable-postproc --enable-bzlib --enable-zlib --enable-parsers --enable-libx264 --enable-libmp3lame --enable-libfaac --enable-libfdk-aac --enable-pthreads --extra-ldflags=-static --disable-ffserver --enable-ffplay --enable-asm --enable-yasm --enable-optimizations --pkg-config-flags=--static --extra-cflags=--static --extra-version=static --extra-ldflags=-L/usr/local/lib --extra-cflags=-I/usr/local/include --extra-libs=-lrtmp --enable-libspeex --enable-encoder=libspeex --enable-decoder=libspeex
32:  libavutil      55. 17.103 / 55. 17.103
32:  libavcodec     57. 24.102 / 57. 24.102
32:  libavformat    57. 25.100 / 57. 25.100
32:  libavdevice    57.  0.101 / 57.  0.101
32:  libavfilter     6. 31.100 /  6. 31.100
32:  libswscale      4.  0.100 /  4.  0.100
32:  libswresample   2.  0.101 /  2.  0.101
32:  libpostproc    54.  0.100 / 54.  0.100
32:[h264 @ 0x242c260] decoding video .......
32:Input #0, flv, from 'test_speex1.flv':
32:  Metadata:
32:    metadatacreator : 32:Yet Another Metadata Injector for FLV - Version 1.832:
32:    canSeekToEnd    : 32:true32:
32:    videosize       : 32:2034203232:
32:    audiosize       : 32:272241432:
32:    lastkeyframetimestamp: 32:31832:
32:    lastkeyframelocation: 32:2313943332:
32:    encoder         : 32:Lavf57.25.10032:
32:  Duration: 32:00:00:01.7332:, start: 32:0.08100032:, bitrate: 32:331 kb/s32:
32:    Stream #0:032:: Video: h264 (High), yuv420p, 608x448 [SAR 56:57 DAR 4:3]32:, 32:15.17 fps, 32:15 tbr, 32:1k tbn, 32:30 tbc32:
32:    Stream #0:132:: Audio: speex, 16000 Hz, mono, s16, 27 kb/s32:

 

输出h264+mp3app

例1:ide

./ffmpeg -re -i test_speex1.flv -vcodec libx264 -acodec libmp3lame  -y -f flv test_1out.flvpost

-8:[root@andrew ffmpeg-3.0]# ./ffmpeg -re -i test_speex1.flv -vcodec libx264 -acodec libmp3lame -y -f flv test_1out.flv
32:ffmpeg version 3.0-static32: Copyright (c) 2000-2016 the FFmpeg developers32:
32:  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-16)
32:  configuration: --enable-gpl --enable-nonfree --disable-shared --enable-postproc --enable-bzlib --enable-zlib --enable-parsers --enable-libx264 --enable-libmp3lame --enable-libfaac --enable-libfdk-aac --enable-pthreads --extra-ldflags=-static --disable-ffserver --enable-ffplay --enable-asm --enable-yasm --enable-optimizations --pkg-config-flags=--static --extra-cflags=--static --extra-version=static --extra-ldflags=-L/usr/local/lib --extra-cflags=-I/usr/local/include --extra-libs=-lrtmp --enable-libspeex --enable-encoder=libspeex --enable-decoder=libspeex
32:  libavutil      55. 17.103 / 55. 17.103
32:  libavcodec     57. 24.102 / 57. 24.102
32:  libavformat    57. 25.100 / 57. 25.100
32:  libavdevice    57.  0.101 / 57.  0.101
32:  libavfilter     6. 31.100 /  6. 31.100
32:  libswscale      4.  0.100 /  4.  0.100
32:  libswresample   2.  0.101 /  2.  0.101
32:  libpostproc    54.  0.100 / 54.  0.100
32:[h264 @ 0x3ec1b40] decoding video .......
32:Input #0, flv, from 'test_speex1.flv':
32:  Metadata:
32:    metadatacreator : 32:Yet Another Metadata Injector for FLV - Version 1.832:
32:    canSeekToEnd    : 32:true32:
32:    videosize       : 32:2034203232:
32:    audiosize       : 32:272241432:
32:    lastkeyframetimestamp: 32:31832:
32:    lastkeyframelocation: 32:2313943332:
32:    encoder         : 32:Lavf57.25.10032:
32:  Duration: 32:00:00:01.7332:, start: 32:0.08100032:, bitrate: 32:331 kb/s32:
32:    Stream #0:032:: Video: h264 (High), yuv420p, 608x448 [SAR 56:57 DAR 4:3]32:, 32:15.17 fps, 32:15 tbr, 32:1k tbn, 32:30 tbc32:
32:    Stream #0:132:: Audio: speex, 16000 Hz, mono, s16, 27 kb/s32:
32:[libx264 @ 0x3ec2fe0] x264 init info........
32:[libx264 @ 0x3ec2fe0] using SAR=56/57
32:[libx264 @ 0x3ec2fe0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
32:[libx264 @ 0x3ec2fe0] profile High, level 2.2
32:[libx264 @ 0x3ec2fe0] 264 - core 144 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=15 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
16:[flv @ 0x3f6f440] FLV does not support sample rate 16000, choose from (44100, 22050, 11025)
16:[flv @ 0x3f6f440] Audio codec mp3 not compatible with flv
32:Output #0, flv, to 'test_1out.flv':
32:  Metadata:
32:    metadatacreator : 32:Yet Another Metadata Injector for FLV - Version 1.832:
32:    canSeekToEnd    : 32:true32:
32:    videosize       : 32:2034203232:
32:    audiosize       : 32:272241432:
32:    lastkeyframetimestamp: 32:31832:
32:    lastkeyframelocation: 32:2313943332:
32:    encoder         : 32:Lavf57.25.10032:
32:    Stream #0:032:: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 608x448 [SAR 56:57 DAR 4:3], q=-1--132:, 32:15 fps, 32:1k tbn, 32:15 tbc32:
32:    Metadata:
32:      encoder         : 32:Lavc57.24.102 libx26432:
32:    Side data:
32:      24:unknown side data type 10 (24 bytes)32:
32:    Stream #0:132:: Audio: mp3 (libmp3lame) ([2][0][0][0] / 0x0002), 16000 Hz, mono, s16p32:
32:    Metadata:
32:      encoder         : 32:Lavc57.24.102 libmp3lame32:
32:Stream mapping:
32:  Stream #0:0 -> #0:032: (h264 (native) -> h264 (libx264))32:
32:  Stream #0:1 -> #0:132: (speex (libspeex) -> mp3 (libmp3lame))32:
16:Could not write header for output file #0 (incorrect codec parameters ?): Function not implemented

问题:ui

16:[flv @ 0x3f6f440] FLV does not support sample rate 16000, choose from (44100, 22050, 11025)
16:[flv @ 0x3f6f440] Audio codec mp3 not compatible with flv
编码

libmp3lame.c代码code

static const int libmp3lame_sample_rates[] = {
    44100, 48000,  32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
};

 

例2:orm

./ffmpeg -re -i test_speex1.flv -vcodec libx264 -acodec libmp3lame -ar 22050 -y -f flv test_1out.flv视频

可以正常转码。

 

例3:

./ffmpeg -re -i test_speex1.flv -vcodec libx264 -acodec aac -y -f flv test_1out.flv

可以正常转码。即aac支持16kHz的采样率。

aacenctab.h

/* duplicated from avpriv_mpeg4audio_sample_rates to avoid shared build
 * failures */
static const int mpeg4audio_sample_rates[16] = {
    96000, 88200, 64000, 48000, 44100, 32000,
    24000, 22050, 16000, 12000, 11025, 8000, 7350
};

 

flvenc.c对采样率和声道的判断

static int get_audio_flags(AVFormatContext *s, AVCodecContext *enc)
{
    int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT
                                                   : FLV_SAMPLESSIZE_8BIT;

    if (enc->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
        return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ |
               FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
    else if (enc->codec_id == AV_CODEC_ID_SPEEX) {
        if (enc->sample_rate != 16000) {
            av_log(s, AV_LOG_ERROR,
                   "FLV only supports wideband (16kHz) Speex audio\n");
            return AVERROR(EINVAL);
        }
        if (enc->channels != 1) {
            av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n");
            return AVERROR(EINVAL);
        }
        return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
    } else {
        switch (enc->sample_rate) {
        case 44100:
            flags |= FLV_SAMPLERATE_44100HZ;
            break;
        case 22050:
            flags |= FLV_SAMPLERATE_22050HZ;
            break;
        case 11025:
            flags |= FLV_SAMPLERATE_11025HZ;
            break;
        case 16000: // nellymoser only
        case  8000: // nellymoser only
        case  5512: // not MP3
            if (enc->codec_id != AV_CODEC_ID_MP3) {
                flags |= FLV_SAMPLERATE_SPECIAL;
                break;
            }
        default:
            av_log(s, AV_LOG_ERROR,
                   "FLV does not support sample rate %d, "
                   "choose from (44100, 22050, 11025)\n", enc->sample_rate);
            return AVERROR(EINVAL);
        }
    }

    if (enc->channels > 1)
        flags |= FLV_STEREO;

    switch (enc->codec_id) {
    case AV_CODEC_ID_MP3:
        flags |= FLV_CODECID_MP3    | FLV_SAMPLESSIZE_16BIT;
        break;
    case AV_CODEC_ID_PCM_U8:
        flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_8BIT;
        break;
    case AV_CODEC_ID_PCM_S16BE:
        flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_16BIT;
        break;
    case AV_CODEC_ID_PCM_S16LE:
        flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
        break;
    case AV_CODEC_ID_ADPCM_SWF:
        flags |= FLV_CODECID_ADPCM  | FLV_SAMPLESSIZE_16BIT;
        break;
    case AV_CODEC_ID_NELLYMOSER:
        if (enc->sample_rate == 8000)
            flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO  | FLV_SAMPLESSIZE_16BIT;
        else if (enc->sample_rate == 16000)
            flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
        else
            flags |= FLV_CODECID_NELLYMOSER            | FLV_SAMPLESSIZE_16BIT;
        break;
    case AV_CODEC_ID_PCM_MULAW:
        flags = FLV_CODECID_PCM_MULAW | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
        break;
    case AV_CODEC_ID_PCM_ALAW:
        flags = FLV_CODECID_PCM_ALAW  | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
        break;
    case 0:
        flags |= enc->codec_tag << 4;
        break;
    default:
        av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n",
               avcodec_get_name(enc->codec_id));
        return AVERROR(EINVAL);
    }

    return flags;
}

从以上代码能够总结出如下几点:

1. 编码类型为AV_CODEC_ID_AAC,flv都支持

2.编码类型为AV_CODEC_ID_SPEEX,采样率只能为16000,而且单声道

3. 采用率为44100,22050,11025的全部编码方式都是支持的

4. MP3的编码方式不支持采样率为16000,8000和5512

 

 

总结:

使用ffmpeg转码speex到mp3时,须要指定输出的采样率,不然可能转码合成flv时出问题。