Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • How to use libx264 to encode video in ffmpeg ?

    2 juin 2017, par wzjing

         I want to compress a video to smaller size, and if the video not encode by h264, then I transcode to h264. I have compiled ffmpeg 3.3.2 with libx264, When I using avcodec_send_frame(), it always return an error code, I use av_err2str(ret), and it said "Generic error in an external library" . Is this becouse my code is wrong or my lib is wrong? any way to crrect it or get more details?
         so here is my code of the transcode part, I do encode in the while loop, and the method to encode frame is encodeFrame(), the error happend in the last 3 line in method encodeFrame(), that means avcodec_send_frame(outCxt, outFrame) return whith an error:

    int transcode(){
        ......
    
        /******** Section 1 arguments configure ********/
        AVCodec outCodec = NULL;
        outCodec = avcodec_find_encoder_by_name("libx264");
        //tried avcodec_find_encoder(AV_CODEC_ID_H264) got same error
        if (outCodec == NULL) {
            LOGE("Unable to find encoder libx264");
            exit(1);
        }
        AVCodecContext *outCtx = NULL;
        outCtx = avcodec_alloc_context3(outCodec);
        outCtx->bit_rate = 40000;
        outCtx->width = inCtx->width;
        outCtx->height = inCtx->height;
        outCtx->time_base = (AVRational) {1, 25};
        outCtx->framerate = (AVRational) {25, 1};
        outCtx->gop_size = inCtx->gop_size;
        outCtx->max_b_frames = inCtx->max_b_frames;
        outCtx->pix_fmt = AV_PIX_FMT_YUV420P;
        outCtx->sample_fmt = inCtx->sample_fmt;
        outCtx->sample_rate = inCtx->sample_rate;
        outCtx->frame_size = inCtx->frame_size;
        outCtx->channel_layout = inCtx->channel_layout;
        outCtx->channels = inCtx->channels;
        av_opt_set(outCtx->priv_data, "preset", "slow", 0);
        ret = avcodec_open2(outCtx, outCodec, NULL);
        if (ret < 0)
            error(ret, "Error while inilizing outCtx instanceof AVCodecContext");
    
        /******** Section 2 init AVPacket and AVFrame ********/
        AVPacket *inPacket = NULL;
        inPacket = av_packet_alloc();
        av_init_packet(inPacket);
    
        AVFrame *vFrame;
        vFrame = av_frame_alloc();
        if (!vFrame)
            LOGE("Unable to allocate vFrame");
        vFrame->width = outCtx->width;
        vFrame->height = outCtx->height;
        vFrame->format = outCtx->pix_fmt;
    
        ret = av_frame_get_buffer(vFrame, 32);
        if (ret < 0)
            error(ret, "Unable to execute av_frame_get_buffer");
    
        /******** Section 3 start transcode ********/
        int k = 0;
        while (av_read_frame(inFormatContext, inPacket) == 0) {
            LOGI("--------------------- index:%d -------------------", k);
            resend:
            ret = avcodec_send_packet(inCtx, inPacket);
    
            if (ret == AVERROR(EOF))
                LOGW("> avcodec_send_packet: reache EOF");
            else if (ret == AVERROR(EAGAIN)) {
                LOGW("> avcodec_send_packet: last frame not been read");
                goto resend;
            }
            else if (ret == 0) {
                if (inPacket->stream_index == video_index) {
                    LOGD(">> receiving video frame");
                    avcodec_receive_frame(inCtx, vFrame);
                    encodeFrame(outFormatContext, outCtx, vFrame);
                } else {
                    //Currently, I igonred audio stream for now
                    LOGD(">> receiving audio frame");
                    avcodec_receive_frame(inCtx, aFrame);
                }
                av_packet_unref(inPacket);
                k++;
            } else {
                LOGE("> Error: avcodec_send_packet(%s)", av_err2str(ret));
                exit(1);
            }
    
        }
    
        ......
    }
    
    void encodeFrame(AVFormatContext *outFCxt, AVCodecContext *outCxt, AVFrame *outFrame) {
    
        LOGD(">> Encode Start");
        int ret;
    
        AVPacket *packet;
        packet = av_packet_alloc();
        ret = avcodec_send_frame(outCxt, outFrame);
        switch (ret) {
            case AVERROR(EAGAIN):
                LOGW(">>> avcodec_send_frame(last packet not received yet)");
                av_packet_unref(packet);
                encodeFrame(outFCxt, outCxt, outFrame);
                break;
            case AVERROR(EOF):
                LOGW(">>> avocdec_send_frame(reach end)");
                av_packet_unref(packet);
                break;
            case 0:
                av_frame_unref(outFrame);
                LOGD(">>> avcodec_send_frame(packet send success)");
                while (1){
                    ret = avcodec_receive_packet(outCxt, packet);
                    switch (ret) {
                        case AVERROR(EAGAIN):
                            LOGW(">>>> avcodec_receive_packet(no input)");
                            goto end;
                        case AVERROR(EOF):
                            LOGW(">>>> avcodec_receive_packet(reach end)");
                            goto end;
                        case 0:
                            LOGD(">>>> Writting frame");
                            ret = av_write_frame(outFCxt, packet);
                            if (ret == 0){
                                LOGD(">>>>> Write finished");
                                av_packet_unref(packet);
                            }
                            else {
                                LOGE(">>>>> Error:av_write_frame(%s)", av_err2str(ret));
                                exit(1);
                            }
                            goto end;
                        default:
                            LOGE(">>>> Error:avcode_receive_packet(%s)", av_err2str(ret));
                            exit(1);
    
                    }
                    end:
                    break;
                }
                break;
            default:
                /** I got a "Generic error in an external library" here **/
                LOGE(">>> Error: avcodec_send_frame(%s)", av_err2str(ret));
                exit(1);
        }
    

    }

  • FFMPEG method to decode a MPEG video [closed]

    2 juin 2017, par Sanduni Wickramasinghe

    Can any one tell me which is the method or class that can be used to decode a mpeg video. Here decode mean I need to get the video frames in pixel level. That means to read each frame as an 2D array of integers. I have gone through ffmpeg source code and there are several decode methods. I found avcodec_receive_frame()/avcodec_send_frame() methods which are inside Decode function

  • To detect face in a video file

    2 juin 2017, par Yaswanth Pushpak

    My code is to detect face in a video .but video is not being loaded even I have given the right path . Also i installed ffmpeg but there are no .dll files in it.I am struck here anyone can help me?

    import cv2
    
    import numpy as np
    
    faceDetect=cv2.CascadeClassifier('C:\\OPENCV\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml')
    
    ret,cam=cv2.VideoCapture('D:\\New folder (5)\\New folder (3)\\Ae Dil Hai Mushkil.mp4')
    
    img=cam.read()
    
    while(cam.isOpened()):
    
    ret,img=cam.read();
    
    if not ret: break
    
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
     faces=faceDetect.detectMultiScale(gray,1.3,5);
    
        `enter code here`for (x,y,w,h) in faces:
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        cv2.imshow("Face",img);
        if(cv2.waitKey(1) & 0xFF==ord('q')):
            break;
    
    cam.release()
    cv2.destroyAllWindows()
    

    Error: Traceback (most recent call last): File "D:\New folder (5)\New folder (3)\facedet.py", line 5, in ret,cam=cv2.VideoCapture('D:\New folder (5)\New folder (3)\Ae Dil Hai Mushkil.mp4') TypeError: 'cv2.VideoCapture' object is not iterable

  • Getting Intra-Frames from a video

    2 juin 2017, par Thomas

    First, let me preface this with saying that I have to process a massive amount of videos and shaving even 1 second per video is very significant.

    I'm trying to extract the position of all intra-frames in a video.

    FFProbe works, but it's amazingly slow.

    FFMpeg works much faster, but it's still quite slow.

    So, my question is if there is a lib, where I could do something like this pseudo code:

    var frame = first_frame;
    do
    {
        if (frame.type == intra_frame) log(frame.time);
        frame = frame.next;
    } while (frame != null);
    

    and scan through the video very fast, not looking at anything inside the frames, but just the headers to find the type of the frame and move on to the next.

    The end goal is to generate filmstrips, for preview, as fast as possible. I need to generate 48 thumbnails roughly evenly spread through the movie and covering the whole movie span (excluding 5 sec on each end). I am hoping to speed the process up by extracting only intra-frames. Since I need a fixed number of thumbnails, I can only do this by knowing the total amount of intra-frames.

    This scenario works only if the time to find all the intra-frame indices and extract 48 intra frames is less than getting frames, in a single pass, at regular intervals, 48 times.


    Edit: This is the solution I have found so far; in this example I take one frame every minute

    ffmpeg -hide_banner -skip_frame nokey -i _a.mp4 -vf "select='eq(pict_type\,PICT_TYPE_I)*(lt(abs(t\-30)\,2)+lt(abs(t\-90)\,2)+lt(abs(t\-150)\,2)+lt(abs(t\-180)\,2)+lt(abs(t\-240)\,2)+lt(abs(t\-300)\,2)+lt(abs(t\-360)\,2)+lt(abs(t\-420)\,2))',showinfo,scale=640:480,tile=6x8" -vsync 0 frames.jpg
    

    I found that if I specify the frames directly, I don't really get a speed difference.

    Short of going through the API, is there anything faster with FFMPEG?

  • install ffmpeg on debian gives "unmet dependencies"

    1er juin 2017, par yarek

    I was trying to upgrade my FFMPEG. Now when I run:

    apt-get install ffmpeg

    The following packages have unmet dependencies:
         ffmpeg : Depends: libavcodec57 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libavdevice57 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libavfilter6 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libavformat57 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libavresample3 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libavutil55 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libc6 (>= 2.14) but 2.13-38+deb7u11 is to be installed
                  Depends: libpostproc54 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libsdl2-2.0-0 (>= 2.0.4) but it is not going to be installed
                  Depends: libswresample2 (>= 10:3.3.1) but it is not going to be installed
                  Depends: libswscale4 (>= 10:3.3.1) but it is not going to be installed
         x264 : Depends: libavutil55 (>= 10:3.3.1) but it is not going to be installed
                Depends: libc6 (>= 2.14) but 2.13-38+deb7u11 is to be installed
                Depends: libffms2-4 (>= 1:2.23) but it is not going to be installed
                Depends: libswscale4 (>= 10:3.3.1) but it is not going to be installed
                Depends: libx264-150 but it is not going to be installed
        E: Unable to correct problems, you have held broken packages.