Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Vulnerability in file processing [closed]

    3 août, par alex

    I have discovered a vulnerability in the file processing service. If I create an MPD file with a link inside it, then when processing with ffmpeg, an attacker can receive a request to their server, which can lead to serious security issues. Once they know the IP address, they can launch an attack. I'm not quite sure how to deal with this. Are there any libraries for analysing files for malicious content? Yes, I thought about restricting the container's network interface or running file processing services in a sandbox, but I still know very little about combating malicious code in files, how to find it, how to secure my application? Because I am sure that you can add a link to any file, and do something even worse. But I don't know how to deal with it.

  • Can not add tmcd stream using libavcodec to replicate behavior of ffmpeg -timecode option

    2 août, par Sailor Jerry

    I'm trying to replicate option of command line ffmpeg -timecode in my C/C++ code. For some reasons the tcmd stream is not written to the output file. However the av_dump_format shows it in run time

    Here is my minimal test

    #include 
    extern "C" {
    #include avcodec.h>
    #include avformat.h>
    #include avutil.h>
    #include swscale.h>
    #include opt.h>
    #include imgutils.h>
    #include samplefmt.h>
    }
    bool checkProResAvailability() {
      const AVCodec* codec = avcodec_find_encoder_by_name("prores_ks");
      if (!codec) {
        std::cerr << "ProRes codec not available. Please install FFmpeg with ProRes support." << std::endl;
        return false;
      }
      return true;
    }
    
    int main(){
      av_log_set_level(AV_LOG_INFO);
    
      const char* outputFileName = "test_tmcd.mov";
      AVFormatContext* formatContext = nullptr;
      AVCodecContext* videoCodecContext = nullptr;
    
      if (!checkProResAvailability()) {
        return -1;
      }
    
      std::cout << "Creating test file with tmcd stream: " << outputFileName << std::endl;
    
      // Allocate the output format context
      if (avformat_alloc_output_context2(&formatContext, nullptr, "mov", outputFileName) < 0) {
        std::cerr << "Failed to allocate output context!" << std::endl;
        return -1;
      }
    
      if (avio_open(&formatContext->pb, outputFileName, AVIO_FLAG_WRITE) < 0) {
        std::cerr << "Failed to open output file!" << std::endl;
        avformat_free_context(formatContext);
        return -1;
      }
    
      // Find ProRes encoder
      const AVCodec* videoCodec = avcodec_find_encoder_by_name("prores_ks");
      if (!videoCodec) {
        std::cerr << "Failed to find the ProRes encoder!" << std::endl;
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      // Video stream setup
      AVStream* videoStream = avformat_new_stream(formatContext, nullptr);
      if (!videoStream) {
        std::cerr << "Failed to create video stream!" << std::endl;
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      videoCodecContext = avcodec_alloc_context3(videoCodec);
      if (!videoCodecContext) {
        std::cerr << "Failed to allocate video codec context!" << std::endl;
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      videoCodecContext->width = 1920;
      videoCodecContext->height = 1080;
      videoCodecContext->pix_fmt = AV_PIX_FMT_YUV422P10;
      videoCodecContext->time_base = (AVRational){1, 30}; // Set FPS: 30
      videoCodecContext->bit_rate = 2000000;
    
      if (avcodec_open2(videoCodecContext, videoCodec, nullptr) < 0) {
        std::cerr << "Failed to open ProRes codec!" << std::endl;
        avcodec_free_context(&videoCodecContext);
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      if (avcodec_parameters_from_context(videoStream->codecpar, videoCodecContext) < 0) {
        std::cerr << "Failed to copy codec parameters to video stream!" << std::endl;
        avcodec_free_context(&videoCodecContext);
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      videoStream->time_base = videoCodecContext->time_base;
    
      // Timecode stream setup
      AVStream* timecodeStream = avformat_new_stream(formatContext, nullptr);
      if (!timecodeStream) {
        std::cerr << "Failed to create timecode stream!" << std::endl;
        avcodec_free_context(&videoCodecContext);
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      timecodeStream->codecpar->codec_type = AVMEDIA_TYPE_DATA;
      timecodeStream->codecpar->codec_id = AV_CODEC_ID_TIMED_ID3;
      timecodeStream->codecpar->codec_tag = MKTAG('t', 'm', 'c', 'd'); // Timecode tag
      timecodeStream->time_base = (AVRational){1, 30}; // FPS: 30
    
      if (av_dict_set(&timecodeStream->metadata, "timecode", "00:00:30:00", 0) < 0) {
        std::cerr << "Failed to set timecode metadata!" << std::endl;
        avcodec_free_context(&videoCodecContext);
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      // Write container header
      if (avformat_write_header(formatContext, nullptr) < 0) {
        std::cerr << "Failed to write file header!" << std::endl;
        avcodec_free_context(&videoCodecContext);
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      // Encode a dummy video frame
      AVFrame* frame = av_frame_alloc();
      if (!frame) {
        std::cerr << "Failed to allocate video frame!" << std::endl;
        avcodec_free_context(&videoCodecContext);
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      frame->format = videoCodecContext->pix_fmt;
      frame->width = videoCodecContext->width;
      frame->height = videoCodecContext->height;
    
      if (av_image_alloc(frame->data, frame->linesize, frame->width, frame->height, videoCodecContext->pix_fmt, 32) < 0) {
        std::cerr << "Failed to allocate frame buffer!" << std::endl;
        av_frame_free(&frame);
        avcodec_free_context(&videoCodecContext);
        avio_close(formatContext->pb);
        avformat_free_context(formatContext);
        return -1;
      }
    
      // Fill frame with black
      memset(frame->data[0], 0, frame->linesize[0] * frame->height); // Y plane
      memset(frame->data[1], 128, frame->linesize[1] * frame->height / 2); // U plane
      memset(frame->data[2], 128, frame->linesize[2] * frame->height / 2); // V plane
    
      // Encode the frame
      AVPacket packet;
      av_init_packet(&packet);
      packet.data = nullptr;
      packet.size = 0;
    
      if (avcodec_send_frame(videoCodecContext, frame) == 0) {
        if (avcodec_receive_packet(videoCodecContext, &packet) == 0) {
          packet.stream_index = videoStream->index;
          av_interleaved_write_frame(formatContext, &packet);
          av_packet_unref(&packet);
        }
      }
    
      av_frame_free(&frame);
    
      // Write a dummy packet for the timecode stream
      AVPacket tmcdPacket;
      av_init_packet(&tmcdPacket);
      tmcdPacket.stream_index = timecodeStream->index;
      tmcdPacket.flags |= AV_PKT_FLAG_KEY;
      tmcdPacket.data = nullptr; // Empty packet for timecode
      tmcdPacket.size = 0;
      tmcdPacket.pts = 0; // Set necessary PTS
      tmcdPacket.dts = 0;
      av_interleaved_write_frame(formatContext, &tmcdPacket);
    
      // Write trailer
      if (av_write_trailer(formatContext) < 0) {
        std::cerr << "Failed to write file trailer!" << std::endl;
      }
    
      av_dump_format(formatContext, 0, "test.mov", 1);
    
      // Cleanup
      avcodec_free_context(&videoCodecContext);
      avio_close(formatContext->pb);
      avformat_free_context(formatContext);
    
      std::cout << "Test file with timecode created successfully: " << outputFileName << std::endl;
    
      return 0;
    }
    

    The code output is:

    Creating test file with tmcd stream: test_tmcd.mov
    [prores_ks @ 0x11ce05790] Autoselected HQ profile to keep best quality. It can be overridden through -profile option.
    [mov @ 0x11ce04f20] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
    [mov @ 0x11ce04f20] Encoder did not produce proper pts, making some up.
    Output #0, mov, to 'test.mov':
      Metadata:
        encoder         : Lavf61.7.100
      Stream #0:0: Video: prores (HQ) (apch / 0x68637061), yuv422p10le, 1920x1080, q=2-31, 2000 kb/s, 15360 tbn
      Stream #0:1: Data: timed_id3 (tmcd / 0x64636D74)
          Metadata:
            timecode        : 00:00:30:00
    Test file with timecode created successfully: test_tmcd.mov
    

    The ffprobe output is:

    $ ffprobe  test_tmcd.mov
    ffprobe version 7.1.1 Copyright (c) 2007-2025 the FFmpeg developers
      built with Apple clang version 16.0.0 (clang-1600.0.26.6)
      configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.1.1_3 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
      libavutil      59. 39.100 / 59. 39.100
      libavcodec     61. 19.101 / 61. 19.101
      libavformat    61.  7.100 / 61.  7.100
      libavdevice    61.  3.100 / 61.  3.100
      libavfilter    10.  4.100 / 10.  4.100
      libswscale      8.  3.100 /  8.  3.100
      libswresample   5.  3.100 /  5.  3.100
      libpostproc    58.  3.100 / 58.  3.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test_tmcd.mov':
      Metadata:
        major_brand     : qt  
        minor_version   : 512
        compatible_brands: qt  
        encoder         : Lavf61.7.100
      Duration: N/A, start: 0.000000, bitrate: N/A
      Stream #0:0[0x1]: Video: prores (HQ) (apch / 0x68637061), yuv422p10le, 1920x1080, 15360 tbn (default)
          Metadata:
            handler_name    : VideoHandler
            vendor_id       : FFMP
    $ 
    
    

    Spent hours with all AI models, no help. Appeal to the human intelligence now

  • ffmpeg libx264 settings to keep exact colors [closed]

    2 août, par vinni

    does anyone have an idea how I need to edit these ffmpeg settings to keep the original colors? I'm trying to convert a video, but need to absolutely keep the rgb colors, as it's going to be embedded into a website with a background color.

    The parameters look currently like this:

    '-acodec aac -ac 2 -ab 160k  '
    '-vcodec libx264 -preset slow -profile:v baseline -level 25 '
    '-maxrate 10000000 -bufsize 10000000 -vb 1200k -f mp4 '
    '-threads 0'
    

    Thanks!

  • Stream klv and video using ffmpeg [closed]

    31 juillet, par Dennis Jansky

    I have a python program that launches an ffmpeg process where the process has two inputs 1. stdin (rawvideo) and 2. udp port (klv).

    klv_sender.py

    try:
       while True:
         sock.sendto(packet, (localhost, 4444))
    except BrokenPipeError:
      pass
    

    ffmpeg command:

    ffmpeg -loglevel verbose -f rawvideo -vcodec rawvideo -pix_fmt bgr24 -s 704x480 -r 30 -i - -f data -i udp://0.0.0.0:4444 -map 0:v -c:v libx264 -preset ultrafast -tune zerolatency -map 1:d -c:d copy -f mpegts localhost:1234?pkt_size=1316
    

    I see data coming from klv on port 4444 and video data is being sent but, for some reason, I only get klv data. It seems like something blocks so that only klv data is getting sent. If I remove klv from the command video is fine

    Any thoughts as to why one process seems to be blocking? I have sent the sleep time to 1/30 for both streams. The loglevel outputs messages indicating video/klv are received but only 2 packets of video and like 1200 klv packets.

    Thanks!

  • How can I generate a metadata.mov file for higher-resolution Live Photos (e.g. 1440x2560) ?

    30 juillet, par brijesh patel

    I'm generating Live Photos programmatically for use as wallpapers on iOS. I'm using a known metadata.mov file bundled with the app (likely extracted from a working Live Photo with resolution 1080x1920). This setup works fine when I use a video of the same resolution.

    I'm using this open-source library to handle the video-to-LivePhoto conversion:
    https://github.com/TouSC/Video2LivePhoto

    However, when I try using a higher-resolution video (e.g. 2560x1440) to avoid black bars on high-resolution devices (like iPhone 14 Pro Max), the Photos app shows the Live Photo, but the motion component doesn't work—it just says “Motion Not Available.”

    I believe the issue is that the static metadata.mov contains resolution-specific metadata, which prevents it from working correctly with other video sizes.

    • Tried changing the resolution of the video (e.g. 1440x2560, 1284x2778) – motion breaks.

    • Tried generating a new .mov file using FFmpeg with a silent video track, matching the new resolution – Live Photo not recognized or shows errors.

    • Tried modifying the existing metadata.mov with tools like FFmpeg, AtomicParsley, Bento4, and mp4box – resulting files often break the Live Photo entirely.

    • I expected to generate a valid metadata.mov (or similar track) that would support the custom resolution and restore Live Photo motion support.

    static func convertVideo(videoURL: URL, complete: @escaping (_ success: Bool, _ errorMessage: String?) -> Void) {
        print("start converting")
        
        guard let metaURL = Bundle.main.url(forResource: "metadata", withExtension: "mov") else {
            complete(false, "metadata.mov not found")
            return
        }
    
        let livePhotoSize = CGSize(width: 1440, height: 2560) // <-- updated resolution
        let livePhotoDuration = CMTime(value: 550, timescale: 600)
        let assetIdentifier = UUID().uuidString
    
        guard let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else {
            complete(false, "Document path not found")
            return
        }
    
        let durationPath = documentPath + "/duration.mp4"
        let acceleratePath = documentPath + "/accelerate.mp4"
        let resizePath = documentPath + "/resize.mp4"
        let finalPath = resizePath
    
        removeFileIfExists(at: durationPath)
        removeFileIfExists(at: acceleratePath)
        removeFileIfExists(at: resizePath)
    
        let converter = Converter4Video(path: finalPath)
    
        Task {
            do {
                try await converter.durationVideo(at: videoURL, outputPath: durationPath, targetDuration: 3)
                try await converter.accelerateVideo(at: durationPath, to: livePhotoDuration, outputPath: acceleratePath)
                try await converter.resizeVideo(at: acceleratePath, outputPath: resizePath, outputSize: livePhotoSize)
    
                print("### resize Success")
                let image = try await generateCGImage(finalPath: finalPath)
    
                await generateLivePhoto(
                    image: image,
                    documentPath: documentPath,
                    assetIdentifier: assetIdentifier,
                    metaURL: metaURL,
                    converter: converter,
                    complete: complete
                )
            } catch {
                print("Video conversion error: \(error)")
                complete(false, error.localizedDescription)
                return
            }
        }
    }