Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • ffmpeg unable to convert vhs-captured .ts files [closed]

    4 avril, par fredko

    I'm digitizing vhs tapes with a Hauppauge Colossus capture card and Mediaportal on windows 7. They're captured as .ts files and the .ts files play well, with no sign of corruption.

    But I'd like to convert them to .mkv (losslessly from the .ts), and ffmpeg (version 2023-08-28-git-b5273c619d-essentials_build-www.gyan.dev) fails at this. I use

    ffmpeg.exe -i test.ts -c copy test.mkv
    

    and get these errors repeated many times

    [mpegts @ 00000000003d6d40] Packet corrupt (stream = 0, dts = 46105).
    [mpegts @ 00000000003d6d40] Packet corrupt (stream = 0, dts = 49107).
    [h264 @ 00000000003fc580] non-existing PPS 0 referenced
        Last message repeated 1 times
    [h264 @ 00000000003fc580] decode_slice_header error
    [h264 @ 00000000003fc580] no frame!
    ...etc...
    [in#0/mpegts @ 00000000003d6b80] corrupt input packet in stream 0
    [mpegts @ 00000000003d6d40] Packet corrupt (stream = 0, dts = 247305).
    ...etc...
    

    The resulting mkv file is much smaller than the .ts, and displays solid black in mpc-hc. Ffprobe gives the same errors and ends with

    Input #0, mpegts, from 'test.ts':
      Duration: 00:24:26.80, start: 0.099956, bitrate: 4486 kb/s
      Program 137 
      Stream #0:0[0x30]: Video: h264 (Main) (HDMV / 0x564D4448), yuv420p(top first),
     720x480 [SAR 10:11 DAR 15:11], 29.97 fps, 29.97 tbr, 90k tbn
      Stream #0:1[0x40]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo,
     fltp, 188 kb/s
    

    Ffmpeg seems to be complaining about corruption, though again the .ts files play fine. Is there some way to use ffmpeg to convert these files to mkv? Or is the problem with the capture setup?

  • FFMPEG demuxer seek error in Chrome range slider with AWS S3 audio file

    4 avril, par Tania Rascia

    I'm encountering an issue where if you click or slide enough on a range slider in Chrome, it will eventually stop working and give this error, an error 2 (network error):

    PIPELINE_ERROR_READ: FFmpegDemuxer: demuxer seek failed
    

    If I google this error, I only find the Chrome source code:

    Line 1749 of Chrome source code

    First, this issue only happens in Chrome, not Firefox. Second, I can only get it to happen with an encrypted file from AWS, which I can't get into a sandbox, so the sandbox I made will never encounter that error with the random audio file I used.

    Here's the sandbox.

    The only difference I can find between this code and the failing code is the source of the audio file (AWS S3).

  • ffmpeg decoding mixing frames

    4 avril, par Paulo Morgado

    I'm using FFmpeg.AutoGen in a .NET 8.0 application for decoding H264 and JPEG.

    Every usage uses its own instance (there are no shared objects) and there isn't simultanous use of each object.

    However, they can be used from different threads.

    But I'm getting the encoded results with mixed data from all sources. It happens mostly for the same origin codeec, but not esclusivelly.

    I have a base class that looks like this:

    public abstract unsafe class Decoder : IDisposable
    {
        protected readonly object sync = new();
        protected AVCodecContext* codecContext;
        protected AVFrame* frame;
        protected AVPacket* packet;
        protected SwsContext* swsContext;
        private bool disposed = false;
    
        protected Decoder(AVCodecID codecId, ChannelWriter<(ArraySegment decodedData, int width, int height)> writer)
        {
            Writer = writer;
            var codec = ffmpeg.avcodec_find_decoder(codecId);
            codecContext = ffmpeg.avcodec_alloc_context3(codec);
            ffmpeg.avcodec_open2(codecContext, codec, null);
    
            frame = ffmpeg.av_frame_alloc();
            packet = ffmpeg.av_packet_alloc();
        }
    ...
    

    This is the H264 decoding code:

    public override void Decode(ReadOnlySpan data)
    {
        CheckDisposed();
    
        lock (sync)
        {
            fixed (byte* pData = data)
            {
                ffmpeg.av_packet_unref(packet);
    
                packet->data = pData;
                packet->size = data.Length;
    
                var result = ffmpeg.avcodec_send_packet(codecContext, packet);
                if (result < 0 && result != ffmpeg.AVERROR(ffmpeg.EAGAIN))
                {
                    EvalResult(result);
                }
    
                while (true)
                {
                    ffmpeg.av_frame_unref(frame);
    
                    result = ffmpeg.avcodec_receive_frame(codecContext, frame);
                    if (result == ffmpeg.AVERROR(ffmpeg.EAGAIN))
                    {
                        // Need more input data, continue sending packets
                        return;
                    }
                    else if (result < 0)
                    {
                        EvalResult(result);
                    }
    
                    var width = frame->width;
                    var height = frame->height;
    
                    if (swsContext == null)
                    {
                        swsContext = ffmpeg.sws_getContext(
                            width, height, (AVPixelFormat)frame->format,
                            width, height, AVPixelFormat.AV_PIX_FMT_BGRA,
                            ffmpeg.SWS_BILINEAR, null, null, null);
                    }
    
                    var bgraFrame = ffmpeg.av_frame_alloc();
                    bgraFrame->format = (int)AVPixelFormat.AV_PIX_FMT_BGRA;
                    bgraFrame->width = width;
                    bgraFrame->height = height;
                    ffmpeg.av_frame_get_buffer(bgraFrame, 32);
    
                    ffmpeg.sws_scale(
                        swsContext,
                        frame->data, frame->linesize, 0, height,
                        bgraFrame->data, bgraFrame->linesize);
    
                    var bgraSize = width * height * 4;
                    var bgraArray = ArrayPool.Shared.Rent(bgraSize);
                    var bgraSegment = new ArraySegment(bgraArray, 0, bgraSize);
    
                    fixed (byte* pBGRAData = bgraSegment.Array)
                    {
                        var data4 = new byte_ptr4();
                        data4.UpdateFrom(bgraFrame->data.ToArray());
    
                        var linesize4 = new int4();
                        linesize4.UpdateFrom(bgraFrame->linesize.ToArray());
    
                        ffmpeg.av_image_copy_to_buffer(
                            pBGRAData, bgraSize,
                            data4, linesize4,
                            (AVPixelFormat)bgraFrame->format, width, height, 1);
                    }
    
                    ffmpeg.av_frame_free(&bgraFrame);
    
                    Writer.TryWrite((bgraSegment, width, height));
                }
            }
        }
    }
    

    And this is the JPEG decoding code:

    public override void Decode(ReadOnlySpan data)
    {
        CheckDisposed();
    
        lock (sync)
        {
            fixed (byte* pData = data)
            {
                ffmpeg.av_packet_unref(packet);
    
                packet->data = pData;
                packet->size = data.Length;
    
                var result = ffmpeg.avcodec_send_packet(codecContext, packet);
                if (result < 0 && result != ffmpeg.AVERROR(ffmpeg.EAGAIN))
                {
                    EvalResult(result);
                }
    
                while (true)
                {
                    ffmpeg.av_frame_unref(frame);
    
                    result = ffmpeg.avcodec_receive_frame(codecContext, frame);
    
                    if (result == ffmpeg.AVERROR(ffmpeg.EAGAIN))
                    {
                        // Need more input data, continue sending packets
                        return;
                    }
                    else if (result < 0)
                    {
                        EvalResult(result);
                    }
    
                    var width = frame->width;
                    var height = frame->height;
    
                    if (swsContext == null)
                    {
                        swsContext = ffmpeg.sws_getContext(
                            width, height, (AVPixelFormat)frame->format,
                            width, height, AVPixelFormat.AV_PIX_FMT_BGRA,
                            ffmpeg.SWS_BILINEAR, null, null, null);
                    }
    
                    var bgraFrame = ffmpeg.av_frame_alloc();
                    bgraFrame->format = (int)AVPixelFormat.AV_PIX_FMT_BGRA;
                    bgraFrame->width = frame->width;
                    bgraFrame->height = frame->height;
                    ffmpeg.av_frame_get_buffer(bgraFrame, 32);
    
                    ffmpeg.sws_scale(
                        swsContext,
                        frame->data, frame->linesize, 0, frame->height,
                        bgraFrame->data, bgraFrame->linesize);
    
                    int bgraDataSize = bgraFrame->linesize[0] * bgraFrame->height;
                    byte[] bgraData = ArrayPool.Shared.Rent(bgraDataSize);
                    var bgraSegment = new ArraySegment(bgraData, 0, bgraDataSize);
                    Marshal.Copy((IntPtr)bgraFrame->data[0], bgraData, 0, bgraDataSize);
    
                    ffmpeg.av_frame_free(&bgraFrame);
    
                    Writer.TryWrite((bgraSegment, width, height));
                }
            }
        }
    }
    

    What am I doing wrong here?

  • OpenCV FFMPEG RTSP Camera Feed Errors

    4 avril, par trn2020

    I'm getting these errors at random times when saving frames from an rtsp camera feed. The errors happen at different times, usually after 100-200 images have been saved, and the errors themselves are not always exactly the same. They cause the images that are saved at the time of the error to be distorted either to the point of being completely grey or contain distorted pixels.

    #Frame_142 - [hevc @ 0c3bf800] The cu_qp_delta 29 is outside the valid range [-26, 25].

    #Frame_406 - [hevc @ 0b6bdb80] Could not find ref with POC 41

    I've tried implementing the code in both python and c++ with the same result. Also tried saving as .png instead of .jpg. The rtsp feed works fine when using imshow to display the camera, the problem only appears to happen when trying to save the frames. From what I can gather the errors have to do with ffmpeg but google isn't much help for these types of errors.

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace cv;
    
    int main() {
    
        VideoCapture cap("rtsp://admin:admin@192.168.88.97/media/video1");
        if (!cap.isOpened())
            return -1;
    
        for (int i = 0; i < 500; i++)
        {
            Mat frame;
            cap >> frame;
            imwrite("C:\\Users\\Documents\\Dev\\c++\\OpenCVExample\\frames\\frame" + std::to_string(i) + ".png", frame);
            cout << i << "\n";
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
    
        }
    
        return 0;
    }
    
  • FFmpeg WASM Custom build : defining custom flags

    4 avril, par Ettur

    I wish to create custom ffmpeg.wasm build

    Now the official GUIDE show four commands "make dev" "make prd" etc

    So I cloned THE REPO and ran "make prd". It did build, but obviously, this build was with default settings, whatever exactly they made be.

    So pardon my stupidity, as I cannot figure out how / where / what do I edit to set the custom flags about what I want to be included / excluded in the build?