Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Kodi : playback several video files from the internet as single movie with single timeline

    30 mai 2018, par HarryFox

    I'm writing an add-on for Kodi in Python, for a site with movie collection, which can be able to playback online video from the site. But problem is that many of them splitted apart on pieces of different length. And its not mpeg dash nor m3u playlists, its just individual pieces of single movie or an episode.

    My target is able to play those splitted videos as single movie with single timeline and playback will no require download all pieces of a movie. It's important because many of Movies\episodes provides with subtitle file, that why single timeline is important. Also general idea for plugin which I'm writing is comfort to use, so online playback is very important too.

    I did some research but it almost no result. On official Kodi forum advise to create Input Stream add-on like Input Stream Adaptive, but it was written on c++ and it's unattainable for me (for now at least). There is no tools for python to create such kind of add-ons.

    Another idea is to create middle server which on fly will somehow (with ffmpeg) combine those pieces, but it seems that the process will too resource-intensive especially for TV-boxes. In this case also raise many question about which way is better, and i have no enough experience and knowledge to solve the problems by myself even with google.

    So i need an advice how can i solve the problem, just to know right direction. Thank you for your time. Sorry for my English.

  • FFMpeg lib memory leak

    30 mai 2018, par unresolved_external

    I am using lib ffmpeg for video decoding. So in my code I have something like:

    if ((err = av_hwdevice_ctx_create(&m_hw_device_ctx, type,
                                                NULL, NULL, 0)) < 0) 
        {
              printf( "Failed to create specified HW device.\n");
              return err;
            }
    
            m_decoder_ctx->hw_device_ctx = av_buffer_ref(m_hw_device_ctx);
            if ((ret = avcodec_open2(m_decoder_ctx, decoder, NULL)) < 0)
            {
                printf("Failed to open codec for video stream\n");
                return -1;                
            }
            if (0 > avcodec_send_packet(m_decoder_ctx, packet)) 
            {                
                return -1;
            }
        AVFrame *frame = nullptr;
        if (!(frame = av_frame_alloc())) 
        {
             printf( "Can not alloc frame\n");
             return -1;
        }
    
    
    
        int ret = avcodec_receive_frame(avctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) 
        {
            av_frame_free(&frame);
            return 0;
        } 
        else if (ret < 0) 
        {
            printf("Error while decoding\n");
            av_frame_free(&frame);
        }
    //here is some application specific manipulation(ommitted for redability)
      av_frame_free(&frame);
    

    And then I free allocated memory like this:

    av_buffer_unref(&m_decoder_ctx->hw_device_ctx);
                av_buffer_unref(&m_hw_device_ctx);
                m_hw_device_ctx = nullptr;
    

    But Valgrind constantly reports an error:

    ==1115== 524,288 bytes in 1 blocks are definitely lost in loss record 2,147 of 2,174
    ==1115==    at 0x76A67920: ??? (in /usr/lib/x86_64-linux-gnu/libdrm_intel.so.1.0.0)
    ==1115==    by 0x76727A07: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_drv_video.so)
    ==1115==    by 0x76727B2A: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_drv_video.so)
    ==1115==    by 0x766FC2F5: ??? (in /usr/lib/x86_64-linux-gnu/dri/i965_drv_video.so)
    ==1115==    by 0x7670459C: __vaDriverInit_0_39 (in /usr/lib/x86_64-linux-gnu/dri/i965_drv_video.so)
    ==1115==    by 0x144A8AE5: ??? (in /usr/lib/x86_64-linux-gnu/libva.so.1.3900.0)
    ==1115==    by 0x144A9906: vaInitialize (in /usr/lib/x86_64-linux-gnu/libva.so.1.3900.0)
    ==1115==    by 0xD03A55E: ??? (in /usr/local/lib/libavutil.so.56.18.102)
    ==1115==    by 0xD03839C: av_hwdevice_ctx_create (in /usr/local/lib/libavutil.so.56.18.102)
    

    I've been trying to troubleshoot it without any success. Also checked a couple of examples - looks like everyone works with that function the same as I do. (e.g. -https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/vaapi_encode.c) Compiler flag, which I am using are:

    -O0 -g3 -pthread -fpermissive -lm
    

    Any ideas - what is wrong with buffer deallocation?

  • FFMPEG increases bitrate after splitting an avi file

    30 mai 2018, par Yaser Sakkaf

    I am trying to split a video (.avi) file with duration of 1 hour to 8 minutes. I am using the following command

     ffmpeg -i videos_22_05_18/cnbc.avi -ss 00:00:00 -to 00:08:00 -c copy 1.avi
    

    But it makes a video of 37 minutes instead and also the audio quality is too fast.

    I compared the specifications of the original .avi file and the resulting one, and the only difference is that of bitrate.

    Bitrate of original file is 646k while that of splitted file is 1126.

    I have literally tried every thing. like using

    ffmpeg -i videos_22_05_18/cnbc.avi -ss 00:00:00 -to 00:08:00 -c copy -b 646k 1.avi

    AND

    ffmpeg -i videos_22_05_18/cnbc.avi -ss 00:00:00 -to 00:08:00 -c copy -async 1 1.avi

    AND

    ffmpeg -i videos_22_05_18/cnbc.avi -ss 00:00:00 -to 00:08:00 -c copy -b 646k -async 1 1.avi

    AND

    ffmpeg -i videos_22_05_18/cnbc.avi -ss 00:00:00 -to 00:08:00 -vcodec copy -acodec copy -b 646k 1.avi

    AND MANY MORE combinations

    Nothing seems to work I don't know why it is happening like this. PLease help.

  • Adding fade in and out transition between the images while creating video from images with FFmpeg

    30 mai 2018, par BentCoder

    The command below creates a video out of unknown amount images and a background audio. I am looking a way to add fade in and out transition between the images. What should the command be in order to achieve this?

    I have seen the examples in links below but sadly cannot implement it.

    Command

    ffmpeg \
      -pattern_type glob \
      -framerate 1/3 -i "/images/img-*.png" \
      -framerate 1/3 -loop 1 -t 5 -i "/images/background.png" \
      -i "/audios/audio.mp3" \
      -filter_complex \
        "[0:v]scale=854:480,setsar=1[v0]; \
         [1:v]scale=854:480,setsar=1[v1]; \
         [v0][v1]concat=n=2:v=1:a=0,format=yuv420p[v]" \
      -map "[v]" -map 2:a \
      -c:v libx264 \
      -c:a aac \
      -shortest \
      -movflags +faststart \
      "/output/video.mp4"
    

    If you wish the the the output, it is here.

    Note: If it helps, the size of my images and the names are all different. Some big some small and UUID names.

  • H265 codec changes from hvc1 to hev1

    30 mai 2018, par ppcat

    I try to copy mp4 file with hevc video. Source file have hvc1 codec.

    Stream #0:0(und): Video: hevc (Main) (hvc1 / 0x31637668)
    

    After copying the codec gets the value of hev1

    Stream #0:0(und): Video: hevc (Main) (hev1 / 0x31766568)
    

    How can i get hvc1? I found that ffmpeg can do it with "-tag:v hvc1" (https://stackoverflow.com/questions/32152090/encode-h265-to-hvc1-codec) And how to set tag in c++ code (FFMpeg copy streams without transcode) But it does not effect

    My code:

    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
    AVPacket pkt;
    const char *in_filename, *out_filename;
    int ret, i;
    int stream_index = 0;
    int *stream_mapping = NULL;
    int stream_mapping_size = 0;
    
    if (argc < 3) {
        printf("usage: %s input output\n"
            "API example program to remux a media file with libavformat and libavcodec.\n"
            "The output format is guessed according to the file extension.\n"
            "\n", argv[0]);
        return 1;
    }
    
    in_filename = argv[1];
    out_filename = argv[2];
    
    av_register_all();
    
    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
        fprintf(stderr, "Could not open input file '%s'", in_filename);
        goto end;
    }
    
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information");
        goto end;
    }
    
    av_dump_format(ifmt_ctx, 0, in_filename, 0);
    
    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        fprintf(stderr, "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }
    
    stream_mapping_size = ifmt_ctx->nb_streams;
    stream_mapping = (int*)av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
    if (!stream_mapping) {
        ret = AVERROR(ENOMEM);
        goto end;
    }
    
    ofmt = ofmt_ctx->oformat;
    
    for (i = 0; i < (int)ifmt_ctx->nb_streams; i++) {
        AVStream *out_stream;
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVCodecParameters *in_codecpar = in_stream->codecpar;
        AVCodec *decoder = avcodec_find_decoder(in_stream->codecpar->codec_id);
        AVCodec *encoder = avcodec_find_encoder(in_stream->codecpar->codec_id);
    
        if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
            stream_mapping[i] = -1;
            continue;
        }
    
        stream_mapping[i] = stream_index++;
    
        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            goto end;
        }
    
        ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
        if (ret < 0) {
            fprintf(stderr, "Failed to copy codec parameters\n");
            goto end;
        }
    
        unsigned int tag = 0;
        // for ffmpeg new api 3.x
        AVCodecParameters *parameters = out_stream->codecpar;
        if (av_codec_get_tag2(ofmt_ctx->oformat->codec_tag, encoder->id, &tag) == 0) {
            av_log(NULL, AV_LOG_ERROR, "could not find codec tag for codec id %d, default to 0.\n", encoder->id);
        }
        parameters->codec_tag = tag;
        out_stream->codec = avcodec_alloc_context3(encoder);
        // more setting for stream->codec
        avcodec_parameters_to_context(out_stream->codec, parameters);
    }
    av_dump_format(ofmt_ctx, 0, out_filename, 1);
    
    if (!(ofmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open output file '%s'", out_filename);
            goto end;
        }
    }
    
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        goto end;
    }
    
    while (1) {
        AVStream *in_stream, *out_stream;
    
        ret = av_read_frame(ifmt_ctx, &pkt);
        if (ret < 0)
            break;
    
        in_stream = ifmt_ctx->streams[pkt.stream_index];
        if (pkt.stream_index >= stream_mapping_size ||
            stream_mapping[pkt.stream_index] < 0) {
            av_packet_unref(&pkt);
            continue;
        }
    
        pkt.stream_index = stream_mapping[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];
        log_packet(ifmt_ctx, &pkt, "in");
    
        /* copy packet */
        pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
        log_packet(ofmt_ctx, &pkt, "out");
    
        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
        if (ret < 0) {
            fprintf(stderr, "Error muxing packet\n");
            break;
        }
        av_packet_unref(&pkt);
    }
    
    av_write_trailer(ofmt_ctx);
    

    end:

    avformat_close_input(&ifmt_ctx);
    
    /* close output */
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
        avio_closep(&ofmt_ctx->pb);
    avformat_free_context(ofmt_ctx);
    
    av_freep(&stream_mapping);
    
    if (ret < 0 && ret != AVERROR_EOF) {
        fprintf(stderr, "Error occurred: %s\n", av_errTOstr(ret).c_str());
        return 1;
    }
    

    After changes:

        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
    
        AVCodecParameters *parameters = out_stream->codecpar;
        unsigned int tag = 0;
        av_codec_get_tag2(ofmt_ctx->oformat->codec_tag, encoder->id, &tag) == 0);
        parameters->codec_tag = tag;
    
        out_stream->codec = avcodec_alloc_context3(encoder);
        avcodec_parameters_to_context(out_stream->codec, parameters);
    
        if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            parameters->codec_tag = MKTAG('h', 'v', 'c', '1');
        }
    }
    
    av_dump_format(ofmt_ctx, 0, out_filename, 1);
    ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
    
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error write header: %s\n", av_errTOstr(ret). c_str());
        goto end;
    }