Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Difference behaviour between Ubuntu and Windows for popen, _popen

    30 avril, par Humam Helfawi

    I need to open FFMPEG pipe and write frames data to that pipe. I am using this code:

    #ifdef _WIN32
    #define POPEN _popen
    #define PCLOSE _pclose
    #else
    #define POPEN popen
    #define PCLOSE pclose
    #endif
    
    // FFmpeg command to receive raw BGR24 data and encode with libx264
    std::ostringstream oss;
    oss << "ffmpeg -y -f rawvideo -pixel_format bgr24 "
        << "-video_size " << width << "x" << height << " "
        << "-framerate " << fps << " -i - "
        << "-c:v libx264 -preset ultrafast "
        << "" << video_path << "";
    std::string cmd = oss.str();        
    #ifdef _WIN32
    auto mode = "wb";
    #else
    auto mode = "w";
    #endif
    pipe_ = POPEN(cmd.c_str(), mode);
    if (!pipe_) {
        throw std::runtime_error("[FFmpegWriter::FFmpegWriter] FFmpeg pipe could not be oppnened");
    }
    

    The writing (in loop) looks like:

    size_t written = fwrite(img.data, 1, img.total() * img.elemSize(), pipe_);
    

    This works fine but it is much slower on Ubuntu (while writing frames) than it is on Windows. The wb option does not work on ubuntu and return empty pipe_. What exactly the problem here:

    1. Why wb does not work on Ubuntu?
    2. if w is the way to go on Ubuntu, why it is much slower than windows (2-3X)?
  • FFmpeg webcam image capture from default Windows webcam

    29 avril, par Mark Ryan

    I want to capture a webcam image from my default Windows webcam without specifying the webcam name and save it as a JPG. I also want to scale and crop it to 640x480. I have come up with -

    ffmpeg -f dshow -i "video=EasyCamera" -vf scale=-1:480,crop=640:480 -frames:v 1 test.jpg -y

    And it works fine, but I want to be able to be able to capture the image without having to specify the camera name. Is this possible?

    I have been able to do it with the following -

    ffmpeg.exe -f vfwcap -i 0 -vf scale=-1:480,crop=640:480 -frames:v 1 test.jpg -y

    But vfwcap is very old & I shouldn't be using it.

    Sorry if this is a very basic question but I am new to FFmpeg and have spent a long time try to figure this out. Any help would be greatly appreciated

  • Can't set seeker in GSTREAMER cv2, python

    29 avril, par Alperen Ölçer

    I want to skip n seconds forward and backward in gstreamer cv2 capture for recorded videos. But when I use cap_gstreamer.set(cv2.CAP_PROP_POS_FRAMES, fps*skip_second) it resets seeker to beginning of video. How can I solve it? I wrote an example, used recorded clock video.

    import cv2
    
    video_p = '/home/alperenlcr/Videos/clock.mp4'
    
    cap_gstreamer = cv2.VideoCapture(video_p, cv2.CAP_GSTREAMER)
    cap_ffmpeg = cv2.VideoCapture(video_p, cv2.CAP_FFMPEG)
    
    fps = cap_gstreamer.get(cv2.CAP_PROP_FPS)
    skip_second = 100
    
    im1 = cv2.resize(cap_gstreamer.read()[1], (960, 540))
    im1_ffmpeg = cv2.resize(cap_ffmpeg.read()[1], (960, 540))
    
    cap_gstreamer.set(cv2.CAP_PROP_POS_FRAMES, fps*skip_second)
    cap_ffmpeg.set(cv2.CAP_PROP_POS_FRAMES, fps*skip_second)
    
    im2 = cv2.resize(cap_gstreamer.read()[1], (960, 540))
    im2_ffmpeg = cv2.resize(cap_ffmpeg.read()[1], (960, 540))
    
    merge_gstreamer = cv2.hconcat([im1, im2])
    merge_ffmpeg = cv2.hconcat([im1_ffmpeg, im2_ffmpeg])
    
    cv2.imshow(str(skip_second) + ' gstreamer', merge_gstreamer)
    cv2.imshow(str(skip_second) + ' ffmpeg', merge_ffmpeg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    cap_gstreamer.release()
    cap_ffmpeg.release()
    
    

    Result: enter image description here

    My cv2 build is like:

    >>> print(cv2.getBuildInformation())
    
    General configuration for OpenCV 4.8.1 =====================================
      Version control:               4.8.1-dirty
    
      Extra modules:
        Location (extra):            /home/alperenlcr/SourceInstalls/opencv_contrib/modules
        Version control (extra):     4.8.1
    
      Platform:
        Timestamp:                   2024-12-02T13:44:58Z
        Host:                        Linux 6.8.0-49-generic x86_64
        CMake:                       3.22.1
        CMake generator:             Unix Makefiles
        CMake build tool:            /usr/bin/gmake
        Configuration:               RELEASE
    
      CPU/HW features:
        Baseline:                    SSE SSE2 SSE3
          requested:                 SSE3
        Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
          requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
          SSE4_1 (18 files):         + SSSE3 SSE4_1
          SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
          FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
          AVX (8 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
          AVX2 (37 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
          AVX512_SKX (8 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
    
      C/C++:
        Built as dynamic libs?:      NO
        C++ standard:                11
        C++ Compiler:                /usr/bin/c++  (ver 10.5.0)
        C++ flags (Release):         -fsigned-char -ffast-math -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
        C++ flags (Debug):           -fsigned-char -ffast-math -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
        C Compiler:                  /usr/bin/cc
        C flags (Release):           -fsigned-char -ffast-math -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
        C flags (Debug):             -fsigned-char -ffast-math -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
        Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
        Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
        ccache:                      NO
        Precompiled headers:         NO
        Extra dependencies:          /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/x86_64-linux-gnu/libtiff.so /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/x86_64-linux-gnu/libharfbuzz.so Iconv::Iconv m pthread cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/lib/x86_64-linux-gnu -L/usr/lib/cuda/lib64
        3rdparty dependencies:       libprotobuf ade ittnotify libwebp libopenjp2 IlmImf quirc ippiw ippicv
    
      OpenCV modules:
        To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
        Disabled:                    cudacodec world
        Disabled by dependency:      -
        Unavailable:                 alphamat cvv hdf java julia matlab ovis python2 sfm viz
        Applications:                tests perf_tests examples apps
        Documentation:               NO
        Non-free algorithms:         NO
    
      GUI:                           GTK2
        QT:                          NO
        GTK+:                        YES (ver 2.24.33)
          GThread :                  YES (ver 2.72.4)
          GtkGlExt:                  NO
        OpenGL support:              NO
        VTK support:                 NO
    
      Media I/O: 
        ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
        JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
        WEBP:                        build (ver encoder: 0x020f)
        PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
        TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.3.0)
        JPEG 2000:                   build (ver 2.5.0)
        OpenEXR:                     build (ver 2.3.0)
        HDR:                         YES
        SUNRASTER:                   YES
        PXM:                         YES
        PFM:                         YES
    
      Video I/O:
        DC1394:                      NO
        FFMPEG:                      YES
          avcodec:                   YES (58.134.100)
          avformat:                  YES (58.76.100)
          avutil:                    YES (56.70.100)
          swscale:                   YES (5.9.100)
          swresample:                YES (3.9.100)
        GStreamer:                   YES (1.20.3)
        v4l/v4l2:                    YES (linux/videodev2.h)
    
      Parallel framework:            TBB (ver 2021.5 interface 12050)
    
      Trace:                         YES (with Intel ITT)
    
      Other third-party libraries:
        Intel IPP:                   2021.8 [2021.8.0]
               at:                   /home/alperenlcr/SourceInstalls/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
        Intel IPP IW:                sources (2021.8.0)
                  at:                /home/alperenlcr/SourceInstalls/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
        VA:                          NO
        Lapack:                      NO
        Eigen:                       NO
        Custom HAL:                  NO
        Protobuf:                    build (3.19.1)
        Flatbuffers:                 builtin/3rdparty (23.5.9)
    
      NVIDIA CUDA:                   YES (ver 11.5, CUFFT CUBLAS NVCUVID NVCUVENC FAST_MATH)
        NVIDIA GPU arch:             86
        NVIDIA PTX archs:
    
      cuDNN:                         YES (ver 8.6.0)
    
      OpenCL:                        YES (no extra features)
        Include path:                /home/alperenlcr/SourceInstalls/opencv/3rdparty/include/opencl/1.2
        Link libraries:              Dynamic load
    
      ONNX:                          NO
    
      Python 3:
        Interpreter:                 /usr/bin/python3 (ver 3.10.12)
        Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.10.so (ver 3.10.12)
        numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.21.5)
        install path:                lib/python3.10/dist-packages/cv2/python-3.10
    
      Python (for build):            /usr/bin/python3
    
      Java:                          
        ant:                         NO
        Java:                        NO
        JNI:                         NO
        Java wrappers:               NO
        Java tests:                  NO
    
      Install to:                    /usr/local
    -----------------------------------------------------------------
    
    
  • How to add dynamic captions with animations on a video in a cross-platform Android & iOS app and export it as a final video ? [closed]

    29 avril, par Danial Qsk

    I'm building a cross-platform mobile app using Flutter (or React Native) where users can:

    Add dynamic text captions with animations over a video, similar to TikTok

    See these changes in real-time during video playback

    Export the video with the animated captions burned into the output (e.g., MP4)

    What I’ve implemented so far:

    Video playback using video_player in Flutter (or react-native-video).

    Captions are rendered using Canvas/UI layer during playback.

    Problem: While I can overlay animated text in the UI, I can't figure out how to include these in the exported video. I’m considering using FFmpeg to burn in the captions, but I’m unclear how to integrate it with the animated UI overlays or render them into a final video on both Android and iOS.

    What I want to achieve:

    Efficient way to render animated text overlays onto a video and export it as a final video file on both Android and iOS.

    Maintain good performance for real-time playback and smooth export.

    What approaches or processing pipelines could achieve this? (For example, rendering the UI layer to an offscreen video, integrating with FFmpeg, or using native rendering techniques.)

  • ffmpeg : Stream specifiers per format/codec [closed]

    29 avril, par nathan wurtz

    I was wondering if it were possible to select all the stream in a specific format.

    Example :

      Stream #0:0(eng): Video: h264 (High), yuv420p(tv, smpte170m/smpte170m/bt709, progressive), 710x480 [SAR 32:27 DAR 142:81], SAR 841:710 DAR 841:480, 29.97 fps, 29.97 tbr, 1k tbn, 180k tbc (default)
      Stream #0:1(deu): Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s (default)
      Stream #0:2(deu): Subtitle: dvd_subtitle, 720x480
      Stream #0:3(eng): Subtitle: dvd_subtitle, 720x480
      Stream #0:4(fra): Subtitle: dvd_subtitle, 720x480
      Stream #0:5(spa): Subtitle: dvd_subtitle, 720x480
      Stream #0:6(eng): Subtitle: subrip (default)
    

    I would like to select all the dvd_subtitle to set the 'codec' to copy. And select the subrip subtitle to set the 'codec' to mov_text. And all that without having to specify the stream number (I would like to reuse the same arguments to convert other videos)

    I thought about doing something like that : -c:m:dvd_subtitle copy -c:m:subrip mov_text, but that obviously didn't work.

    I have read the doc https://ffmpeg.org/ffmpeg.html#Stream-specifiers but didn't find a solution.

    Thank you for reading