Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • RTSP streaming to video file using FFMPEG library

    17 août 2017, par Gona

    I want to save video taken with a network camera(IP camera) as a video file like .avi.

    At first I tried OpenCV but I have a codec problem and try to use FFMPEG. This is the first time to use FFMPEG so I am looking for some sample code.

    The overall project structure is C# with a C++ DLL, so I want to save(or write or record) the camera stream as a video file in C++.

    Camera stream is received using RTSP, and the RTSP URL is also known. How to save RTSP stream as video file using FFMPEG library? The codec is H264.

    I would appreciate it if you could show me some sample code.

    My development environment is 64-bit Windows 10 and Visual Studio 2015. I downloaded FFMPEG library version 20170817-92da230, 64-bit architecture and linking both Shared and Dev from here https://ffmpeg.zeranoe.com/builds/

  • Read first 100Kb before ffmpeg finishes conversion

    17 août 2017, par SiWM

    I have mp3 files and need to convert them to webm(or ogg) on the fly. Usually this conversion takes about ~10 seconds but i need to send the first 100Kb so that the sound can start immediately but ffmpeg writes the whole file in one go.

    Is there any way that ffmpeg can write chunks of converted file or are there any tricks to start the playback earlier?

    (The client uses MediaSourceExtensions for audio playback)

  • Seeking with FFmpeg gives old frames with new packet timestamp

    17 août 2017, par Bipul Prasad

    I'm using FFmpeg 2.7 to write a sample application that would do the following:

    1. Read the video packets from the given .mov file
    2. Seek to a specified position after 10 sec of decoded video and continue for another 10 sec
    3. Decode all video packets before and after the seek and write them into a target file.

    In the process, I am also dumping all the video frames received into raw image files, using the PTS of the original packet as the filename. I notice that even after the seek has happened (noted by the jump in timestamp), the first few frames after the seek contain the new timestamp, but the old frames (i.e., frames I would have received without seek).

    These images illustrate the problem.

    The following is a snippet of the code I'm using:

    int SeekAndTranscode::_TranscodePacket(AVPacket &packet, AVFrame *frame)
    {
        if(packet.stream_index == _videoStreamIndex)
        {
            int status = avcodec_decode_video2(&_videoDecContext, frame, &gotFrame, &packet);
            if (status < 0)
            {
                fprintf(stderr, "\nError decoding video frame\n");
                exit(1);
            }
    
            if (gotFrame)
            {
                if (frame->width != _width || frame->height != _height ||
                    frame->format != _pixelFormat)
                {
                    return -1;
                }
    
                vector rgbData;
                rgbData.resize(_width * _height * 4);
    
                uint8_t *rgbSrc[3]= {&rgbData[0], NULL, NULL};
                int rgbStride[3]={4 * _width, 0, 0};
    
                sws_scale(_csc, frame->data, frame->linesize, 0, _height, rgbSrc, rgbStride);
    
                int64_t pts = packet.pts * AV_TIME_BASE / _videoDecContext.time_base.den;
                FILE *file;
                stringstream filename;
                filename << "C:\\Bipul\\" << pts << ".raw";
    
                file = fopen(filename.str().c_str(), "wb");
                fwrite(&rgbData[0], 1, rgbData.size(), file);
                fclose(file);
    
                fwrite(&rgbData[0], 1, rgbData.size(), _videoOutputFile);
            }
        }
    
        return decodedSize;
    }
    
    
    void SeekAndTranscode::TranscodeIntoFile()
    {
        AVFrame* frame = NULL;
        AVPacket packet;
        int videoSeeks = 0;
        int frameCount = 0;
        int framerate = 24;
        try
        {
            frame = av_frame_alloc();
            if (!frame)
            {
                fprintf(stderr, "\nCould not allocate frame for decoding\n");
                exit(1);
            }
    
            av_init_packet(&packet);
            packet.data = NULL;
            packet.size = 0;
    
            while (av_read_frame(_formatContext, &packet) >= 0)
            {
                if(packet.stream_index == _videoStreamIndex)
                {
                    frameCount++;
                    if(frameCount % (10*framerate) == 0)
                    {
                        if(videoSeeks == 1)
                            break;
                        av_seek_frame(_formatContext, -1, _seekPosition * AV_TIME_BASE, 0);
                        videoSeeks++;
                        continue;
                    }
                }
                AVPacket originalPacket = packet;
                do
                {
                    int status = _TranscodePacket(packet, frame);
    
                    if (status < 0)
                        break;
                    packet.data += status;
                    packet.size -= status;
                } while (packet.size > 0);
                av_packet_unref(&originalPacket);
            }
    
            if (frame)
            {
                av_frame_free(&frame);
            }
    
            swr_free(&_resampler);
        }
        catch(const exception& e)
        {
            fprintf(stderr, "\n%s\n", e.what());
            exit(1);
        }
    }
    

    Is this a known issue with this version of FFmpeg? Has this been fixed in later versions? Is there something I'm doing wrong? Any leads will be appreciated.

  • FFmpeg video from file to network

    17 août 2017, par caiomcg

    I am currently working with FFmpeg in a video streaming solution over RTP. What bugs me is how to stream at a correct framerate. Currently I am using a sleep in the main streaming loop:

    while (av_read_frame (input_format_ctx, &packet) >= 0) { // Read a video packet as long as EOF is not reached or an error occurr
            if(packet.stream_index == 0) {
                if (av_interleaved_write_frame(output_format_ctx, &packet) < 0) { // Write the packet to the output stream
                    av_packet_unref(&packet); // Wipe the packet
                    av_free_packet(&packet); // Release the packet and finishes if a problem occured
                    break; 
                }
                av_packet_unref(&packet); // Wipe the packet
                av_free_packet(&packet); // Release the packet
            }
    
            current_time = getSystemTime(); // Get system time
            adaptativeSleep ((last_time + 1/(output_framerate)) - current_time); // Sleep according to framerate
            last_time = getSystemTime(); // Get finish 
        }
    

    Is there a correct way of letting FFmpeg handle the framerate?

    Thanks in advance

    P.S.: I only remux the stream, as a result FFmpeg runs through the file in a few seconds witought my "sleep".

  • Android FFmpeg : get frame count from video

    17 août 2017, par Tix

    I only want to filter out 20% of frames from the start of the video and the end of the video, but i cant do that if i dont have total frame count.

    I alr know how to filter out frames, i just need to know how to get total frame count of a video.

    I've looked this up, and there was answers saying use ffprobe to fetch total number of frames in a video. However ffmpeg for android doesnt have it.. is there another way to get the frame count from a video?

    Compiled ffmpeg for android using:

    compile 'com.writingminds:FFmpegAndroid:0.3.2'