Recherche avancée

Médias (1)

Mot : - Tags -/epub

Autres articles (29)

  • Publier sur MédiaSpip

    13 juin 2013

    Puis-je poster des contenus à partir d’une tablette Ipad ?
    Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir

  • Des sites réalisés avec MediaSPIP

    2 mai 2011, par

    Cette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
    Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page.

  • Contribute to a better visual interface

    13 avril 2011

    MediaSPIP is based on a system of themes and templates. Templates define the placement of information on the page, and can be adapted to a wide range of uses. Themes define the overall graphic appearance of the site.
    Anyone can submit a new graphic theme or template and make it available to the MediaSPIP community.

Sur d’autres sites (6032)

  • Why does adding audio stream to libavcodec output container causes a crash ?

    19 mars 2021, par Sniggerfardimungus

    As it stands, my project correctly uses libavcodec to decode a video, where each frame is manipulated (it doesn't matter how) and output to a new video. I've cobbled this together from examples found online, and it works. The result is a perfect .mp4 of the manipulated frames, minus the audio.

    


    My problem is, when I try to add an audio stream to the output container, I get a crash in mux.c that I can't explain. It's in static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt). Where st->internal->priv_pts->val = pkt->dts; is attempted, priv_pts is nullptr.

    


    I don't recall the version number, but this is from a November 4, 2020 ffmpeg build from git.

    


    My MediaContentMgr is much bigger than what I have here. I'm stripping out everything to do with the frame manipulation, so if I'm missing anything, please let me know and I'll edit.

    


    The code that, when added, triggers the nullptr exception, is called out inline

    


    The .h :

    


    #ifndef _API_EXAMPLE_H&#xA;#define _API_EXAMPLE_H&#xA;&#xA;#include <glad></glad>glad.h>&#xA;#include <glfw></glfw>glfw3.h>&#xA;#include "glm/glm.hpp"&#xA;&#xA;extern "C" {&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>avutil.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;}&#xA;&#xA;#include "shader_s.h"&#xA;&#xA;class MediaContainerMgr {&#xA;public:&#xA;    MediaContainerMgr(const std::string&amp; infile, const std::string&amp; vert, const std::string&amp; frag, &#xA;                      const glm::vec3* extents);&#xA;    ~MediaContainerMgr();&#xA;    void render();&#xA;    bool recording() { return m_recording; }&#xA;&#xA;    // Major thanks to "shi-yan" who helped make this possible:&#xA;    // https://github.com/shi-yan/videosamples/blob/master/libavmp4encoding/main.cpp&#xA;    bool init_video_output(const std::string&amp; video_file_name, unsigned int width, unsigned int height);&#xA;    bool output_video_frame(uint8_t* buf);&#xA;    bool finalize_output();&#xA;&#xA;private:&#xA;    AVFormatContext*   m_format_context;&#xA;    AVCodec*           m_video_codec;&#xA;    AVCodec*           m_audio_codec;&#xA;    AVCodecParameters* m_video_codec_parameters;&#xA;    AVCodecParameters* m_audio_codec_parameters;&#xA;    AVCodecContext*    m_codec_context;&#xA;    AVFrame*           m_frame;&#xA;    AVPacket*          m_packet;&#xA;    uint32_t           m_video_stream_index;&#xA;    uint32_t           m_audio_stream_index;&#xA;    &#xA;    void init_rendering(const glm::vec3* extents);&#xA;    int decode_packet();&#xA;&#xA;    // For writing the output video:&#xA;    void free_output_assets();&#xA;    bool                   m_recording;&#xA;    AVOutputFormat*        m_output_format;&#xA;    AVFormatContext*       m_output_format_context;&#xA;    AVCodec*               m_output_video_codec;&#xA;    AVCodecContext*        m_output_video_codec_context;&#xA;    AVFrame*               m_output_video_frame;&#xA;    SwsContext*            m_output_scale_context;&#xA;    AVStream*              m_output_video_stream;&#xA;    &#xA;    AVCodec*               m_output_audio_codec;&#xA;    AVStream*              m_output_audio_stream;&#xA;    AVCodecContext*        m_output_audio_codec_context;&#xA;};&#xA;&#xA;#endif&#xA;

    &#xA;

    And, the hellish .cpp :

    &#xA;

    #include &#xA;#include &#xA;#include &#xA;#include &#xA;#include &#xA;&#xA;#include "media_container_manager.h"&#xA;&#xA;MediaContainerMgr::MediaContainerMgr(const std::string&amp; infile, const std::string&amp; vert, const std::string&amp; frag,&#xA;    const glm::vec3* extents) :&#xA;    m_video_stream_index(-1),&#xA;    m_audio_stream_index(-1),&#xA;    m_recording(false),&#xA;    m_output_format(nullptr),&#xA;    m_output_format_context(nullptr),&#xA;    m_output_video_codec(nullptr),&#xA;    m_output_video_codec_context(nullptr),&#xA;    m_output_video_frame(nullptr),&#xA;    m_output_scale_context(nullptr),&#xA;    m_output_video_stream(nullptr)&#xA;{&#xA;    // AVFormatContext holds header info from the format specified in the container:&#xA;    m_format_context = avformat_alloc_context();&#xA;    if (!m_format_context) {&#xA;        throw "ERROR could not allocate memory for Format Context";&#xA;    }&#xA;    &#xA;    // open the file and read its header. Codecs are not opened here.&#xA;    if (avformat_open_input(&amp;m_format_context, infile.c_str(), NULL, NULL) != 0) {&#xA;        throw "ERROR could not open input file for reading";&#xA;    }&#xA;&#xA;    printf("format %s, duration %lldus, bit_rate %lld\n", m_format_context->iformat->name, m_format_context->duration, m_format_context->bit_rate);&#xA;    //read avPackets (?) from the avFormat (?) to get stream info. This populates format_context->streams.&#xA;    if (avformat_find_stream_info(m_format_context, NULL) &lt; 0) {&#xA;        throw "ERROR could not get stream info";&#xA;    }&#xA;&#xA;    for (unsigned int i = 0; i &lt; m_format_context->nb_streams; i&#x2B;&#x2B;) {&#xA;        AVCodecParameters* local_codec_parameters = NULL;&#xA;        local_codec_parameters = m_format_context->streams[i]->codecpar;&#xA;        printf("AVStream->time base before open coded %d/%d\n", m_format_context->streams[i]->time_base.num, m_format_context->streams[i]->time_base.den);&#xA;        printf("AVStream->r_frame_rate before open coded %d/%d\n", m_format_context->streams[i]->r_frame_rate.num, m_format_context->streams[i]->r_frame_rate.den);&#xA;        printf("AVStream->start_time %" PRId64 "\n", m_format_context->streams[i]->start_time);&#xA;        printf("AVStream->duration %" PRId64 "\n", m_format_context->streams[i]->duration);&#xA;        printf("duration(s): %lf\n", (float)m_format_context->streams[i]->duration / m_format_context->streams[i]->time_base.den * m_format_context->streams[i]->time_base.num);&#xA;        AVCodec* local_codec = NULL;&#xA;        local_codec = avcodec_find_decoder(local_codec_parameters->codec_id);&#xA;        if (local_codec == NULL) {&#xA;            throw "ERROR unsupported codec!";&#xA;        }&#xA;&#xA;        if (local_codec_parameters->codec_type == AVMEDIA_TYPE_VIDEO) {&#xA;            if (m_video_stream_index == -1) {&#xA;                m_video_stream_index = i;&#xA;                m_video_codec = local_codec;&#xA;                m_video_codec_parameters = local_codec_parameters;&#xA;            }&#xA;            m_height = local_codec_parameters->height;&#xA;            m_width = local_codec_parameters->width;&#xA;            printf("Video Codec: resolution %dx%d\n", m_width, m_height);&#xA;        }&#xA;        else if (local_codec_parameters->codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;            if (m_audio_stream_index == -1) {&#xA;                m_audio_stream_index = i;&#xA;                m_audio_codec = local_codec;&#xA;                m_audio_codec_parameters = local_codec_parameters;&#xA;            }&#xA;            printf("Audio Codec: %d channels, sample rate %d\n", local_codec_parameters->channels, local_codec_parameters->sample_rate);&#xA;        }&#xA;&#xA;        printf("\tCodec %s ID %d bit_rate %lld\n", local_codec->name, local_codec->id, local_codec_parameters->bit_rate);&#xA;    }&#xA;&#xA;    m_codec_context = avcodec_alloc_context3(m_video_codec);&#xA;    if (!m_codec_context) {&#xA;        throw "ERROR failed to allocate memory for AVCodecContext";&#xA;    }&#xA;&#xA;    if (avcodec_parameters_to_context(m_codec_context, m_video_codec_parameters) &lt; 0) {&#xA;        throw "ERROR failed to copy codec params to codec context";&#xA;    }&#xA;&#xA;    if (avcodec_open2(m_codec_context, m_video_codec, NULL) &lt; 0) {&#xA;        throw "ERROR avcodec_open2 failed to open codec";&#xA;    }&#xA;&#xA;    m_frame = av_frame_alloc();&#xA;    if (!m_frame) {&#xA;        throw "ERROR failed to allocate AVFrame memory";&#xA;    }&#xA;&#xA;    m_packet = av_packet_alloc();&#xA;    if (!m_packet) {&#xA;        throw "ERROR failed to allocate AVPacket memory";&#xA;    }&#xA;}&#xA;&#xA;MediaContainerMgr::~MediaContainerMgr() {&#xA;    avformat_close_input(&amp;m_format_context);&#xA;    av_packet_free(&amp;m_packet);&#xA;    av_frame_free(&amp;m_frame);&#xA;    avcodec_free_context(&amp;m_codec_context);&#xA;&#xA;&#xA;    glDeleteVertexArrays(1, &amp;m_VAO);&#xA;    glDeleteBuffers(1, &amp;m_VBO);&#xA;}&#xA;&#xA;&#xA;bool MediaContainerMgr::advance_frame() {&#xA;    while (true) {&#xA;        if (av_read_frame(m_format_context, m_packet) &lt; 0) {&#xA;            // Do we actually need to unref the packet if it failed?&#xA;            av_packet_unref(m_packet);&#xA;            continue;&#xA;            //return false;&#xA;        }&#xA;        else {&#xA;            if (m_packet->stream_index == m_video_stream_index) {&#xA;                //printf("AVPacket->pts %" PRId64 "\n", m_packet->pts);&#xA;                int response = decode_packet();&#xA;                av_packet_unref(m_packet);&#xA;                if (response != 0) {&#xA;                    continue;&#xA;                    //return false;&#xA;                }&#xA;                return true;&#xA;            }&#xA;            else {&#xA;                printf("m_packet->stream_index: %d\n", m_packet->stream_index);&#xA;                printf("  m_packet->pts: %lld\n", m_packet->pts);&#xA;                printf("  mpacket->size: %d\n", m_packet->size);&#xA;                if (m_recording) {&#xA;                    int err = 0;&#xA;                    //err = avcodec_send_packet(m_output_video_codec_context, m_packet);&#xA;                    printf("  encoding error: %d\n", err);&#xA;                }&#xA;            }&#xA;        }&#xA;&#xA;        // We&#x27;re done with the packet (it&#x27;s been unpacked to a frame), so deallocate &amp; reset to defaults:&#xA;/*&#xA;        if (m_frame == NULL)&#xA;            return false;&#xA;&#xA;        if (m_frame->data[0] == NULL || m_frame->data[1] == NULL || m_frame->data[2] == NULL) {&#xA;            printf("WARNING: null frame data");&#xA;            continue;&#xA;        }&#xA;*/&#xA;    }&#xA;}&#xA;&#xA;int MediaContainerMgr::decode_packet() {&#xA;    // Supply raw packet data as input to a decoder&#xA;    // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga58bc4bf1e0ac59e27362597e467efff3&#xA;    int response = avcodec_send_packet(m_codec_context, m_packet);&#xA;&#xA;    if (response &lt; 0) {&#xA;        char buf[256];&#xA;        av_strerror(response, buf, 256);&#xA;        printf("Error while receiving a frame from the decoder: %s\n", buf);&#xA;        return response;&#xA;    }&#xA;&#xA;    // Return decoded output data (into a frame) from a decoder&#xA;    // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c&#xA;    response = avcodec_receive_frame(m_codec_context, m_frame);&#xA;    if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {&#xA;        return response;&#xA;    } else if (response &lt; 0) {&#xA;        char buf[256];&#xA;        av_strerror(response, buf, 256);&#xA;        printf("Error while receiving a frame from the decoder: %s\n", buf);&#xA;        return response;&#xA;    } else {&#xA;        printf(&#xA;            "Frame %d (type=%c, size=%d bytes) pts %lld key_frame %d [DTS %d]\n",&#xA;            m_codec_context->frame_number,&#xA;            av_get_picture_type_char(m_frame->pict_type),&#xA;            m_frame->pkt_size,&#xA;            m_frame->pts,&#xA;            m_frame->key_frame,&#xA;            m_frame->coded_picture_number&#xA;        );&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;&#xA;bool MediaContainerMgr::init_video_output(const std::string&amp; video_file_name, unsigned int width, unsigned int height) {&#xA;    if (m_recording)&#xA;        return true;&#xA;    m_recording = true;&#xA;&#xA;    advance_to(0L); // I&#x27;ve deleted the implmentation. Just seeks to beginning of vid. Works fine.&#xA;&#xA;    if (!(m_output_format = av_guess_format(nullptr, video_file_name.c_str(), nullptr))) {&#xA;        printf("Cannot guess output format.\n");&#xA;        return false;&#xA;    }&#xA;&#xA;    int err = avformat_alloc_output_context2(&amp;m_output_format_context, m_output_format, nullptr, video_file_name.c_str());&#xA;    if (err &lt; 0) {&#xA;        printf("Failed to allocate output context.\n");&#xA;        return false;&#xA;    }&#xA;&#xA;    //TODO(P0): Break out the video and audio inits into their own methods.&#xA;    m_output_video_codec = avcodec_find_encoder(m_output_format->video_codec);&#xA;    if (!m_output_video_codec) {&#xA;        printf("Failed to create video codec.\n");&#xA;        return false;&#xA;    }&#xA;    m_output_video_stream = avformat_new_stream(m_output_format_context, m_output_video_codec);&#xA;    if (!m_output_video_stream) {&#xA;        printf("Failed to find video format.\n");&#xA;        return false;&#xA;    } &#xA;    m_output_video_codec_context = avcodec_alloc_context3(m_output_video_codec);&#xA;    if (!m_output_video_codec_context) {&#xA;        printf("Failed to create video codec context.\n");&#xA;        return(false);&#xA;    }&#xA;    m_output_video_stream->codecpar->codec_id = m_output_format->video_codec;&#xA;    m_output_video_stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    m_output_video_stream->codecpar->width = width;&#xA;    m_output_video_stream->codecpar->height = height;&#xA;    m_output_video_stream->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;    // Use the same bit rate as the input stream.&#xA;    m_output_video_stream->codecpar->bit_rate = m_format_context->streams[m_video_stream_index]->codecpar->bit_rate;&#xA;    m_output_video_stream->avg_frame_rate = m_format_context->streams[m_video_stream_index]->avg_frame_rate;&#xA;    avcodec_parameters_to_context(m_output_video_codec_context, m_output_video_stream->codecpar);&#xA;    m_output_video_codec_context->time_base = m_format_context->streams[m_video_stream_index]->time_base;&#xA;    &#xA;    //TODO(P1): Set these to match the input stream?&#xA;    m_output_video_codec_context->max_b_frames = 2;&#xA;    m_output_video_codec_context->gop_size = 12;&#xA;    m_output_video_codec_context->framerate = m_format_context->streams[m_video_stream_index]->r_frame_rate;&#xA;    //m_output_codec_context->refcounted_frames = 0;&#xA;    if (m_output_video_stream->codecpar->codec_id == AV_CODEC_ID_H264) {&#xA;        av_opt_set(m_output_video_codec_context, "preset", "ultrafast", 0);&#xA;    } else if (m_output_video_stream->codecpar->codec_id == AV_CODEC_ID_H265) {&#xA;        av_opt_set(m_output_video_codec_context, "preset", "ultrafast", 0);&#xA;    } else {&#xA;        av_opt_set_int(m_output_video_codec_context, "lossless", 1, 0);&#xA;    }&#xA;    avcodec_parameters_from_context(m_output_video_stream->codecpar, m_output_video_codec_context);&#xA;&#xA;    m_output_audio_codec = avcodec_find_encoder(m_output_format->audio_codec);&#xA;    if (!m_output_audio_codec) {&#xA;        printf("Failed to create audio codec.\n");&#xA;        return false;&#xA;    }&#xA;

    &#xA;

    I've commented out all of the audio stream init beyond this next line, because this is where&#xA;the trouble begins. Creating this output stream causes the null reference I mentioned. If I&#xA;uncomment everything below here, I still get the null deref. If I comment out this line, the&#xA;deref exception vanishes. (IOW, I commented out more and more code until I found that this&#xA;was the trigger that caused the problem.)

    &#xA;

    I assume that there's something I'm doing wrong in the rest of the commented out code, that,&#xA;when fixed, will fix the nullptr and give me a working audio stream.

    &#xA;

        m_output_audio_stream = avformat_new_stream(m_output_format_context, m_output_audio_codec);&#xA;    if (!m_output_audio_stream) {&#xA;        printf("Failed to find audio format.\n");&#xA;        return false;&#xA;    }&#xA;    /*&#xA;    m_output_audio_codec_context = avcodec_alloc_context3(m_output_audio_codec);&#xA;    if (!m_output_audio_codec_context) {&#xA;        printf("Failed to create audio codec context.\n");&#xA;        return(false);&#xA;    }&#xA;    m_output_audio_stream->codecpar->codec_id = m_output_format->audio_codec;&#xA;    m_output_audio_stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;&#xA;    m_output_audio_stream->codecpar->format = m_format_context->streams[m_audio_stream_index]->codecpar->format;&#xA;    m_output_audio_stream->codecpar->bit_rate = m_format_context->streams[m_audio_stream_index]->codecpar->bit_rate;&#xA;    m_output_audio_stream->avg_frame_rate = m_format_context->streams[m_audio_stream_index]->avg_frame_rate;&#xA;    avcodec_parameters_to_context(m_output_audio_codec_context, m_output_audio_stream->codecpar);&#xA;    m_output_audio_codec_context->time_base = m_format_context->streams[m_audio_stream_index]->time_base;&#xA;    */&#xA;&#xA;    //TODO(P2): Free assets that have been allocated.&#xA;    err = avcodec_open2(m_output_video_codec_context, m_output_video_codec, nullptr);&#xA;    if (err &lt; 0) {&#xA;        printf("Failed to open codec.\n");&#xA;        return false;&#xA;    }&#xA;&#xA;    if (!(m_output_format->flags &amp; AVFMT_NOFILE)) {&#xA;        err = avio_open(&amp;m_output_format_context->pb, video_file_name.c_str(), AVIO_FLAG_WRITE);&#xA;        if (err &lt; 0) {&#xA;            printf("Failed to open output file.");&#xA;            return false;&#xA;        }&#xA;    }&#xA;&#xA;    err = avformat_write_header(m_output_format_context, NULL);&#xA;    if (err &lt; 0) {&#xA;        printf("Failed to write header.\n");&#xA;        return false;&#xA;    }&#xA;&#xA;    av_dump_format(m_output_format_context, 0, video_file_name.c_str(), 1);&#xA;&#xA;    return true;&#xA;}&#xA;&#xA;&#xA;//TODO(P2): make this a member. (Thanks to https://emvlo.wordpress.com/2016/03/10/sws_scale/)&#xA;void PrepareFlipFrameJ420(AVFrame* pFrame) {&#xA;    for (int i = 0; i &lt; 4; i&#x2B;&#x2B;) {&#xA;        if (i)&#xA;            pFrame->data[i] &#x2B;= pFrame->linesize[i] * ((pFrame->height >> 1) - 1);&#xA;        else&#xA;            pFrame->data[i] &#x2B;= pFrame->linesize[i] * (pFrame->height - 1);&#xA;        pFrame->linesize[i] = -pFrame->linesize[i];&#xA;    }&#xA;}&#xA;

    &#xA;

    This is where we take an altered frame and write it to the output container. This works fine&#xA;as long as we haven't set up an audio stream in the output container.

    &#xA;

    bool MediaContainerMgr::output_video_frame(uint8_t* buf) {&#xA;    int err;&#xA;&#xA;    if (!m_output_video_frame) {&#xA;        m_output_video_frame = av_frame_alloc();&#xA;        m_output_video_frame->format = AV_PIX_FMT_YUV420P;&#xA;        m_output_video_frame->width = m_output_video_codec_context->width;&#xA;        m_output_video_frame->height = m_output_video_codec_context->height;&#xA;        err = av_frame_get_buffer(m_output_video_frame, 32);&#xA;        if (err &lt; 0) {&#xA;            printf("Failed to allocate output frame.\n");&#xA;            return false;&#xA;        }&#xA;    }&#xA;&#xA;    if (!m_output_scale_context) {&#xA;        m_output_scale_context = sws_getContext(m_output_video_codec_context->width, m_output_video_codec_context->height, &#xA;                                                AV_PIX_FMT_RGB24,&#xA;                                                m_output_video_codec_context->width, m_output_video_codec_context->height, &#xA;                                                AV_PIX_FMT_YUV420P, SWS_BICUBIC, nullptr, nullptr, nullptr);&#xA;    }&#xA;&#xA;    int inLinesize[1] = { 3 * m_output_video_codec_context->width };&#xA;    sws_scale(m_output_scale_context, (const uint8_t* const*)&amp;buf, inLinesize, 0, m_output_video_codec_context->height,&#xA;              m_output_video_frame->data, m_output_video_frame->linesize);&#xA;    PrepareFlipFrameJ420(m_output_video_frame);&#xA;    //TODO(P0): Switch m_frame to be m_input_video_frame so I don&#x27;t end up using the presentation timestamp from&#xA;    //          an audio frame if I threadify the frame reading.&#xA;    m_output_video_frame->pts = m_frame->pts;&#xA;    printf("Output PTS: %d, time_base: %d/%d\n", m_output_video_frame->pts,&#xA;        m_output_video_codec_context->time_base.num, m_output_video_codec_context->time_base.den);&#xA;    err = avcodec_send_frame(m_output_video_codec_context, m_output_video_frame);&#xA;    if (err &lt; 0) {&#xA;        printf("  ERROR sending new video frame output: ");&#xA;        switch (err) {&#xA;        case AVERROR(EAGAIN):&#xA;            printf("AVERROR(EAGAIN): %d\n", err);&#xA;            break;&#xA;        case AVERROR_EOF:&#xA;            printf("AVERROR_EOF: %d\n", err);&#xA;            break;&#xA;        case AVERROR(EINVAL):&#xA;            printf("AVERROR(EINVAL): %d\n", err);&#xA;            break;&#xA;        case AVERROR(ENOMEM):&#xA;            printf("AVERROR(ENOMEM): %d\n", err);&#xA;            break;&#xA;        }&#xA;&#xA;        return false;&#xA;    }&#xA;&#xA;    AVPacket pkt;&#xA;    av_init_packet(&amp;pkt);&#xA;    pkt.data = nullptr;&#xA;    pkt.size = 0;&#xA;    pkt.flags |= AV_PKT_FLAG_KEY;&#xA;    int ret = 0;&#xA;    if ((ret = avcodec_receive_packet(m_output_video_codec_context, &amp;pkt)) == 0) {&#xA;        static int counter = 0;&#xA;        printf("pkt.key: 0x%08x, pkt.size: %d, counter:\n", pkt.flags &amp; AV_PKT_FLAG_KEY, pkt.size, counter&#x2B;&#x2B;);&#xA;        uint8_t* size = ((uint8_t*)pkt.data);&#xA;        printf("sizes: %d %d %d %d %d %d %d %d %d\n", size[0], size[1], size[2], size[2], size[3], size[4], size[5], size[6], size[7]);&#xA;        av_interleaved_write_frame(m_output_format_context, &amp;pkt);&#xA;    }&#xA;    printf("push: %d\n", ret);&#xA;    av_packet_unref(&amp;pkt);&#xA;&#xA;    return true;&#xA;}&#xA;&#xA;bool MediaContainerMgr::finalize_output() {&#xA;    if (!m_recording)&#xA;        return true;&#xA;&#xA;    AVPacket pkt;&#xA;    av_init_packet(&amp;pkt);&#xA;    pkt.data = nullptr;&#xA;    pkt.size = 0;&#xA;&#xA;    for (;;) {&#xA;        avcodec_send_frame(m_output_video_codec_context, nullptr);&#xA;        if (avcodec_receive_packet(m_output_video_codec_context, &amp;pkt) == 0) {&#xA;            av_interleaved_write_frame(m_output_format_context, &amp;pkt);&#xA;            printf("final push:\n");&#xA;        } else {&#xA;            break;&#xA;        }&#xA;    }&#xA;&#xA;    av_packet_unref(&amp;pkt);&#xA;&#xA;    av_write_trailer(m_output_format_context);&#xA;    if (!(m_output_format->flags &amp; AVFMT_NOFILE)) {&#xA;        int err = avio_close(m_output_format_context->pb);&#xA;        if (err &lt; 0) {&#xA;            printf("Failed to close file. err: %d\n", err);&#xA;            return false;&#xA;        }&#xA;    }&#xA;&#xA;    return true;&#xA;}&#xA;

    &#xA;

  • ffmpeg, download video stream from url

    5 juillet 2021, par samiullah

    I am developing application to save any online webinar(video or audio) given the url using ffmpeg library. Url usually contain other contents like text, images as well. So i have problem, how to separately get video stream from url using ffmpeg(or some other better free library). Url may be for any site, not only for youtube, but as an example, link may be like

    &#xA;

    http://www.youtube.com/watch?v=wnrJJYm7qIw

    &#xA;

  • Unable to play video recorded from OpenAi Environment

    10 mars 2021, par ipro_ultra

    I am developing some Reinforcement Learning algorithms in python and I am trying to record the environment into a video however I am unable to play the file once I have recorded it and it is only 1KB in size, I have tried an example I found online and this didn't work either so I assume it's a system issue rather than a problem with my code, I have enabled the debugging by running gym.logger.set_level(gym.logger.DEBUG) but this doesn't seem to through anything useful back, below is my code

    &#xA;

    def replay_episode(self):&#xA;    self._env.reset()&#xA;    video_recorder = VideoRecorder(self._env, "{}/episode{}.mp4".format(self._output_directory, episode_number), enabled=True)&#xA;    for action_taken in self._current_episode_actions:&#xA;        self._env.render()&#xA;        video_recorder.capture_frame()&#xA;        self._env.step(action_taken)&#xA;    video_recorder.close()&#xA;&#xA;for i in range(solution.get_episode_count()):&#xA;    # AI Stuff&#xA;    episode_number = i &#x2B; 1&#xA;    solution.intialise_episode(episode_number)&#xA;    profiler.enable()&#xA;    while not solution.current_episode_done():&#xA;       solution.run_tick()&#xA;    solution.train_model()&#xA;    profiler.disable()&#xA;    export_profiling_results(profiler, &#x27;{}/episode{}.csv&#x27;.format(solution._output_directory, episode_number))&#xA;&#xA;&#xA;    # This is the bit we&#x27;re intrested in&#xA;    if solution.current_episode_successful() or episode_number % 50 == 0:&#xA;        solution.replay_episode()&#xA;&#xA;    done_time = time.time()&#xA;    print("Episode {} Completed in {}s".format(episode_number, done_time-start_time))&#xA;    start_time = done_time&#xA;&#xA;solution.close()&#xA;

    &#xA;

    Here is the log I get from the environment when the replay_episode() method is called

    &#xA;

    DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Starting ffmpeg with "ffmpeg -nostats -loglevel error -y -f rawvideo -s:v 600x400 -pix_fmt rgb24 -framerate 30 -i - -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -vcodec libx264 -pix_fmt yuv420p &#xA;-r 30 data/1003083349/episode50.mp4"&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Capturing video frame: path=data/1003083349/episode50.mp4&#xA;DEBUG: Closing video encoder: path=data/1003083349/episode50.mp4&#xA;

    &#xA;

    When I attempt to play the video I get the error : This file isn&#x27;t playable. That might be because the file type is unsupported, the file extension is incorrect, or the file is corrupt. 0xc10100be

    &#xA;

    Thank you in advance for any responses

    &#xA;