Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Convert video to HLS in iOS app without triggering GPL (FFmpegKit alternative ?) [closed]

    7 juillet, par Aziz Bibitov

    I'm building an iOS app in Swift that needs to convert local video files to HLS format (.m3u8). Initially, I used the ffmpeg-kit-ios-full-gpl package from FFmpegKit, which works well. However, since this build includes GPL-licensed components (such as libx264), I'm concerned that using it would require my app to be released under the GPL, which is not compatible with App Store distribution.

    That said, my needs are fairly basic: I only need to convert H.264 .mp4 video files into HLS format.

    My Questions:

    1. Is there a safe way to use FFmpegKit—such as the full-libarary-lgpl variant—that guarantees no GPL components are used for this task?
    2. Are there any iOS-native or third-party tools that can reliably convert H.264 .mp4 video files to HLS on-device without introducing GPL concerns?
    3. Is using Apple’s AVAssetExportSession a viable alternative for exporting to HLS? I haven't found much official documentation about using it for HLS output.

    Any guidance on how to perform HLS conversion in an App Store–safe (non-GPL) way would be much appreciated.

  • What codec in OpenCV can do a stronger compression than default 'mp4v' ? [closed]

    7 juillet, par ullix

    My project does video recording from a microscope camera, and one experiment can run for days and even weeks. This takes plenty of filespace, so I want as strong a compression as possible. Quality is not a major concern.

    Currently I use the default 'mp4v' codec with opencv on Python. It works.

    I tried all codecs Google could find, and surprisingly, only very few worked. And those few that did work were even worse in compression.

    Where is the limitation? Is it Opencv (4.11.0.86)? Is it FFMPEG (7.1.1)? Is it the distribution (Ubuntu Mate 25.04)? Is it the CPU (AMD Ryzen AI 9 HX 370 w/ Radeon 890M × 24)?

    EDIT: I have uploaded an example clip (1 min) with some explanations to youtube: https://www.youtube.com/watch?v=wW8w2Pppnng Frames are FullHD, constant 10FPS

    The stripped-down code is:

    import cv2
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out    = cv2.VideoWriter("myvideo.mp4", fourcc, 19,(1920, 1080))
    cam    = cv2.VideoCapture(0)
    # loop
      success, image = cam.read()              
      cv2.imshow(title, image)                 
      key = cv2.waitKey(1)                     
      out.write(image)
    ...
    out.release()
    

    What alternative codec can I use which gives better compression?

  • Is there a way to force FFMPEG to decode a video stream with alpha from ​a WebM video encoded with libvpx-vp9 ?

    6 juillet, par David

    I have a ​WebM file with one video stream that was encoded with VP9 (libvpx-vp9).

    I wrote a C++ program to extract the frames from the video stream and save them out as PNG's. This works fine except that the resulting PNG's are missing alpha.

    If I extract the frames from the same WebM file using FFMPEG the resulting PNG's do contain alpha. Here is the output from FFMPEG:

    $ ffmpeg -c:v libvpx-vp9 -i temp/anim.webm temp/output-%3d.png
    
    [libvpx-vp9 @ 0000024732b106c0] v1.10.0-rc1-11-gcb0d8ce31
        Last message repeated 1 times
    Input #0, matroska,webm, from 'temp/anim.webm':
      Metadata:
        ENCODER         : Lavf58.45.100
      Duration: 00:00:04.04, start: 0.000000, bitrate: 112 kb/s
      Stream #0:0: Video: vp9 (Profile 0), yuva420p(tv), 640x480, SAR 1:1 DAR 4:3, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
        Metadata:
          alpha_mode      : 1
          ENCODER         : Lavc58.91.100 libvpx-vp9
          DURATION        : 00:00:04.040000000
    

    FFMPEG identifies the stream format as yuva420p.

    Here is the output from my program when av_dump_format is called:

    Input #0, matroska,webm, from 'temp/anim.webm':
      Metadata:
        ENCODER         : Lavf58.45.100
      Duration: 00:00:04.04, start: 0.000000, bitrate: 112 kb/s
      Stream #0:0: Video: vp9 (Profile 0), yuv420p(tv), 640x480, SAR 1:1 DAR 4:3, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
        Metadata:
          alpha_mode      : 1
          ENCODER         : Lavc58.91.100 libvpx-vp9
          DURATION        : 00:00:04.040000000
    

    Notice that the detected stream format is yuv420p (the alpha is missing).

    Does anybody know how to force the stream format to use alpha?

    My setup code resembles the following (error handling is omitted)

    auto result = avformat_open_input(&formatContext, fileName.c_str(), nullptr, nullptr);
    auto result = avformat_find_stream_info(formatContext, nullptr);
    streamIndex = av_find_best_stream(formatContext, mediaType, -1, -1, nullptr, 0);
    auto stream = formatContext->streams[streamIndex];
    const auto codecIdentifier{ AV_CODEC_ID_VP9 };
    auto decoder = avcodec_find_decoder(codecIdentifier);
    pCodecContext = avcodec_alloc_context3(decoder);
    auto result = avcodec_open2(pCodecContext, decoder, &options);
    // AV_PIX_FMT_YUV420P - missing alpha
    auto pixelFormat = pCodecContext->pix_fmt;
    

    Gyan pointed out what the problem was. Here is the corrected code:

    In case anybody else runs into this issue in the future here is the code (error handling omitted):

    auto formatContext = avformat_alloc_context();
    formatContext->video_codec_id = AV_CODEC_ID_VP9;
    const auto decoder = avcodec_find_decoder_by_name("libvpx-vp9");
    formatContext->video_codec = decoder;
    avformat_open_input(&formatContext, fileName.c_str(), nullptr, nullptr);
    avformat_find_stream_info(formatContext.get(), nullptr);
    for (unsigned int streamIndex = 0; streamIndex < formatContext->nb_streams; ++streamIndex) {
        // Displayed the stream format as yuva420p (contains alpha)
        av_dump_format(formatContext, static_cast(streamIndex), fileName.toStdString().c_str(), 0);
    }
    

    Thanks,

  • Randomly extract video frames from multiple files [closed]

    5 juillet, par PatraoPedro

    I have a folder with hundreds of video files (*.avi), each one with more or less an hour long. What I would like to achieve is a piece of code that could go through each one of those videos and randomly select two or three frames from each file and then stitch it back together or alternatively save the frames in a folder as jpegs.

    Initially I thought I could do this using R but quickly I've realised that I would need something else possibly working together with R.

    Is it possible to call FFMPEG from R to do the task above?

  • Moviepy write_videofile works the second time but not the first ?

    5 juillet, par Andrew Best

    I'm concatenating a list of video objects together then writing them with write_videofile, weirdly enough the first time I write the file, it plays fine for the first halfish then the first few frames of each clip in the file afterwards plays before freezing. But here's the odd part, If I write the exact same video object right after the first video writes, it writes just fine and plays perfectly.

    Here's my code

    from moviepy.editor import VideoFileClip, concatenate_videoclips
    
    clipslist = []
    clips = ['https://clips-media-assets2.twitch.tv/AT-cm%7C787619651.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787628097.mp4', 'https://clips-media-assets2.twitch.tv/2222789345-offset-20860.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787624765.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787539697.mp4', 'https://clips-media-assets2.twitch.tv/39235981488-offset-3348.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788412970.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787682495.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787962593.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787627256.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787573008.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788543065.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787593688.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788079881.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788707738.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788021727.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787595029.mp4', 'https://clips-media-assets2.twitch.tv/39233367648-offset-9536.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788517651.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788087743.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787497542.mp4', 'https://clips-media-assets2.twitch.tv/39233367648-offset-9154.mp4', 'https://clips-media-assets2.twitch.tv/7109626012888880881-offset-4818.mp4', 'https://clips-media-assets2.twitch.tv/72389234-offset-760.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787774924.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787565708.mp4']
    
    for clip in clips:
        dlclip = VideoFileClip(clip, target_resolution=(1080, 1920))  # Download clip
        clipslist.append(dlclip)
    
    videofile = concatenate_videoclips(clipslist)
    videofile.write_videofile("final1.mp4") # Broken after the first halfish
    videofile.write_videofile("final2.mp4") # Works entirely fine.
    videofile.close
    

    Any ideas? Any suggestions appreciated.

    • Sometimes when the video is small enough it seems to write the first time just fine too.
    • It seems there is no set point where it breaks, each time I write it for the first time it typically breaks at a different spot.
    • I've tried waiting for the thread to exit and sleeping after the concatenation and that doesn't seem to fix the issue.