Newest 'ffmpeg' Questions - Stack Overflow

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

Articles published on the website

  • C# documentation about how to add an event handler [duplicate]

    14 December 2017, by DevFranz_Tech

    This question already has an answer here:

    I am coding a video converter using NRECO and ffmpeg everything works but when it comes at to get the Progress time i've tried

    pgbConversion.Value = FFMpegConverter.ConvertProgress();
    

    but it is impossible so i got ! ** **ConvertProgress is an even; please check C# documentation about how to add an even handler.****

    from NReco please how can i get the video progress knowing that ConvertProgrss is an event

  • C# documentation about how to add an even handler [duplicate]

    14 December 2017, by DevFranz_Tech

    This question already has an answer here:

    I am coding a video converter using NRECO and ffmpeg everything works but when it comes at to get the Progress time i've tried

    pgbConversion.Value = FFMpegConverter.ConvertProgress();
    

    but it is impossible so i got ! ** **ConvertProgress is an even; please check C# documentation about how to add an even handler.****

    from NReco please how can i get the video progress knowing that ConvertProgrss is an event

  • Encode images on the fly into an mp4 video using ffmpeg

    14 December 2017, by Kushagra

    I was wondering if there's a way where I can append images at the end of video. I'm capturing an image every 10th frame and approximately after 10 seconds I want all the images captured encoded into a video. I tried the traditional way of saving all the captured images and then running the //-y -i .../image%1d.jpg -r 30 video.mp4 ffmpeg command but this takes like 15-20 seconds on Android, not tested iOS yet. Wondering if there's a way where I can encode these images on the fly to be part of one single video output ? I've been googling for the last couple of days but none of the solution I've seen so far has worked for me. Also, in all of this, there is no audio involved at all.

    Note : I'm working only with Android and iOS using Unity and I'm looking for high performance encoding.

    If you know of any other codec that can be used to achieve this let me know that as well

  • ffmpeg error while adding text on video

    14 December 2017, by Sid

    I have used 'com.writingminds:FFmpegAndroid:0.3.2' library for using ffmpeg in my app.I want to add text in video.I have added the library and used the methods specified there .Then I'm passing the ffmpeg command as an argument to the execute method .But it shows an error as output file not specified but i have added an output file in the commands.Don't know what is going wrong.

    My code is as follows:

     `FFmpeg fFmpeg = FFmpeg.getInstance (MainActivity.this);
                try {
                    fFmpeg.loadBinary (new LoadBinaryResponseHandler () {
    
                        @Override
                        public void onStart() {
                            Log.e ("onStart", "onStart load");
                        }
    
                        @Override
                        public void onFailure() {
                            Log.e ("onFailure", "onFailure load");
                        }
    
                        @Override
                        public void onSuccess() {
                            Log.e ("onSuccess", "onSuccess load");
                        }
    
                        @Override
                        public void onFinish() {
                            Log.e ("onFinish", "onFinish load");
                        }
                    });
                } catch (FFmpegNotSupportedException e) {
                    e.printStackTrace ();
                    // Handle if FFmpeg is not supported by device
                }
    
                String[] cmd = {"-y",
                        "-i",
                        "/storage/emulated/0/DCIM/Camera/VID_20171212_120337.mp4",
                        "-vf",
                        "format=yuv444p",
                        "-codec:v",
                        "drawtext=text='Title of this Video': fontcolor=white: fontsize=24: x=(w-tw)/2: y=(h/PHI)+th  box=1: boxcolor=black@0.5",
                        "-c:v copy",
                        "outVideo.mp4"
                };
    
                try {
                    fFmpeg.execute (cmd, new ExecuteBinaryResponseHandler () {
    
                        @Override
                        public void onStart() {
                            Log.e ("onStart", "onStart execute");
                        }
    
                        @Override
                        public void onProgress(String message) {
                            Log.e ("onProgress execute", message);
                        }
    
                        @Override
                        public void onFailure(String message) {
                            Log.e ("onFailure execute", message);
                        }
    
                        @Override
                        public void onSuccess(String message) {
                            Log.e ("onSuccess execute", message);
                        }
    
                        @Override
                        public void onFinish() {
                            Log.e ("onFinish execute", "onFinish execute");
                        }
                    });
                } catch (FFmpegCommandAlreadyRunningException e) {
                    e.printStackTrace ();
                    // Handle if FFmpeg is already running
                }`
    

    It shows the following error :

    `onFailure execute: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers built with gcc 4.8 (GCC)
    

    Trailing options were found on the commandline.

                                                                Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DCIM/Camera/VID_20171212_120337.mp4':
                                                                   Metadata:
                                                                     major_brand     : mp42
                                                                     minor_version   : 0
                                                                     compatible_brands: isommp42
                                                                     creation_time   : 2017-12-12 06:33:43
                                                                   Duration: 00:00:04.84, start: 0.000000, bitrate: 8552 kb/s
                                                                     Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 7842 kb/s, SAR 1:1 DAR 16:9, 16.84 fps, 16.67 tbr, 90k tbn, 180k tbc (default)
                                                                     Metadata:
                                                                       rotate          : 90
                                                                       creation_time   : 2017-12-12 06:33:43
                                                                       handler_name    : VideoHandle
                                                                     Side data:
                                                                       displaymatrix: rotation of -90.00 degrees
                                                                     Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
                                                                     Metadata:
                                                                       creation_time   : 2017-12-12 06:33:43
                                                                       handler_name    : SoundHandle
                                                              **At least one output file must be specified**`
    
  • libavformat/libavcodec providing invalid container header

    14 December 2017, by seanr8

    I'm using libavcodec to encode a stream to h264 and libavformat to store it in an mp4. The resulting container has an invalid header that can be played in VLC, but not any other player.

    I've found that using the mp4 container and the "mpeg4" codec produces a valid mp4 file, but using libx265 (HEVC) or the libx264 codec produces invalid mp4s.

    I can use ffmpeg -i invalid.mp4 -vcodec copy valid.mp4 and I get a file of almost the exact same size, but in a valid container.

    Examples of these files are here: Broken file and Repaied file [use the download links in the upper right to examine]

    I used a hex editor to see the differences in the headers of the two files and the invalid one is 1 byte smaller than the valid one.

    The code I'm using to open the container and codec and to write the header is here:

    AVOutputFormat *container_format;
    AVFormatContext *container_format_context;
    AVStream *video_stream;
    int ret;
    
    /* allocate the output media context */
    avformat_alloc_output_context2(&container_format_context, NULL, NULL, out_file);
    if (!container_format_context) {
        log(INFO, "Unable to determine container format from filename, exiting\n");
        exit(1);
    }
    else {
        log(INFO, "Using container %s\n", container_format_context->oformat->name);
    }
    
    if (!container_format_context) {
        log(ERROR, "Could not build container format context. Encoding failed.");
        exit(1);
    }
    
    container_format = container_format_context->oformat;
    
    /* Pull codec based on name */
    AVCodec* codec = avcodec_find_encoder_by_name(codec_name);
    if (codec == NULL) {
        log(ERROR, "Failed to locate codec \"%s\".",
                codec_name);
        exit(1);
    }
    
    /* create stream */
    video_stream = NULL;
    video_stream = avformat_new_stream(container_format_context, codec);
    if (!video_stream) {
        log(ERROR, "Could not allocate encoder stream. Cannot continue.\n");
        exit(1);
    }
    video_stream->id = container_format_context->nb_streams - 1;
    
    video_stream->time_base = video_stream->codec->time_base = (AVRational) { 1, 25};
    
    av_dump_format(container_format_context, 0, out_file, 1);
    
    
    
    /* Retrieve encoding context */
    AVCodecContext* avcodec_context = video_stream->codec;
    if (avcodec_context == NULL) {
        log(ERROR, "Failed to allocate context for "
                "codec \"%s\".", codec_name);
        exit(1);
    }
    
    
    /* Init context with encoding parameters */
    avcodec_context->bit_rate = bitrate;
    avcodec_context->width = width;
    avcodec_context->height = height;
    avcodec_context->gop_size = 10;
    avcodec_context->max_b_frames = 1;
    avcodec_context->qmax = 31;
    avcodec_context->qmin = 2;
    avcodec_context->pix_fmt = AV_PIX_FMT_YUV420P;
    
    av_dump_format(container_format_context, 0, out_file, 1);
    
    /* Open codec for use */
    if (avcodec_open2(avcodec_context, codec, NULL) < 0) {
        log(ERROR, "Failed to open codec \"%s\".", codec_name);
        exit(1);
    }
    
    /* Allocate corresponding frame */
    AVFrame* frame = av_frame_alloc();
    if (frame == NULL) {
        exit(1);
    }
    
    /* Copy necessary data for frame from avcodec_context */
    frame->format = avcodec_context->pix_fmt;
    frame->width = avcodec_context->width;
    frame->height = avcodec_context->height;
    
    /* Allocate actual backing data for frame */
    if (av_image_alloc(frame->data, frame->linesize, frame->width,
                frame->height, frame->format, 32) < 0) {
        exit(1);
    }
    
    /* open the output file, if the container needs it */
    if (!(container_format->flags & AVFMT_NOFILE)) {
        ret = avio_open(&container_format_context->pb, out_file, AVIO_FLAG_WRITE);
        if (ret < 0) {
            log(ERROR, "Error occurred while opening output file: %s\n",
                    av_err2str(ret));
            exit(1);
        }
    }
    
    /* write the stream header, if needed */
    ret = avformat_write_header(container_format_context, NULL);
    if (ret < 0) {
        log(ERROR, "Error occurred while writing output file header: %s\n",
                    av_err2str(ret));
    }
    

    The code to encode a frame is here:

    /* Init video packet */
    AVPacket packet;
    av_init_packet(&packet);
    
    /* Request that encoder allocate data for packet */
    packet.data = NULL;
    packet.size = 0;
    
    /* Write frame to video */
    int got_data;
    if (avcodec_encode_video2(avcontext, &packet, frame, &got_data) < 0) {
        log(WARNING, "Error encoding frame #%" PRId64,
                video_struct->next_pts);
        return -1;
    }
    
    /* Write corresponding data to file */
    if (got_data) {
        if (packet.pts != AV_NOPTS_VALUE) {
            packet.pts = av_rescale_q(packet.pts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
        }
        if (packet.dts != AV_NOPTS_VALUE) {
            packet.dts = av_rescale_q(packet.dts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
        }
        write_packet(video_struct, &packet, packet.size);
        av_packet_unref(&packet);
    }
    

    And the code to write the packet to the video stream:

    static int write_packet(video_struct* video, void* data, int size) {
    
    int ret;
    
    /* use AVStream is not null, otherwise write to output fd */
    AVPacket *pkt = (AVPacket*) data;
    pkt->stream_index = video->output_stream->index;
    ret = av_interleaved_write_frame(video->container_format_context, pkt);
    if (ret != 0) {
        return -1;
    }
    
    /* Data was written successfully */
    return ret;
    }