Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • ffmpeg is reading SDP from RTSP stream but unable to save a screenshot. is it network or utility issue ?

    8 juillet 2017, par Paul Serikov

    My task is to get a screenshot from IP camera rtsp stream via ffmpeg. I got following error when I'm trying to do that on DigitalOcean droplet:

    root@docker-512mb-fra1-01:~# ffmpeg -hide_banner -loglevel debug -i rtsp://10.132.193.9//ch0.h264 -f image2 latest.jpg
    Splitting the commandline.
    Reading option '-hide_banner' ... matched as option 'hide_banner' (do not show program banner) with argument '1'.
    Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.
    Reading option '-i' ... matched as input url with argument 'rtsp://10.132.193.9//ch0.h264'.
    Reading option '-f' ... matched as option 'f' (force format) with argument 'image2'.
    Reading option 'latest.jpg' ... matched as output url.
    Finished splitting the commandline.
    Parsing a group of options: global .
    Applying option hide_banner (do not show program banner) with argument 1.
    Applying option loglevel (set logging level) with argument debug.
    Successfully parsed a group of options.
    Parsing a group of options: input url rtsp://10.132.193.9//ch0.h264.
    Successfully parsed a group of options.
    Opening an input file: rtsp://10.132.193.9//ch0.h264.
    [rtsp @ 0x1298440] SDP:
    v=0
    o=- 1499314217993040 1 IN IP4 192.168.1.128
    s=H.264 Program Stream, streamed by the LIVE555 Media Server
    i=ch0.h264
    t=0 0
    a=DevVer:pusher2
    a=GroupName:IPCAM
    a=NickName:CIF
    a=CfgSection:PROG_CHN0
    a=tool:LIVE555 Streaming Media v2011.08.13
    a=type:broadcast
    a=control:*
    a=range:npt=0-
    a=x-qt-text-nam:H.264 Program Stream, streamed by the LIVE555 Media Server
    a=x-qt-text-inf:ch0.h264
    m=video 0 RTP/AVP 96
    c=IN IP4 0.0.0.0
    b=AS:4000
    a=rtpmap:96 H264/90000
    a=control:trackID=1
    a=fmtp:96 packetization-mode=1;profile-level-id=64001F;sprop-parameter-sets=Z2QAH6wrUCgC3IA=,aO48MA==
    a=framesize:96 1280-720
    a=cliprect:0,0,1280,720
    m=audio 0 RTP/AVP 97
    a=rtpmap:97 mpeg4-generic/8000/2
    a=fmtp:97 streamtype=5;profile-level-id=1;cpresent=0;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1590
    a=control:trackID=2
    
    Failed to parse interval end specification ''
    [rtsp @ 0x1298440] video codec set to: h264
    [rtsp @ 0x1298440] RTP Packetization Mode: 1
    [rtsp @ 0x1298440] RTP Profile IDC: 64 Profile IOP: 0 Level: 1f
    [rtsp @ 0x1298440] Extradata set to 0x1298a20 (size: 23)
    [rtsp @ 0x1298440] audio codec set to: aac
    [rtsp @ 0x1298440] audio samplerate set to: 8000
    [rtsp @ 0x1298440] audio channels set to: 2
    [udp @ 0x129e7e0] end receive buffer size reported is 131072
    [udp @ 0x129e680] end receive buffer size reported is 131072
    [udp @ 0x12bf380] end receive buffer size reported is 131072
    [udp @ 0x12bf1c0] end receive buffer size reported is 131072
    [rtsp @ 0x1298440] hello state=0
    [rtsp @ 0x1298440] UDP timeout, retrying with TCP
    [rtsp @ 0x1298440] hello state=0
    [rtsp @ 0x1298440] Could not find codec parameters for stream 0 (Video: h264, 1 reference frame, none(left), 1280x720, 1/180000): unspecified pixel format
    Consider increasing the value for the 'analyzeduration' and 'probesize' options
    Input #0, rtsp, from 'rtsp://10.132.193.9//ch0.h264':
      Metadata:
        title           : H.264 Program Stream, streamed by the LIVE555 Media Server
        comment         : ch0.h264
      Duration: N/A, start: 0.000000, bitrate: N/A
        Stream #0:0, 0, 1/90000: Video: h264, 1 reference frame, none(left), 1280x720, 1/180000, 90k tbr, 90k tbn, 180k tbc
        Stream #0:1, 0, 1/8000: Audio: aac, 8000 Hz, stereo, fltp
    Successfully opened the file.
    Parsing a group of options: output url latest.jpg.
    Applying option f (force format) with argument image2.
    Successfully parsed a group of options.
    Opening an output file: latest.jpg.
    Successfully opened the file.
    detected 1 logical cores
    [graph 0 input from stream 0:0 @ 0x1298280] Setting 'video_size' to value '1280x720'
    [graph 0 input from stream 0:0 @ 0x1298280] Setting 'pix_fmt' to value '-1'
    [buffer @ 0x12f9680] Unable to parse option value "-1" as pixel format
    [graph 0 input from stream 0:0 @ 0x1298280] Setting 'time_base' to value '1/90000'
    [graph 0 input from stream 0:0 @ 0x1298280] Setting 'pixel_aspect' to value '0/1'
    [graph 0 input from stream 0:0 @ 0x1298280] Setting 'sws_param' to value 'flags=2'
    [graph 0 input from stream 0:0 @ 0x1298280] Setting 'frame_rate' to value '180000/2'
    [buffer @ 0x12f9680] Unable to parse option value "-1" as pixel format
    [buffer @ 0x12f9680] Error setting option pix_fmt to value -1.
    [graph 0 input from stream 0:0 @ 0x1298280] Error applying options to the filter.
    Error opening filters!
    Exiting normally, received signal 2.
    

    As you see, ffmpeg is able to read SDP metadata, but for some reason is unable to save a screenshot

    Also same command works fine on my laptop with same VPN configuration!

    Just in case, IP camera doesn't have a public IP address and accessible via VPN.

    What could be wrong and how to debug?

    I tried to increase -analyzeduration and -probesize options from default 5s to 30s, but it doesn't work.

  • Deploy application with accord.video.ffmpeg c#

    8 juillet 2017, par Alex Gimondi

    I'm using accord.video.ffmpeg 3.4.0, downloaded via nuget, to save a video with videorecorder class. If I compile (x86) my solution the application is running smoothly in visual studio, instead if I publish the app (same architecture ) when I try to run the setup.exe it installs the program but suddenly crashes. The problem is in the constructor of a class when I do new videorecorder(). It is like If during the deployment process not all the libraries needed are included. Anyonw knows which libraries are missing? Thank, Alex

  • subprocess "TypeError : a bytes-like object is required, not 'str'" (Python 2 -> 3 issue maybe ?)

    8 juillet 2017, par chatbottest

    I'm using this code from a previously asked question a few years ago, however I believe this is outdated. Trying to run the code, I receive the error above. I'm still a novice in python, so I could not get much clarification from similar questions. Anyone know why this is happening?

    import subprocess
    
    def getLength(filename):
      result = subprocess.Popen(["ffprobe", filename],
        stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
      return [x for x in result.stdout.readlines() if "Duration" in x]
    
    print(getLength('bell.mp4'))
    

    Traceback

    Traceback (most recent call last):
      File "B:\Program Files\ffmpeg\bin\test3.py", line 7, in 
        print(getLength('bell.mp4'))
      File "B:\Program Files\ffmpeg\bin\test3.py", line 6, in getLength
        return [x for x in result.stdout.readlines() if "Duration" in x]
      File "B:\Program Files\ffmpeg\bin\test3.py", line 6, in 
        return [x for x in result.stdout.readlines() if "Duration" in x]
    TypeError: a bytes-like object is required, not 'str'
    
  • why is ffmpeg so fast

    8 juillet 2017, par jx.xu

    I have written a ffmpeg-based C++ program about converting yuv to rgb using libswscale, similar to the official example. Just simple copy and modify the official example before build in my visual studio 2017 on windows 10. However, the time performance is much slower than the ffmpeg.exe executable file, as 36 seconds vs 12 seconds. I already know that ffmpeg uses some optimization techniques like SIMD instructions. While in my performance profiling, the bottleneck is disk I/O writing which takes at least 2/3 time. Then I develop a concurrent version where a dedicated thread will handle all I/O task while the situation doesn't seem to improve. It's worth noting that I use Boost C++ library to utilize multi-thread and asynchronous events.

    So, I just wanna know how can I modify program using the libraries of ffmpeg or the time performance gap towards ffmpeg.exe just can' be catched up.

    As requested by friendly answers, I post my codes. Compiler is msvc in vs2017 and I turn on the full optimization /Ox .

    Supplement my main question, I make another plain disk I/O test merely copying the file of the same size. It's surprising to find that plain sequential disk I/O costs 28 seconds while the front codes cost 36 seconds in total... Any one knows how can ffmpeg finishes the same job in only 12 seconds? That must use some optimization techniques, like random disk I/O or memory buffer reusing?

    #include "stdafx.h"
    #define __STDC_CONSTANT_MACROS
    extern "C" {
    #include imgutils.h>
    #include parseutils.h>
    #include swscale.h>
    }
    
    #ifdef _WIN64
    #pragma comment(lib, "avformat.lib")
    #pragma comment(lib, "avcodec.lib")
    #pragma comment(lib, "avutil.lib")
    #pragma comment(lib, "swscale.lib")
    #endif
    
    #include cite.hpp>   // just include headers of c++ STL/Boost
    
    int main(int argc, char **argv)
    {
        chrono::duration period;
        auto pIn = fopen("G:/Panorama/UHD/originalVideos/DrivingInCountry_3840x1920_30fps_8bit_420_erp.yuv", "rb");
        auto time_mark = chrono::steady_clock::now();
    
        int src_w = 3840, src_h = 1920, dst_w, dst_h;
        enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_YUV420P, dst_pix_fmt = AV_PIX_FMT_RGB24;
        const char *dst_filename = "G:/Panorama/UHD/originalVideos/out.rgb";
        const char *dst_size = "3840x1920";
        FILE *dst_file;
        int dst_bufsize;
        struct SwsContext *sws_ctx;
        int i, ret;
        if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
            fprintf(stderr,
                "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
                dst_size);
            exit(1);
        }
        dst_file = fopen(dst_filename, "wb");
        if (!dst_file) {
            fprintf(stderr, "Could not open destination file %s\n", dst_filename);
            exit(1);
        }
        /* create scaling context */
        sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
            dst_w, dst_h, dst_pix_fmt,
            SWS_BILINEAR, NULL, NULL, NULL);
        if (!sws_ctx) {
            fprintf(stderr,
                "Impossible to create scale context for the conversion "
                "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
                av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
                av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
            ret = AVERROR(EINVAL);
            exit(1);
        }
        io_service srv;   // Boost.Asio class
        auto work = make_shared(srv); 
        thread t{ bind(&io_service::run,&srv) }; // I/O worker thread
        vector> result;
        /* utilize function class so that lambda can capture itself */
        function)> recursion;  
        recursion = [&](int left, unique_future writable)   
        {
            if (left <= 0)
            {
                writable.wait();
                return;
            }
            uint8_t *src_data[4], *dst_data[4];
            int src_linesize[4], dst_linesize[4];
            /* promise-future pair used for thread synchronizing so that the file part is written in the correct sequence  */
            promise sync; 
            /* allocate source and destination image buffers */
            if ((ret = av_image_alloc(src_data, src_linesize,
                src_w, src_h, src_pix_fmt, 16)) < 0) {
                fprintf(stderr, "Could not allocate source image\n");
            }
            /* buffer is going to be written to rawvideo file, no alignment */
            if ((ret = av_image_alloc(dst_data, dst_linesize,
                dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
                fprintf(stderr, "Could not allocate destination image\n");
            }
            dst_bufsize = ret;
            fread(src_data[0], src_h*src_w, 1, pIn);
            fread(src_data[1], src_h*src_w / 4, 1, pIn);
            fread(src_data[2], src_h*src_w / 4, 1, pIn);
            result.push_back(async([&] {
                /* convert to destination format */
                sws_scale(sws_ctx, (const uint8_t * const*)src_data,
                    src_linesize, 0, src_h, dst_data, dst_linesize);
                if (left>0)
                {
                    assert(writable.get() == true);
                    srv.post([=]
                    {
                        /* write scaled image to file */
                        fwrite(dst_data[0], 1, dst_bufsize, dst_file);
                        av_freep((void*)&dst_data[0]);
                    });
                }
                sync.set_value(true);
                av_freep(&src_data[0]);
            }));
            recursion(left - 1, sync.get_future());
        };
    
        promise root;
        root.set_value(true);
        recursion(300, root.get_future());  // .yuv file only has 300 frames
        wait_for_all(result.begin(), result.end()); // wait for all unique_future to callback
        work.reset();    // io_service::work releses
        srv.stop();      // io_service stops
        t.join();        // I/O thread joins
        period = steady_clock::now() - time_mark;  // calculate valid time
        fprintf(stderr, "Scaling succeeded. Play the output file with the command:\n"
            "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
            av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h, dst_filename);
        cout << "period" << et << period << en;
    end:
        fclose(dst_file);
        //  av_freep(&src_data[0]);
        //  av_freep(&dst_data[0]);
        sws_freeContext(sws_ctx);
        return ret < 0;
    }
    
  • Python : What's the fastest way to load a video file into memory ?

    8 juillet 2017, par michaelh

    First some background

    I am trying to write my own set of tools for video analysis, mainly for detecting render errors like flashing frames and possibly some other stuff in the future.

    The (obvious) goal is to write a script, that is faster and more accurate than me watching the file in real time.

    Using OpenCV, I have something that looks like this:

    import cv2
    
    
    vid = cv2.VideoCapture("Video/OpenCV_Testfile.mov", cv2.CAP_FFMPEG)
    width = 1024
    height = 576
    length = vid.get(cv2.CAP_PROP_FRAME_COUNT)
    
    for f in range(length):
        blue_values = []
        vid.set(cv2.CAP_PROP_POS_FRAMES, f)
        is_read, frame = vid.read()
        if is_read:
            for row in range(height):
                for col in range(width):
                    blue_values.append(frame[row][col][0])
        print(blue_values)
    vid.release()
    

    This just prints out a list of all blue values of every frame. - Just for simplicity (My actual script compares a few values across each frame and only saves the frame number when all are equal)

    Although this works, it is not a very fast operation. (Nested loops, but most important, the read() method has to be called for every frame, which is rather slow. I tried to use multiprocessing but basically ended up having the same crashes as described here:

    how to get frames from video in parallel using cv2 & multiprocessing in python

    I have a 20s long 1024x576@25fps Testfile which performs as follows:

    • mov, ProRes: 15s
    • mp4, h.264: 30s (too slow)

    My machine is capable of playing back h.264 in 1920x1080@50fps with mplayer (which uses ffmpeg to decode). So, I should be able to get more out of this. Which leads me to

    my Question

    How can I decode a video and simply dump all pixel values into a list for further (possibly multithreaded) operations? Speed is really all that matters. Note: I'm not fixated on OpenCV. Whatever works best.

    Thanks!