Search code examples
javaandroidcommand-lineffmpeglibx264

Android Error While Using FFMPEG Commands - error while opening encoder for output stream - maybe incorrect parameters such as bit_rate etc


I compiled ffmpeg with libx264 on linux and now when i am running this command

String[] ffmpegCommandImages = {
                    "/data/data/uk.org.humanfocus.hfi/ffmpeg",
                    "-i", videoPath,
                    "-b:v", "1000k",
                    "-vf", "scale=-1:576",
                    "-c:a", "copy",
                     "-c:v", "libx264", 
                    "-threads", "12",
                    "-b:a", "196k",
                    to };

This is the error I get, posted the log...
Please help me out.

***Starting FFMPEG***
***ffmpeg version 2.5.4 Copyright (c) 2000-2015 the FFmpeg developers***   ***  built on Mar  5 2015 19:02:01 with gcc 4.8 (GCC)***   V/asd(26115): ***  configuration:
--prefix=/home/hfi/Downloads/android-ndk-r10d/sources/ffmpeg-2.5.4/android/arm --enable-shared --disable-static --disable-doc --enable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-avdevice --disable-doc --disable-symver --cross-prefix=/home/hfi/Downloads/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
--target-os=linux --arch=arm --enable-cross-compile --enable-libx264 --enable-gpl --sysroot=/home/hfi/Downloads/android-ndk-r10d/platforms/android-9/arch-arm
--extra-cflags='-Os -fpic -I /usr/local/include -marm' --extra-ldflags=' -L /usr/local/lib '***
***  libavutil      54. 15.100 / 54. 15.100***
***  libavcodec     56. 13.100 / 56. 13.100***
***  libavformat    56. 15.102 / 56. 15.102***
***  libavfilter     5.  2.103 /  5.  2.103***
***  libswscale      3.  1.101 /  3.  1.101***
***  libswresample   1.  1.100 /  1.  1.100***
***  libpostproc    53.  3.100 / 53.  3.100*** E/Runnable(26115): CAlled V/asd(26115): ***Input #0, image2, from '/storage/emulated/0/HumanFocus/MarkerFrame/frame%05d.jpg':*** V/asd(26115): ***  Duration: 00:00:01.76, start: 0.000000, bitrate: N/A*** V/asd(26115): ***    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1279x719 [SAR 1:1 DAR 1279:719], 25 fps, 25 tbr, 25 tbn, 25 tbc*** V/asd(26115): ***Please use -q:a or -q:v,
-qscale is ambiguous*** V/asd(26115): ***No pixel format specified, yuvj420p for H.264 encoding chosen.*** V/asd(26115): ***Use -pix_fmt yuv420p for compatibility with outdated media players.*** V/asd(26115): ***[libx264 @ 0x52e80] width not divisible by 2 (1279x719)*** V/asd(26115): ***Output #0, mp4, to '/storage/emulated/0/HumanFocus/WPOvideos/TEMP/20150305144005.mp4':*** V/asd(26115): ***    Stream #0:0: Video: h264, none, q=2-31, 128 kb/s, SAR 1:1 DAR 0:0, 11 fps*** V/asd(26115): ***    Metadata:*** V/asd(26115): ***      encoder         : Lavc56.13.100 libx264*** V/asd(26115): ***Stream mapping:*** V/asd(26115): ***  Stream #0:0 ->
#0:0 (mjpeg (native) -> h264 (libx264))*** V/asd(26115): ***Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height*** V/asdas(26115):
***Ending FFMPEG***

Solution

  • Solution

    Change this:

    scale=-1:576
    

    To this:

    scale=-2:576
    

    Why

    Here's the relevant message in console output:

    [libx264 @ 0x52e80] width not divisible by 2 (1279x719)
    

    When encoding with libx264, with outputs using YUV 4:2:0 chroma subsampling, the width and height must be divisible by 2.

    For the scale filter:

    • -1 in either width or height will automatically calculate a value, in relation to the other dimension, that will preserve the aspect ratio.

    • -2 will do the same as -1, but then will additionally make sure the output is divisible by 2.

    Additional notes

    • Using a single pass with -b:v is generally not recommended. Either use two passes with -b:v or just perform a single pass with -crf. Two passes with -b:v is generally only used if you are targeting a specific output file size; otherwise use a single pass with -crf. See FFmpeg Wiki: H.264 Encoding Guide.

    • You should explicitly add -pix_fmt yuv420p as an output option to ensure compatibility for non-FFmpeg based players. Right now you're relying on the default behavior which is not consistent between ffmpeg versions and may not produce an output playable by crappy players.

    • Consider removing "-threads", "12",. libx264 generally knows what is best and automatically selects an appropriate number of threads by default.

    • Add -movflags +faststart as an output option if your viewers will be watching via progressive download. Otherwise the video will have to be completely downloaded before playback can begin.