Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • using ffmpeg in Azure function to cut files using c#

    24 mars 2017, par abhishekmoondra1989

    I have written an Azure function in C# which will cut a big mp4 files into some small duration. I have copied everything that is required (ffmpeg executeable, video file) in home directory via KUDU console. But when I run the the function it runs for more than 5 minutes and it doesn't give any files in the home directory.

    Function :

    using System;
    using System.Diagnostics;
    
    public static void Run(string input, TraceWriter log)
    {
        log.Info("Executing");
        using (var process = new Process())
        {
            process.StartInfo.FileName = @"D:\home\ffmpeg.exe";
            process.StartInfo.Arguments = @"-i D:\home\AmnestyInternational.mp4 -ss 00:00:03 -t 00:00:08 -async 1 D:\home\cut.mp4";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            log.Info(Directory.GetCurrentDirectory());
            log.Info("Cutting starts:"+DateTime.Now.ToString("h:mm:ss tt"));
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            log.Info("Cutting ends :"+DateTime.Now.ToString("h:mm:ss tt"));
            log.Info(output);
        }
    }
    

    Output seen on Azure function Console:

    2017-03-24T11:06:00.705 Function started (Id=df082f54-719a-415f-b7f1-b10548a213be)
    2017-03-24T11:06:00.721 Executing
    2017-03-24T11:06:00.721 D:\Windows\system32
    2017-03-24T11:06:00.721 Cutting start :11:06:00 AM
    2017-03-24T11:07:14  No new trace in the past 1 min(s).
    2017-03-24T11:08:14  No new trace in the past 2 min(s).
    2017-03-24T11:09:14  No new trace in the past 3 min(s).
    2017-03-24T11:10:14  No new trace in the past 4 min(s).
    2017-03-24T11:11:00.758 Microsoft.Azure.WebJobs.Host: Timeout value of 00:05:00 was exceeded by function: Functions.ManualTriggerCSharp1.
    

    When I try to execute this same command on KUDU console or my own PC it only take 1.5 mins and I get a file of the desired duration

    Could anyone please help me with this? What I might be missing?

  • Check if a video file has subtitles

    24 mars 2017, par TRS

    Is it possible to check if a video file has a subtitle using bash and get a simple answer like "yes" or "no". I don't need to know any details about the subtitles.

    Maybe using ffmpeg?

  • Reading h264 stream in Python

    24 mars 2017, par verified.human

    I am trying to read a video stream from a Parrot Bebop 2 drone. The video stream is written as "socket" to a file as H264 stream.

    $ ffmpeg -i [STREAM]
    Input #0, h264, from 'stream_h264':
      Duration: N/A, bitrate: N/A
      Stream #0:0: Video: h264 (Constrained Baseline), 1 reference frame, yuv420p(progressive, left), 1280x720, 23.98 fps, 23.98 tbr, 1200k tbn, 47.95 tbc
    

    Reading the video stream in MPlayer is not a problem using the parameters below. Playing it using VLC or ffmpeg should also not be too hard. For MPlayer the following works:

    mplayer -fs -demuxer h264es -benchmark stream_h264
    

    This plays the stream in high-res. However my goal is to perform image processing on the frames using Python (mostly OpenCV). Therefore, I would like to read the frames into NumPy arrays. I have already considered using cv2.VideoCapture but this does not seem to work for my stream. Other (somewhat easy) to use options I am not aware of, therefore my question is whether someone recommend me how to read the video frames in Python?

    All recommendations are more than welcome!

  • Cant link against ffmpeg static build

    24 mars 2017, par David Barishev

    I have build ffmpeg libraries statically for x86 android using a custom configuration.Now i wanted to add them in my android project.

    Im using ffmpeg 3.2.git, android studio 2.3.

    I created a folder named distribution which had my binaries, and the minimum headers i needed for it to not tell me about missing header (Except for the avutil- i just included them all), located the root of my project.
    Here is a file tree:

    distribution
    ├── libavcodec
    │   ├── lib
    │   │   └── x86
    │   │       └── libavcodec.a
    │   ├── avcodec.h
    │   └── version.h
    ├── libavfilter
    │   ├── lib
    │   │   └── x86
    │   │       └── libavfilter.a
    │   ├── avfiltergraph.h
    │   └── avfilter.h
    ├── libavformat
    │   ├── lib
    │   │   └── x86
    │   │       └── libavformat.a
    │   ├── avformat.h
    │   ├── avio.h
    │   └── version.h
    ├── libavutil
    │   ├── lib
    │   │   └── x86
    │   │       └── libavutil.a
    │   ├── attributes.h
    │   ├── avconfig.h
    │   ├── avutil.h
    │   ├── buffer.h
    │   ├── buffer_internal.h
    │   ├── camellia.h
    │   ├── cast5.h
    │   ├── channel_layout.h
    │   ├── colorspace.h
    │   ├── color_utils.h
    │   ├── common.h
    │   ├── cpu.h
    │   ├── cpu_internal.h
    │   ├── crc.h
    │   ├── des.h
    │   ├── dict.h
    │   ├── display.h
    │   ├── downmix_info.h
    │   ├── dynarray.h
    │   ├── error.h
    │   ├── eval.h
    │   ├── ffmath.h
    │   ├── ffversion.h
    │   ├── fifo.h
    │   ├── file.h
    │   ├── fixed_dsp.h
    │   ├── float_dsp.h
    │   ├── frame.h
    │   ├── hash.h
    │   ├── hmac.h
    │   ├── hwcontext_cuda.h
    │   ├── hwcontext_cuda_internal.h
    │   ├── hwcontext_dxva2.h
    │   ├── hwcontext.h
    │   ├── hwcontext_internal.h
    │   ├── hwcontext_qsv.h
    │   ├── hwcontext_vaapi.h
    │   ├── hwcontext_vdpau.h
    │   ├── imgutils.h
    │   ├── integer.h
    │   ├── internal.h
    │   ├── intfloat.h
    │   ├── intmath.h
    │   ├── intreadwrite.h
    │   ├── lfg.h
    │   ├── libm.h
    │   ├── lls.h
    │   ├── log.h
    │   ├── lzo.h
    │   ├── macros.h
    │   ├── mastering_display_metadata.h
    │   ├── mathematics.h
    │   ├── md5.h
    │   ├── mem.h
    │   ├── mem_internal.h
    │   ├── motion_vector.h
    │   ├── murmur3.h
    │   ├── opencl.h
    │   ├── opencl_internal.h
    │   ├── opt.h
    │   ├── parseutils.h
    │   ├── pca.h
    │   ├── pixdesc.h
    │   ├── pixelutils.h
    │   ├── pixfmt.h
    │   ├── qsort.h
    │   ├── random_seed.h
    │   ├── rational.h
    │   ├── rc4.h
    │   ├── replaygain.h
    │   ├── reverse.h
    │   ├── ripemd.h
    │   ├── samplefmt.h
    │   ├── sha512.h
    │   ├── sha.h
    │   ├── softfloat.h
    │   ├── softfloat_ieee754.h
    │   ├── softfloat_tables.h
    │   ├── spherical.h
    │   ├── stereo3d.h
    │   ├── tablegen.h
    │   ├── tea.h
    │   ├── thread.h
    │   ├── threadmessage.h
    │   ├── timecode.h
    │   ├── time.h
    │   ├── time_internal.h
    │   ├── timer.h
    │   ├── timestamp.h
    │   ├── tree.h
    │   ├── twofish.h
    │   └── version.h
    └── libswresample
        ├── lib
        │   └── x86
        │       └── libswresample.a
        ├── swresample.h
        └── swresample_internal.h 
    

    I edited my cmake to include the libraries:

    add_library(
             native-lib
             SHARED
             src/main/cpp/native-lib.cpp )
    
    set(distribution_DIR ${CMAKE_SOURCE_DIR}/../distribution)
    
    add_library(lib_avcodec STATIC IMPORTED)
    set_target_properties(lib_avcodec PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/libavcodec/lib/${ANDROID_ABI}/libavcodec.a)
    
    add_library(lib_avfilter STATIC IMPORTED)
    set_target_properties(lib_avfilter PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/libavfilter/lib/${ANDROID_ABI}/libavfilter.a)
    
    add_library(lib_avformat STATIC IMPORTED)
    set_target_properties(lib_avformat PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/libavformat/lib/${ANDROID_ABI}/libavformat.a)
    
    add_library(lib_avutil STATIC IMPORTED)
    set_target_properties(lib_avutil PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/libavutil/lib/${ANDROID_ABI}/libavutil.a)
    
    add_library(lib_swresample STATIC IMPORTED)
    set_target_properties(lib_swresample PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/libswresample/lib/${ANDROID_ABI}/libswresample.a)
    
    
    include_directories(
                              ${distribution_DIR}
                              )
    
    
    target_link_libraries( 
                           native-lib
    
                           lib_avcodec
                           lib_avfilter
                           lib_avformat
                           lib_avutil
                           lib_swresample
                            )
    

    I also restricted the build to only x86, in my app build.gradle :

    ndk {
                // Specifies the ABI configurations of your native
                // libraries Gradle should build and package with your APK.
                abiFilters 'x86'
        }
    

    The project gradle sync worked successfully.
    I wrote the following code in my cpp file :

    #include 
    
    
    extern "C"{
        #include "libavformat/avformat.h"
    }
    JNIEXPORT void JNICALL
    Java_com_example_david_testffmpegcpp_MainActivity_stringFromJNI(
            JNIEnv *env,
            jobject /* this */) {
    
        av_register_all ();
        avformat_network_init ();
    
    
    }
    

    Just to check if the library works, but i cant seem to link against the libraries correctly. It complains about undefined symbols. What did i do wrong ?

    Error :

    [2/2] Linking CXX shared library 
    
    ..\..\..\..\build\intermediates\cmake\debug\obj\x86\libnative-lib.so
    FAILED: cmd.exe /C "cd . && D:\AndroidSDK\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=i686-none-linux-android --gcc-toolchain=D:/AndroidSDK/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64 --sysroot=D:/AndroidSDK/ndk-bundle/platforms/android-16/arch-x86 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security  -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  -llog ../../../../../distribution/libavcodec/lib/x86/libavcodec.a ../../../../../distribution/libavfilter/lib/x86/libavfilter.a ../../../../../distribution/libavformat/lib/x86/libavformat.a ../../../../../distribution/libavutil/lib/x86/libavutil.a ../../../../../distribution/libswresample/lib/x86/libswresample.a -lm "D:/AndroidSDK/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/libgnustl_static.a" && cd ."
    src/libavformat/allformats.c:51: error: undefined reference to 'avcodec_register_all'
      src/libavformat/id3v2.c:1009: error: undefined reference to 'uncompress'
      src/libavformat/id3v2.c:1153: error: undefined reference to 'av_init_packet'
      src/libavformat/matroskadec.c:1393: error: undefined reference to 'inflateInit_'
      src/libavformat/matroskadec.c:1408: error: undefined reference to 'inflate'
      src/libavformat/matroskadec.c:1401: error: undefined reference to 'inflateEnd'
      src/libavformat/matroskadec.c:1411: error: undefined reference to 'inflateEnd'
      src/libavformat/matroskadec.c:3113: error: undefined reference to 'av_new_packet'
      src/libavformat/matroskadec.c:3134: error: undefined reference to 'av_packet_new_side_data'
      src/libavformat/matroskadec.c:2855: error: undefined reference to 'av_new_packet'
      src/libavformat/matroskadec.c:3147: error: undefined reference to 'av_packet_new_side_data'
      src/libavformat/matroskadec.c:3022: error: undefined reference to 'av_new_packet'
      src/libavformat/matroskadec.c:3031: error: undefined reference to 'av_packet_new_side_data'
      src/libavformat/matroskadec.c:3042: error: undefined reference to 'av_packet_new_side_data'
      src/libavformat/matroskadec.c:3151: error: undefined reference to 'av_packet_unref'
      src/libavformat/matroskadec.c:2680: error: undefined reference to 'av_packet_unref'
      src/libavformat/matroskadec.c:963: error: undefined reference to 'av_fast_padded_malloc'
      src/libavformat/matroskadec.c:2163: error: undefined reference to 'av_get_codec_tag_string'
      src/libavformat/matroskadec.c:2680: error: undefined reference to 'av_packet_unref'
      src/libavformat/matroskadec.c:2579: error: undefined reference to 'av_init_packet'
      src/libavformat/matroskadec.c:2580: error: undefined reference to 'av_new_packet'
      src/libavformat/matroskadec.c:1715: error: undefined reference to 'avpriv_mpeg4audio_sample_rates'
      src/libavformat/matroskadec.c:1715: error: undefined reference to 'avpriv_mpeg4audio_sample_rates'
      src/libavformat/matroskadec.c:1715: error: undefined reference to 'avpriv_mpeg4audio_sample_rates'
      src/libavformat/matroskadec.c:1866: error: undefined reference to 'avcodec_chroma_pos_to_enum'
      src/libavformat/matroskadec.c:2680: error: undefined reference to 'av_packet_unref'
      src/libavformat/matroskaenc.c:2482: error: undefined reference to 'avcodec_get_type'
      src/libavformat/matroskaenc.c:646: error: undefined reference to 'avpriv_split_xiph_headers'
      src/libavformat/matroskaenc.c:1066: error: undefined reference to 'av_get_bits_per_sample'
      src/libavformat/matroskaenc.c:723: error: undefined reference to 'avpriv_mpeg4audio_get_config'
      src/libavformat/matroskaenc.c:875: error: undefined reference to 'avcodec_enum_to_chroma_pos'
      src/libavformat/matroskaenc.c:813: error: undefined reference to 'avcodec_get_name'
      src/libavformat/matroskaenc.c:806: error: undefined reference to 'avcodec_get_name'
      src/libavformat/matroskaenc.c:824: error: undefined reference to 'avcodec_get_name'
      .....
    
  • ffmpeg API muxing h264 endoced frames to mkv

    24 mars 2017, par Pawel K

    Hi I'm having some problems with muxing h264 encoded frames into mkv container using code of ffmpeg-3.2.4. I have ended up with the following code that is a mashup of code found on SO and muxing.c example of ffmpeg: (and yes I am aware that it is ugly, no errors checked etc. it is meant to be like that for clarity :) )

    char *filename = "out.mkv";
    const uint8_t SPS[] = { 0x67, 0x42, 0x40, 0x1F, 0x96, 0x54, 0x02, 0x80, 0x2D, 0xD0, 0x0F, 0x39, 0xEA };
    const uint8_t PPS[] = { 0x68, 0xCE, 0x38, 0x80 };
    int fps = 5;
    
    typedef struct OutputStream
    {
       AVStream *st;
       AVCodecContext *enc;
    
       /* pts of the next frame that will be generated */
       int64_t next_pts;
       int samples_count;
       AVFrame *frame;
       AVFrame *tmp_frame;
       float t, tincr, tincr2;
       struct SwsContext *sws_ctx;
       struct SwrContext *swr_ctx;
    } OutputStream;
    
    static void avlog_cb(void *s, int level, const char *szFmt, va_list varg)
    {
       vprintf(szFmt, varg);
    }
    
    void main()
    {
       AVOutputFormat *fmt;
       AVFormatContext *formatCtx;
       AVCodec *audio_codec;
       AVCodec *video_codec;
       OutputStream video_st = { 0 };
       OutputStream audio_st = { 0 };
       av_register_all();
    
       av_log_set_level(AV_LOG_TRACE);
       //av_log_set_callback(avlog_cb);
    
       //allocate output and format ctxs
       avformat_alloc_output_context2(&formatCtx, NULL, NULL, filename);
       fmt = formatCtx->oformat;
    
       //allocate streams
       video_codec = avcodec_find_encoder(fmt->video_codec);
       video_st.st = avformat_new_stream(formatCtx, NULL);
       video_st.st->id = 0;
    
       AVCodecContext *codecCtx =  avcodec_alloc_context3(video_codec);
       fmt->video_codec = AV_CODEC_ID_H264;
       video_st.enc = codecCtx;
    
       codecCtx->codec_id = fmt->video_codec;
       codecCtx->bit_rate = 400000;
       codecCtx->width  = 1080;
       codecCtx->height = 720;
       codecCtx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
       codecCtx->level = 31;
    
       video_st.st->time_base = (AVRational){ 1, fps };
       codecCtx->time_base = video_st.st->time_base;
       codecCtx->gop_size = 4;
       codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
    
       //open video codec
       codecCtx->extradata_size = 24;
       codecCtx->extradata = (uint8_t *)av_malloc(codecCtx->extradata_size);
       uint8_t extra_data_array[] = { 0x01, SPS[1], SPS[2], SPS[3], 0xFF, 0xE1, 0xc0, 0, 0x42, 0x40, 0x1F, 0x96, 0x54, 0x02, 0x80, 0x2D, 0xD0, 0x0F, 0x39, 0xEA, 0x03, 0xCE, 0x38, 0x80 };
       memcpy(codecCtx->extradata, extra_data_array, codecCtx->extradata_size);
    
       AVCodecContext *c = video_st.enc;
       AVDictionary *opt = NULL;
       avcodec_open2(c, video_codec, &opt);
       avcodec_parameters_from_context(video_st.st->codecpar, c);
    
       //open output file
       avio_open(&formatCtx->pb, filename, AVIO_FLAG_WRITE);
    
       //write header
       int res = avformat_write_header(formatCtx, NULL);
    
       //write frames
    
       // get the frames from file
       uint32_t u32frameCnt = 0;
    
       do
       {
          int8_t i8frame_name[64] = "";
    
          uint8_t  *pu8framePtr = NULL;
          AVPacket pkt = { 0 };
    
          av_init_packet(&pkt);
          sprintf(i8frame_name, "frames/frame%d.bin", u32frameCnt++);
          //reading frames from files
          FILE *ph264Frame = fopen(i8frame_name, "r");
          if(NULL == ph264Frame)
          {
             goto leave;
          }
    
          //get file size
          fseek(ph264Frame, 0L, SEEK_END);
          uint32_t u32file_size = 0;
          u32file_size = ftell(ph264Frame);
          fseek(ph264Frame, 0L, SEEK_SET);
    
          pu8framePtr = malloc(u32file_size);
          uint32_t u32readout = fread(pu8framePtr, 1, u32file_size, ph264Frame);
    
          //if the read frame is a key frame i.e. nalu hdr type = 5 set it as a key frame
          if(0x65 == pu8framePtr[4])
          {
             pkt.flags = AV_PKT_FLAG_KEY;
          }
          pkt.data = (uint8_t *)pu8framePtr;
          pkt.size = u32readout;
          pkt.pts  = u32frameCnt;
          pkt.dts  = pkt.pts;
    
          av_packet_rescale_ts(&pkt, c->time_base, video_st.st->time_base);
          pkt.stream_index = video_st.st->index;
          av_interleaved_write_frame(formatCtx, &pkt);
          free(pu8framePtr);
          fclose(ph264Frame);
       }
       while(1);
    leave:
    
       av_write_trailer(formatCtx);
       av_dump_format(formatCtx, 0, filename, 1);
       avcodec_free_context(&video_st.enc);
       avio_closep(&formatCtx->pb);
       avformat_free_context(formatCtx);
    }
    

    It can be compiled with the following command line (after adding headers):

    gcc file.c -o test_app -I/usr/local/include -L/usr/local/lib -lxcb-shm -lxcb -lX11 -lx264 -lm -lz -pthread -lswresample -lswscale -lavcodec -lavformat -lavdevice -lavutil
    

    The files that are read are valid annexB stream (valid as in it's playable in vlc after concatenating into file) it is a Constrained Baseline 3.1 profile H264 and it comes from an IPcam's interleaved RTCP/RTP stream (demuxed)

    The result is ... well I don't see the picture. I get only black screen with the progress bar and timer running. I don't know if I do something wrong with setting up the codecs and streams, or it's just wrong timestamps.
    I know I got them wrong in some manner but I don't understand that fully yet (how to calculate the correct presentation times), i.e. the stream and the codec both contain time_base field, and then I know that the sample rate of the video is 90kHz and the frame rate is 5 fps

    On top of it all the examples I've found have to some extend deprecated parts that change the flow/meaning of the application and that doesn't help at all so thus If anyone could help I would appreciate it (I think not only me I would guess)

    Regards, Pawel