Newest 'ffmpeg' Questions - Stack Overflow

http://stackoverflow.com/questions/tagged/ffmpeg

Les articles publiés sur le site

  • Determine if OpenCV has FFMpeg Support at Configuration Time

    17 février 2014, par archgoon

    I'm building my project with CMake, but I want to throw an error if OPENCV has not been compiled with FFMPEG support (as this will prevent a large amount of functinoality of my application). How can I modify my CMake scripts to check for this?

  • Passing FileInputStream and FileOutputStream to ffmpeg to transcode(using JAVE-Java Audio Video Encoding)

    17 février 2014, par Jagan S

    I am trying to transcode a *.mov file into a *.mp4 file using JAVE, which calls ffmpeg. Both input file and output file are in InputStream and OutputStream forms. That means I need to pass InputStream and OutputStream as -i and -y parematers for ffmpeg. How do I do that ?

        //Read a movfile.mov converted into a FileInputStream  
        InputStream fileInputStream = getFileInputStream();  
        OutputStream fileOutputStream = new FileOutputStrea(outputMP4File) //Output        
        Process p = Runtime.exec("ffmpeg -i - -y -");  
        InputStream pInStrm = p.getInputStream();  
        OutputStream pOutStrm = p.getOutputStream();  
        int vin = 0, vout = 0;   
    
        Thread read = new Thread() {  
             byte[] buff = new byte[4096];  
              void run() {   
                while ((vin=fileInputStream.read(buf))!=-1) {   
                     pOutStrm.write(buf, 0, vin);   
                }   
             }   
          }; read.start();
    
        Thread write = new Thread() {  
            byte[] buff = new byte[4096];  
            void run() {  
                 while ((vout=pInStrm.read(buf))!=-1) {  
                     fileOutputStream.write(buf, 0, vout);  
                }  
             }  
          }; write.start();  
    

    But I keep getting "IOException: pipe is closed" error. Could somebody help me out ? Alternatively if there is any JAVA API that could do this transcoding(on Windows and RedHat Linux), that would be very helpful

    Thanks

  • use ffmpeg api to convert audio files. crash on avcodec_encode_audio2

    17 février 2014, par fabian

    From the examples I got the basic idea of this code. However I am not sure, what I am missing, as muxing.c demuxing.c and decoding_encoding.c all use different approaches.

    The process of converting an audio file to another file should go roughly like this: inputfile -demux-> audiostream -read-> inPackets -decode2frames-> frames -encode2packets-> outPackets -write-> audiostream -mux-> outputfile

    However I found the following comment in demuxing.c: /* Write the raw audio data samples of the first plane. This works
    * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
    * most audio decoders output planar audio, which uses a separate
    * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
    * In other words, this code will write only the first audio channel
    * in these cases.
    * You should use libswresample or libavfilter to convert the frame
    * to packed data. */

    My questions about this are:

    1. Can I expect a frame that was retrieved by calling one of the decoder functions, f.e. avcodec_decode_audio4 to hold suitable values to directly put it into an encoder or is the resampling step mentioned in the comment mandatory?

    2. Am I taking the right approach? ffmpeg is very asymmetric, i.e. if there is a function open_file_for_input there might not be a function open_file_for_output. Also there are different versions of many functions (avcodec_decode_audio[1-4]) and different naming schemes, so it's very hard to tell, if the general approach is right, or actually an ugly mixture of techniques that where used at different version bumps of ffmpeg.

    3. ffmpeg uses a lot of specific terms, like 'planar sampling' or 'packed format' and I am having a hard time, finding definitions for these terms. Is it possible to write working code, without deep knowledge of audio?

    Here is my code so far that right now crashes at avcodec_encode_audio2 and I don't know why.

    int Java_com_fscz_ffmpeg_Audio_convert(JNIEnv * env, jobject this, jstring jformat, jstring jcodec, jstring jsource, jstring jdest) {
        jboolean isCopy;
        jclass configClass = (*env)->FindClass(env, "com.fscz.ffmpeg.Config");
        jfieldID fid = (*env)->GetStaticFieldID(env, configClass, "ffmpeg_logging", "I");
        logging = (*env)->GetStaticIntField(env, configClass, fid);
    
        /// open input
        const char* sourceFile = (*env)->GetStringUTFChars(env, jsource, &isCopy);
        AVFormatContext* pInputCtx;
        AVStream* pInputStream;
        open_input(sourceFile, &pInputCtx, &pInputStream);
    
        // open output
        const char* destFile = (*env)->GetStringUTFChars(env, jdest, &isCopy);
        const char* cformat = (*env)->GetStringUTFChars(env, jformat, &isCopy);
        const char* ccodec = (*env)->GetStringUTFChars(env, jcodec, &isCopy);
        AVFormatContext* pOutputCtx;
        AVOutputFormat* pOutputFmt;
        AVStream* pOutputStream;
        open_output(cformat, ccodec, destFile, &pOutputCtx, &pOutputFmt, &pOutputStream);
    
        /// decode/encode
        error = avformat_write_header(pOutputCtx, NULL);
        DIE_IF_LESS_ZERO(error, "error writing output stream header to file: %s, error: %s", destFile, e2s(error));
    
        AVFrame* frame = avcodec_alloc_frame();
        DIE_IF_UNDEFINED(frame, "Could not allocate audio frame");
        frame->pts = 0;
    
        LOGI("allocate packet");
        AVPacket pktIn;
        AVPacket pktOut;
        LOGI("done");
        int got_frame, got_packet, len, frame_count = 0;
        int64_t processed_time = 0, duration = pInputStream->duration;
        while (av_read_frame(pInputCtx, &pktIn) >= 0) {
            do {
                len = avcodec_decode_audio4(pInputStream->codec, frame, &got_frame, &pktIn);
                DIE_IF_LESS_ZERO(len, "Error decoding frame: %s", e2s(len));
                if (len < 0) break;
                len = FFMIN(len, pktIn.size);
                size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample(frame->format);
                LOGI("audio_frame n:%d nb_samples:%d pts:%s\n", frame_count++, frame->nb_samples, av_ts2timestr(frame->pts, &(pInputStream->codec->time_base)));
                if (got_frame) {
                    do {
                        av_init_packet(&pktOut);
                        pktOut.data = NULL;
                        pktOut.size = 0;
                        LOGI("encode frame");
                        DIE_IF_UNDEFINED(pOutputStream->codec, "no output codec");
                        DIE_IF_UNDEFINED(frame->nb_samples, "no nb samples");
                        DIE_IF_UNDEFINED(pOutputStream->codec->internal, "no internal");
                        LOGI("tests done");
                        len = avcodec_encode_audio2(pOutputStream->codec, &pktOut, frame, &got_packet);
                        LOGI("encode done");
                        DIE_IF_LESS_ZERO(len, "Error (re)encoding frame: %s", e2s(len));
                    } while (!got_packet);
                    // write packet;
                    LOGI("write packet");
                    /* Write the compressed frame to the media file. */
                    error = av_interleaved_write_frame(pOutputCtx, &pktOut);
                    DIE_IF_LESS_ZERO(error, "Error while writing audio frame: %s", e2s(error));
                    av_free_packet(&pktOut);
                }
                pktIn.data += len;
                pktIn.size -= len;
            } while (pktIn.size > 0);
            av_free_packet(&pktIn);
        }
    
        LOGI("write trailer");
        av_write_trailer(pOutputCtx);
        LOGI("end");
    
        /// close resources
        avcodec_free_frame(&frame);
        avcodec_close(pInputStream->codec);
        av_free(pInputStream->codec);
        avcodec_close(pOutputStream->codec);
        av_free(pOutputStream->codec);
        avformat_close_input(&pInputCtx);
        avformat_free_context(pOutputCtx);
    
        return 0;
    }
    
  • Specify background colour when generating movie from images

    17 février 2014, par Uko

    I generate a video from png images using

    ffmpeg -i visualization/%d.png -c:v libx264 -vf "scale=500:trunc(ow/a/2)*2"  -pix_fmt yuv420p z.mov
    

    and if images have transparencies, they become black. Can I somehow make them white?

  • Static Compilation of FFmpeg with x264 support

    17 février 2014, par b1izzard

    Based on the Tutorial, I tried to build the latest FFmpeg binary with x264 support for Command line execution using Android-ndk-r9c.

    I had built the x264 using the below script:

     #!/bin/bash        
    
    
    PREBUILT=/home/blizzard/bin/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt
                    PLATFORM=/home/blizzard/bin/android-ndk-r9c/platforms/android-9/arch-arm
                    PREFIX=/home/blizzard/bin/android-ndk-r9c/sources/ffmpeg
    
    
                ./configure --prefix=$PREFIX \
                --enable-static \
                --enable-pic \
                --disable-asm \
                --disable-cli \             
                --host=arm-linux \
                --cross-prefix=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi- \
                --sysroot=$PLATFORM
    
                make
                sudo make install
                sudo ldconfig
    

    For building FFmpeg I had used the below script:

     #!/bin/bash                    
                  PLATFORM=/home/blizzard/bin/android-ndk-r9c/platforms/android-9/arch-arm
                  PREBUILT=/home/blizzard/bin/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64
                  PREFIX=/home/blizzard/bin/android-ndk-r9c/sources/ffmpeg
    
                    function build_one
                    {
                    ./configure --target-os=linux --prefix=$PREFIX \
                    --arch=arm \
                    --cpu=cortex-a8 \
                    --enable-cross-compile \
                    --enable-runtime-cpudetect \
                    --disable-asm \
                    --enable-static \
                    --disable-shared \
                    --arch=arm \
                    --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
                    --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
                    --disable-stripping \
                    --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
                    --sysroot=$PLATFORM \
                    --enable-nonfree \
                    --enable-version3 \
                    --disable-everything \
                    --enable-gpl \
                    --disable-doc \
                    --enable-avresample \
                    --enable-demuxer=rtsp \
                    --enable-muxer=rtsp \
                    --disable-ffplay \
                    --disable-ffserver \
                    --enable-ffmpeg \
                    --disable-ffprobe \
                    --enable-libx264 \
                    --enable-encoder=libx264 \
                    --enable-decoder=h264 \
                    --enable-protocol=rtp \
                    --enable-hwaccels \
                    --enable-zlib \
                    --disable-devices \
                    --disable-avdevice \
                    --extra-cflags="-I/home/blizzard/bin/android-ndk-r9c/sources/ffmpeg/include -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a" \
                    --extra-ldflags="-L/home/blizzard/bin/android-ndk-r9c/sources/ffmpeg/lib"
                    make -j4 install
    
                    $PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
                    $PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib  -soname libffmpeg.so -shared -nostdlib  -z noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavfilter/libavfilter.a libavresample/libavresample.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog -lx264  --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.6/libgcc.a
                    }
    
                    build_one
    

    The output of the script is

    install prefix            /home/blizzard/bin/android-ndk-r9c/sources/ffmpeg
    source path               .
    C compiler                /home/blizzard/bin/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
    C library                 bionic
    host C compiler           gcc
    host C library            glibc
    ARCH                      c (cortex-a8)
    big-endian                no
    runtime cpu detection     yes
    debug symbols             yes
    strip symbols             no
    optimize for size         no
    optimizations             yes
    static                    yes
    shared                    no
    postprocessing support    yes
    new filter support        yes
    network support           yes
    threading support         pthreads
    safe bitstream reader     yes
    SDL support               no
    opencl enabled            no
    libzvbi enabled           no
    texi2html enabled         yes
    perl enabled              yes
    pod2man enabled           yes
    makeinfo enabled          no
    
    External libraries:
    libx264         zlib
    
    Enabled decoders:
    h264
    
    Enabled encoders:
    libx264
    
    Enabled hwaccels:
    
    Enabled parsers:
    
    Enabled demuxers:
    asf         mpegts          rtsp
    mov         rm
    
    Enabled muxers:
    rtp         rtsp
    
    Enabled protocols:
    http            tcp         udp
    rtp
    
    Enabled filters:
    aformat         format          setpts
    anull           null            trim
    atrim
    
    Enabled bsfs:
    
    Enabled indevs:
    
    Enabled outdevs:
    
    License: nonfree and unredistributable
    Creating config.mak, config.h, and doc/config.texi...
    config.h is unchanged
    libavutil/avconfig.h is unchanged
    
    WARNING: /home/blizzard/bin/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-pkg-config not found, library detection may fail.
    INSTALL install-progs-yes
    INSTALL ffmpeg
    INSTALL presets/libvpx-1080p50_60.ffpreset
    INSTALL presets/libvpx-1080p.ffpreset
    INSTALL presets/libvpx-360p.ffpreset
    INSTALL presets/libvpx-720p50_60.ffpreset
    INSTALL presets/libvpx-720p.ffpreset
    INSTALL presets/libx264-ipod320.ffpreset
    INSTALL presets/libx264-ipod640.ffpreset
    INSTALL doc/ffprobe.xsd
    INSTALL doc/examples/decoding_encoding.c
    INSTALL doc/examples/demuxing_decoding.c
    INSTALL doc/examples/filtering_audio.c
    INSTALL doc/examples/filtering_video.c
    INSTALL doc/examples/metadata.c
    INSTALL doc/examples/muxing.c
    INSTALL doc/examples/remuxing.c
    INSTALL doc/examples/resampling_audio.c
    INSTALL doc/examples/scaling_video.c
    INSTALL doc/examples/transcode_aac.c
    INSTALL doc/examples/Makefile
    INSTALL doc/examples/README
    INSTALL libavfilter/libavfilter.a
    INSTALL libavformat/libavformat.a
    INSTALL presets/libvpx-1080p50_60.ffpreset
    INSTALL presets/libvpx-1080p.ffpreset
    INSTALL presets/libvpx-360p.ffpreset
    INSTALL presets/libvpx-720p50_60.ffpreset
    INSTALL presets/libvpx-720p.ffpreset
    INSTALL presets/libx264-ipod320.ffpreset
    INSTALL presets/libx264-ipod640.ffpreset
    INSTALL doc/ffprobe.xsd
    INSTALL doc/examples/decoding_encoding.c
    INSTALL doc/examples/demuxing_decoding.c
    INSTALL doc/examples/filtering_audio.c
    INSTALL doc/examples/filtering_video.c
    INSTALL doc/examples/metadata.c
    INSTALL doc/examples/muxing.c
    INSTALL doc/examples/remuxing.c
    INSTALL doc/examples/resampling_audio.c
    INSTALL doc/examples/scaling_video.c
    INSTALL doc/examples/transcode_aac.c
    INSTALL doc/examples/Makefile
    INSTALL doc/examples/README
    INSTALL libavresample/libavresample.a
    INSTALL libavcodec/libavcodec.a
    INSTALL libpostproc/libpostproc.a
    INSTALL libswresample/libswresample.a
    INSTALL libavutil/libavutil.a
    a
    INSTALL libavfilter/asrc_abuffer.h
    INSTALL libavfilter/avcodec.h
    INSTALL libavfilter/avfilter.h
    INSTALL libavfilter/avfiltergraph.h
    INSTALL libavfilter/buffersink.h
    INSTALL libavfilter/buffersrc.h
    INSTALL libavfilter/version.h
    INSTALL libavformat/avformat.h
    INSTALL libavformat/avio.h
    INSTALL libavformat/version.h
    INSTALL libavfilter/libavfilter.pc
    INSTALL libavformat/libavformat.pc
    INSTALL libavresample/avresample.h
    INSTALL libavresample/version.h
    INSTALL libavresample/libavresample.pc
    INSTALL libavcodec/avcodec.h
    INSTALL libavcodec/avfft.h
    INSTALL libavcodec/dxva2.h
    INSTALL libavcodec/old_codec_ids.h
    INSTALL libavcodec/vaapi.h
    INSTALL libavcodec/vda.h
    INSTALL libavcodec/vdpau.h
    INSTALL libavcodec/version.h
    INSTALL libavcodec/xvmc.h
    INSTALL libavcodec/libavcodec.pc
    INSTALL libpostproc/postprocess.h
    INSTALL libpostproc/version.h
    INSTALL libpostproc/libpostproc.pc
    INSTALL libswresample/swresample.h
    INSTALL libswresample/version.h
    INSTALL libswscale/swscale.h
    INSTALL libswscale/version.h
    INSTALL libavutil/adler32.h
    INSTALL libavutil/aes.h
    INSTALL libavutil/attributes.h
    INSTALL libavutil/audio_fifo.h
    INSTALL libavutil/audioconvert.h
    INSTALL libavutil/avassert.h
    INSTALL libavutil/avstring.h
    INSTALL libavutil/avutil.h
    INSTALL libavutil/base64.h
    INSTALL libavutil/blowfish.h
    INSTALL libavutil/bprint.h
    INSTALL libavutil/bswap.h
    INSTALL libavutil/buffer.h
    INSTALL libavutil/channel_layout.h
    INSTALL libavutil/common.h
    INSTALL libavutil/cpu.h
    INSTALL libavutil/crc.h
    INSTALL libavutil/error.h
    INSTALL libavutil/eval.h
    INSTALL libavutil/fifo.h
    INSTALL libavutil/file.h
    INSTALL libavutil/frame.h
    INSTALL libavutil/hmac.h
    INSTALL libavutil/imgutils.h
    INSTALL libavutil/intfloat.h
    INSTALL libavutil/intfloat_readwrite.h
    INSTALL libavutil/intreadwrite.h
    INSTALL libavutil/lfg.h
    INSTALL libavutil/log.h
    INSTALL libavutil/macros.h
    INSTALL libavutil/mathematics.h
    INSTALL libavutil/md5.h
    INSTALL libavutil/mem.h
    INSTALL libavutil/murmur3.h
    INSTALL libavutil/dict.h
    INSTALL libavutil/old_pix_fmts.h
    INSTALL libavutil/opt.h
    INSTALL libavutil/parseutils.h
    INSTALL libavutil/pixdesc.h
    INSTALL libavutil/pixfmt.h
    INSTALL libavutil/random_seed.h
    INSTALL libavutil/rational.h
    INSTALL libavutil/ripemd.h
    INSTALL libavutil/samplefmt.h
    INSTALL libavutil/sha.h
    INSTALL libavutil/sha512.h
    INSTALL libavutil/stereo3d.h
    INSTALL libavutil/time.h
    INSTALL libavutil/timecode.h
    INSTALL libavutil/timestamp.h
    INSTALL libavutil/version.h
    INSTALL libavutil/xtea.h
    INSTALL libavutil/avconfig.h
    INSTALL libavutil/ffversion.h
    INSTALL libswresample/libswresample.pc
    INSTALL libswscale/libswscale.pc
    INSTALL libavutil/libavutil.pc
    

    When I tried to execute the following command using the resulting FFmpeg binary in Android Application

    ffmpeg -y -loop 1 -r 30 -i  myImage.jpg -b:v "4096k" -vf "scale=640:480" -t 3 result.mp4
    

    LogCat displays the following message

    02-17 15:42:45.683: D/(14511): *******Starting FFMPEG
    02-17 15:42:45.683: D/(14511): ***ffmpeg version N-60108-gda25a65 Copyright (c) 2000-2014 the FFmpeg developers***
    02-17 15:42:45.693: D/(14511): ***  built on Feb 17 2014 15:35:20 with gcc 4.6 (GCC) 20120106 (prerelease)***
    02-17 15:42:45.693: D/(14511): ***  configuration: --target-os=linux --prefix=/home/blizzard/bin/android-ndk-r9c/sources/ffmpeg --arch=arm --cpu=cortex-a8 --enable-cross-compile --enable-runtime-cpudetect --disable-asm --enable-static --disable-shared --arch=arm --cc=/home/blizzard/bin/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc --cross-prefix=/home/blizzard/bin/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi- --disable-stripping --nm=/home/blizzard/bin/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-nm --sysroot=/home/blizzard/bin/android-ndk-r9c/platforms/android-9/arch-arm --enable-nonfree --enable-version3 --disable-everything --enable-gpl --disable-doc --enable-avresample --enable-demuxer=rtsp --enable-muxer=rtsp --disable-ffplay --disable-ffserver --enable-ffmpeg --disable-ffprobe --enable-libx264 --enable-encoder=libx264 --enable-decoder=h264 --enable-protocol=rtp --enable-hwaccels --enable-zlib --disable-devices --disable-avdevice --extra-cflags='-I/home/blizzard/bin/android-ndk-r9c/sources/ffmpeg/include -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=armv7-a' --extra-ldflags=-L/home/blizzard/bin/android-ndk-r9c/sources/ffmpeg/lib***
    02-17 15:42:45.693: D/(14511): ***  libavutil      52. 63.100 / 52. 63.100***
    02-17 15:42:45.693: D/(14511): ***  libavcodec     55. 49.100 / 55. 49.100***
    02-17 15:42:45.693: D/(14511): ***  libavformat    55. 25.101 / 55. 25.101***
    02-17 15:42:45.693: D/(14511): ***  libavfilter     4.  1.100 /  4.  1.100***
    02-17 15:42:45.693: D/(14511): ***  libavresample   1.  1.  0 /  1.  1.  0***
    02-17 15:42:45.693: D/(14511): ***  libswscale      2.  5.101 /  2.  5.101***
    02-17 15:42:45.693: D/(14511): ***  libswresample   0. 17.104 /  0. 17.104***
    02-17 15:42:45.693: D/(14511): ***  libpostproc    52.  3.100 / 52.  3.100***
    02-17 15:42:45.693: D/(14511): ***Unrecognized option 'loop'.***
    02-17 15:42:45.693: D/(14511): ***Error splitting the argument list: Option not found***
    02-17 15:42:45.693: D/(14511): ****ending FFMPEG****
    

    Can anybody help me in building the latest version of FFmpeg or Can you share the latest FFMpeg binary for Android?