Recherche avancée

Médias (3)

Mot : - Tags -/pdf

Autres articles (9)

  • Organiser par catégorie

    17 mai 2013, par

    Dans MédiaSPIP, une rubrique a 2 noms : catégorie et rubrique.
    Les différents documents stockés dans MédiaSPIP peuvent être rangés dans différentes catégories. On peut créer une catégorie en cliquant sur "publier une catégorie" dans le menu publier en haut à droite ( après authentification ). Une catégorie peut être rangée dans une autre catégorie aussi ce qui fait qu’on peut construire une arborescence de catégories.
    Lors de la publication prochaine d’un document, la nouvelle catégorie créée sera proposée (...)

  • Les thèmes de MediaSpip

    4 juin 2013

    3 thèmes sont proposés à l’origine par MédiaSPIP. L’utilisateur MédiaSPIP peut rajouter des thèmes selon ses besoins.
    Thèmes MediaSPIP
    3 thèmes ont été développés au départ pour MediaSPIP : * SPIPeo : thème par défaut de MédiaSPIP. Il met en avant la présentation du site et les documents média les plus récents ( le type de tri peut être modifié - titre, popularité, date) . * Arscenic : il s’agit du thème utilisé sur le site officiel du projet, constitué notamment d’un bandeau rouge en début de page. La structure (...)

  • Déploiements possibles

    31 janvier 2010, par

    Deux types de déploiements sont envisageable dépendant de deux aspects : La méthode d’installation envisagée (en standalone ou en ferme) ; Le nombre d’encodages journaliers et la fréquentation envisagés ;
    L’encodage de vidéos est un processus lourd consommant énormément de ressources système (CPU et RAM), il est nécessaire de prendre tout cela en considération. Ce système n’est donc possible que sur un ou plusieurs serveurs dédiés.
    Version mono serveur
    La version mono serveur consiste à n’utiliser qu’une (...)

Sur d’autres sites (2816)

  • C++ ffmpeg lib version 7.0 - noice in exported audio

    2 septembre 2024, par Chris P

    I want to make a C++ lib named cppdub which will mimic the python module pydub.

    


    One main function is to export the AudioSegment to a file with a specific format (example : mp3).

    


    The code is :

    


    AudioSegment AudioSegment::from_file(const std::string&amp; file_path, const std::string&amp; format, const std::string&amp; codec,&#xA;    const std::map&amp; parameters, int start_second, int duration) {&#xA;&#xA;    avformat_network_init();&#xA;    av_log_set_level(AV_LOG_ERROR); // Adjust logging level as needed&#xA;&#xA;    AVFormatContext* format_ctx = nullptr;&#xA;    if (avformat_open_input(&amp;format_ctx, file_path.c_str(), nullptr, nullptr) != 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not open audio file." &lt;&lt; std::endl;&#xA;        return AudioSegment();  // Return an empty AudioSegment on failure&#xA;    }&#xA;&#xA;    if (avformat_find_stream_info(format_ctx, nullptr) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not find stream information." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    int audio_stream_index = -1;&#xA;    for (unsigned int i = 0; i &lt; format_ctx->nb_streams; i&#x2B;&#x2B;) {&#xA;        if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;            audio_stream_index = i;&#xA;            break;&#xA;        }&#xA;    }&#xA;&#xA;    if (audio_stream_index == -1) {&#xA;        std::cerr &lt;&lt; "Error: Could not find audio stream." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    AVCodecParameters* codec_par = format_ctx->streams[audio_stream_index]->codecpar;&#xA;    const AVCodec* my_codec = avcodec_find_decoder(codec_par->codec_id);&#xA;    AVCodecContext* codec_ctx = avcodec_alloc_context3(my_codec);&#xA;&#xA;    if (avcodec_parameters_to_context(codec_ctx, codec_par) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not initialize codec context." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    if (avcodec_open2(codec_ctx, my_codec, nullptr) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not open codec." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    SwrContext* swr_ctx = swr_alloc();&#xA;    if (!swr_ctx) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate SwrContext." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    av_opt_set_chlayout(swr_ctx, "in_chlayout", &amp;codec_ctx->ch_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "in_sample_rate", codec_ctx->sample_rate, 0);&#xA;    av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", codec_ctx->sample_fmt, 0);&#xA;&#xA;    AVChannelLayout dst_ch_layout;&#xA;    av_channel_layout_copy(&amp;dst_ch_layout, &amp;codec_ctx->ch_layout);&#xA;    av_channel_layout_uninit(&amp;dst_ch_layout);&#xA;    av_channel_layout_default(&amp;dst_ch_layout, 2);&#xA;&#xA;    av_opt_set_chlayout(swr_ctx, "out_chlayout", &amp;dst_ch_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "out_sample_rate", 48000, 0);&#xA;    av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);&#xA;&#xA;    if (swr_init(swr_ctx) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Failed to initialize the resampling context" &lt;&lt; std::endl;&#xA;        swr_free(&amp;swr_ctx);&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    AVPacket packet;&#xA;    AVFrame* frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate frame." &lt;&lt; std::endl;&#xA;        swr_free(&amp;swr_ctx);&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    std::vector<char> output;&#xA;    while (av_read_frame(format_ctx, &amp;packet) >= 0) {&#xA;        if (packet.stream_index == audio_stream_index) {&#xA;            if (avcodec_send_packet(codec_ctx, &amp;packet) == 0) {&#xA;                while (avcodec_receive_frame(codec_ctx, frame) == 0) {&#xA;                    if (frame->pts != AV_NOPTS_VALUE) {&#xA;                        frame->pts = av_rescale_q(frame->pts, codec_ctx->time_base, format_ctx->streams[audio_stream_index]->time_base);&#xA;                    }&#xA;&#xA;                    uint8_t* output_buffer;&#xA;                    int output_samples = av_rescale_rnd(&#xA;                        swr_get_delay(swr_ctx, codec_ctx->sample_rate) &#x2B; frame->nb_samples,&#xA;                        48000, codec_ctx->sample_rate, AV_ROUND_UP);&#xA;&#xA;                    int output_buffer_size = av_samples_get_buffer_size(&#xA;                        nullptr, 2, output_samples, AV_SAMPLE_FMT_S16, 1);&#xA;&#xA;                    output_buffer = (uint8_t*)av_malloc(output_buffer_size);&#xA;&#xA;                    if (output_buffer) {&#xA;                        memset(output_buffer, 0, output_buffer_size); // Zero padding to avoid random noise&#xA;                        int converted_samples = swr_convert(swr_ctx, &amp;output_buffer, output_samples,&#xA;                            (const uint8_t**)frame->extended_data, frame->nb_samples);&#xA;&#xA;                        if (converted_samples >= 0) {&#xA;                            output.insert(output.end(), output_buffer, output_buffer &#x2B; output_buffer_size);&#xA;                        }&#xA;                        else {&#xA;                            std::cerr &lt;&lt; "Error: Failed to convert audio samples." &lt;&lt; std::endl;&#xA;                        }&#xA;&#xA;                        av_free(output_buffer);&#xA;                    }&#xA;                    else {&#xA;                        std::cerr &lt;&lt; "Error: Could not allocate output buffer." &lt;&lt; std::endl;&#xA;                    }&#xA;                }&#xA;            }&#xA;            else {&#xA;                std::cerr &lt;&lt; "Error: Failed to send packet to codec context." &lt;&lt; std::endl;&#xA;            }&#xA;        }&#xA;        av_packet_unref(&amp;packet);&#xA;    }&#xA;&#xA;    av_frame_free(&amp;frame);&#xA;    swr_free(&amp;swr_ctx);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    avformat_close_input(&amp;format_ctx);&#xA;&#xA;    std::map metadata = {&#xA;        {"sample_width", 2},&#xA;        {"frame_rate", 48000},&#xA;        {"channels", 2},&#xA;        {"frame_width", 4}&#xA;    };&#xA;&#xA;    return AudioSegment(static_cast<const>(output.data()), output.size(), metadata);&#xA;}&#xA;&#xA;&#xA;std::ofstream AudioSegment::export_segment(std::string&amp; out_f,&#xA;    const std::string&amp; format,&#xA;    const std::string&amp; codec,&#xA;    const std::string&amp; bitrate,&#xA;    const std::vector&amp; parameters,&#xA;    const std::map&amp; tags,&#xA;    const std::string&amp; id3v2_version,&#xA;    const std::string&amp; cover) {&#xA;    av_log_set_level(AV_LOG_DEBUG);&#xA;    AVCodecContext* codec_ctx = nullptr;&#xA;    AVFormatContext* format_ctx = nullptr;&#xA;    AVStream* stream = nullptr;&#xA;    AVFrame* frame = nullptr;&#xA;    AVPacket* pkt = nullptr;&#xA;    int ret;&#xA;&#xA;    // Open output file&#xA;    std::ofstream out_file(out_f, std::ios::binary);&#xA;    if (!out_file) {&#xA;        throw std::runtime_error("Failed to open output file.");&#xA;    }&#xA;&#xA;    // Initialize format context&#xA;    avformat_alloc_output_context2(&amp;format_ctx, nullptr, format.c_str(), out_f.c_str());&#xA;    if (!format_ctx) {&#xA;        throw std::runtime_error("Could not allocate format context.");&#xA;    }&#xA;&#xA;    // Find encoder&#xA;    const AVCodec* codec_ptr = avcodec_find_encoder_by_name(codec.c_str());&#xA;    if (!codec_ptr) {&#xA;        throw std::runtime_error("Codec not found.");&#xA;    }&#xA;&#xA;    // Add stream&#xA;    stream = avformat_new_stream(format_ctx, codec_ptr);&#xA;    if (!stream) {&#xA;        throw std::runtime_error("Failed to create new stream.");&#xA;    }&#xA;&#xA;    // Allocate codec context&#xA;    codec_ctx = avcodec_alloc_context3(codec_ptr);&#xA;    if (!codec_ctx) {&#xA;        throw std::runtime_error("Could not allocate audio codec context.");&#xA;    }&#xA;&#xA;    // Set codec parameters&#xA;    codec_ctx->bit_rate = std::stoi(bitrate);&#xA;    codec_ctx->sample_fmt = codec_ptr->sample_fmts ? codec_ptr->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;&#xA;    codec_ctx->sample_rate = frame_rate_;&#xA;    codec_ctx->ch_layout.nb_channels = this->get_channels();&#xA;    AVChannelLayout ch_layout_1;&#xA;    av_channel_layout_uninit(&amp;ch_layout_1);&#xA;    av_channel_layout_default(&amp;ch_layout_1, this->get_channels());&#xA;    codec_ctx->ch_layout = ch_layout_1;&#xA;&#xA;    // Open codec&#xA;    ret = avcodec_open2(codec_ctx, codec_ptr, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        throw std::runtime_error("Could not open codec.");&#xA;    }&#xA;&#xA;    // Initialize packet&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt) {&#xA;        throw std::runtime_error("Could not allocate AVPacket.");&#xA;    }&#xA;&#xA;    // Initialize frame&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        throw std::runtime_error("Could not allocate AVFrame.");&#xA;    }&#xA;&#xA;    frame->nb_samples = codec_ctx->frame_size;&#xA;    frame->format = codec_ctx->sample_fmt;&#xA;    frame->ch_layout = codec_ctx->ch_layout;&#xA;    frame->sample_rate = codec_ctx->sample_rate;&#xA;&#xA;    // Allocate data buffer&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        throw std::runtime_error("Could not allocate audio data buffers.");&#xA;    }&#xA;&#xA;    // Encode frames&#xA;    int samples_read = 0;&#xA;    while (samples_read &lt; data_.size()) {&#xA;        ret = av_frame_make_writable(frame);&#xA;        if (ret &lt; 0) {&#xA;            throw std::runtime_error("Frame not writable.");&#xA;        }&#xA;&#xA;        // Determine the number of samples to copy into the frame&#xA;        int frame_size = std::min<int>(codec_ctx->frame_size, (data_.size() - samples_read) / frame_width_);&#xA;        int buffer_size = frame_size * frame_width_;&#xA;&#xA;        // Clear the frame data to avoid artifacts from previous data&#xA;        std::memset(frame->data[0], 0, codec_ctx->frame_size * frame_width_);&#xA;&#xA;        // Copy the actual audio data into the frame&#xA;        std::memcpy(frame->data[0], data_.data() &#x2B; samples_read, buffer_size);&#xA;        samples_read &#x2B;= buffer_size;&#xA;&#xA;        // If the frame is partially filled, pad the remaining part with zeros&#xA;        if (frame_size &lt; codec_ctx->frame_size) {&#xA;            std::memset(frame->data[0] &#x2B; buffer_size, 0, (codec_ctx->frame_size - frame_size) * frame_width_);&#xA;        }&#xA;&#xA;        // Send the frame for encoding&#xA;        ret = avcodec_send_frame(codec_ctx, frame);&#xA;        if (ret &lt; 0) {&#xA;            throw std::runtime_error("Error sending frame for encoding.");&#xA;        }&#xA;&#xA;        // Receive and write packets&#xA;        while (ret >= 0) {&#xA;            ret = avcodec_receive_packet(codec_ctx, pkt);&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;                break;&#xA;            }&#xA;            else if (ret &lt; 0) {&#xA;                throw std::runtime_error("Error encoding frame.");&#xA;            }&#xA;&#xA;            out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;    }&#xA;&#xA;    // **Explicitly flush the encoder**&#xA;    ret = avcodec_send_frame(codec_ctx, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        throw std::runtime_error("Error flushing the encoder.");&#xA;    }&#xA;&#xA;    // Receive and write remaining packets after flushing&#xA;    while (ret >= 0) {&#xA;        ret = avcodec_receive_packet(codec_ctx, pkt);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;            break;&#xA;        }&#xA;        else if (ret &lt; 0) {&#xA;            throw std::runtime_error("Error encoding frame during flush.");&#xA;        }&#xA;&#xA;        out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;&#xA;    // Cleanup&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    avformat_free_context(format_ctx);&#xA;&#xA;    return out_file;&#xA;}&#xA;</int></const></char>

    &#xA;

    I have no run time error but i see this message in console :

    &#xA;

    [libmp3lame @ 000002d26b239ac0] Trying to remove 47 more samples than there are in the queue&#xA;

    &#xA;

    I can play the exported mp3 file but there is background noise.

    &#xA;

  • C++ ffmpeg lib version 7.0 - runtime error

    1er septembre 2024, par Chris P

    I want to make a C++ lib named cppdub which will mimic the python module pydub.

    &#xA;

    One main function is to export the AudioSegment to a file with a specific format (example : mp3).

    &#xA;

    The code is :

    &#xA;

    void check_av_error(int error_code, const std::string&amp; msg) {&#xA;    if (error_code &lt; 0) {&#xA;        char errbuf[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_strerror(error_code, errbuf, sizeof(errbuf));&#xA;        throw std::runtime_error(msg &#x2B; ": " &#x2B; errbuf);&#xA;    }&#xA;}&#xA;&#xA;std::string av_err2str_(int errnum) {&#xA;    char buf[AV_ERROR_MAX_STRING_SIZE];&#xA;    av_strerror(errnum, buf, sizeof(buf));&#xA;    return std::string(buf);&#xA;}&#xA;&#xA;void log_error(const std::string&amp; msg) {&#xA;    std::cerr &lt;&lt; "Error: " &lt;&lt; msg &lt;&lt; std::endl;&#xA;}&#xA;&#xA;std::ofstream cppdub::AudioSegment::export_segment(&#xA;    std::string&amp; out_f,&#xA;    const std::string&amp; format,&#xA;    const std::string&amp; codec,&#xA;    const std::string&amp; bitrate,&#xA;    const std::vector&amp; parameters,&#xA;    const std::map&amp; tags,&#xA;    const std::string&amp; id3v2_version,&#xA;    const std::string&amp; cover) {&#xA;&#xA;    av_log_set_level(AV_LOG_DEBUG);&#xA;    avformat_network_init();&#xA;&#xA;    AVFormatContext* format_ctx = nullptr;&#xA;    int ret = avformat_alloc_output_context2(&amp;format_ctx, nullptr, format.c_str(), out_f.c_str());&#xA;    check_av_error(ret, "Could not allocate format context");&#xA;&#xA;    if (!(format_ctx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        ret = avio_open(&amp;format_ctx->pb, out_f.c_str(), AVIO_FLAG_WRITE);&#xA;        check_av_error(ret, "Could not open output file");&#xA;    }&#xA;&#xA;    AVStream* stream = avformat_new_stream(format_ctx, nullptr);&#xA;    if (!stream) {&#xA;        avformat_free_context(format_ctx);&#xA;        throw std::runtime_error("Could not allocate stream");&#xA;    }&#xA;&#xA;    const AVCodec* codec_obj = avcodec_find_encoder_by_name(codec.c_str());&#xA;    if (!codec_obj) {&#xA;        avformat_free_context(format_ctx);&#xA;        throw std::runtime_error("Codec not found");&#xA;    }&#xA;&#xA;    AVCodecContext* codec_ctx = avcodec_alloc_context3(codec_obj);&#xA;    if (!codec_ctx) {&#xA;        avformat_free_context(format_ctx);&#xA;        throw std::runtime_error("Could not allocate codec context");&#xA;    }&#xA;&#xA;    codec_ctx->sample_rate = this->get_frame_rate();&#xA;    AVChannelLayout ch_layout_1;&#xA;    av_channel_layout_uninit(&amp;ch_layout_1);&#xA;    av_channel_layout_default(&amp;ch_layout_1, 2);&#xA;    codec_ctx->ch_layout = ch_layout_1; // Adjust based on your needs&#xA;    codec_ctx->bit_rate = std::stoi(bitrate);&#xA;    codec_ctx->sample_fmt = codec_obj->sample_fmts[0];&#xA;&#xA;    if (format_ctx->oformat->flags &amp; AVFMT_GLOBALHEADER) {&#xA;        codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;    }&#xA;&#xA;    ret = avcodec_open2(codec_ctx, codec_obj, nullptr);&#xA;    check_av_error(ret, "Could not open codec");&#xA;&#xA;    stream->time_base = { 1, codec_ctx->sample_rate };&#xA;    ret = avcodec_parameters_from_context(stream->codecpar, codec_ctx);&#xA;    check_av_error(ret, "Could not set codec parameters");&#xA;&#xA;    ret = avformat_write_header(format_ctx, nullptr);&#xA;    check_av_error(ret, "Error occurred when writing header");&#xA;&#xA;    AVPacket pkt;&#xA;    av_init_packet(&amp;pkt);&#xA;    pkt.data = nullptr;&#xA;    pkt.size = 0;&#xA;&#xA;    int frame_size = av_samples_get_buffer_size(nullptr, codec_ctx->ch_layout.nb_channels,&#xA;        codec_ctx->frame_size, codec_ctx->sample_fmt, 0);&#xA;    check_av_error(frame_size, "Could not calculate frame size");&#xA;&#xA;    AVFrame* frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_free_context(format_ctx);&#xA;        throw std::runtime_error("Error allocating frame");&#xA;    }&#xA;&#xA;    frame->format = codec_ctx->sample_fmt;&#xA;    frame->ch_layout = codec_ctx->ch_layout;&#xA;    frame->sample_rate = codec_ctx->sample_rate;&#xA;    frame->nb_samples = codec_ctx->frame_size;&#xA;&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        av_frame_free(&amp;frame);&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_free_context(format_ctx);&#xA;        throw std::runtime_error("Error allocating frame buffer: " &#x2B; av_err2str_(ret));&#xA;    }&#xA;&#xA;    size_t data_offset = 0;&#xA;&#xA;    while (data_offset &lt; this->raw_data().size()) {&#xA;        int samples_to_process = std::min(frame_size, static_cast<int>(this->raw_data().size()) - static_cast<int>(data_offset));&#xA;&#xA;        // Fill the frame with audio data&#xA;        ret = avcodec_fill_audio_frame(frame, codec_ctx->ch_layout.nb_channels, codec_ctx->sample_fmt,&#xA;            reinterpret_cast<const>(this->raw_data().data()) &#x2B; data_offset,&#xA;            samples_to_process, 0);&#xA;        if (ret &lt; 0) {&#xA;            log_error("Error filling audio frame: " &#x2B; av_err2str_(ret));&#xA;            av_frame_free(&amp;frame);&#xA;            avcodec_free_context(&amp;codec_ctx);&#xA;            avformat_free_context(format_ctx);&#xA;            throw std::runtime_error("Error filling audio frame: " &#x2B; av_err2str_(ret));&#xA;        }&#xA;&#xA;        data_offset &#x2B;= samples_to_process;&#xA;&#xA;        ret = avcodec_send_frame(codec_ctx, frame);&#xA;        if (ret &lt; 0) {&#xA;            log_error("Error sending frame for encoding: " &#x2B; av_err2str_(ret));&#xA;            av_frame_free(&amp;frame);&#xA;            avcodec_free_context(&amp;codec_ctx);&#xA;            avformat_free_context(format_ctx);&#xA;            throw std::runtime_error("Error sending frame for encoding: " &#x2B; av_err2str_(ret));&#xA;        }&#xA;&#xA;        while (ret >= 0) {&#xA;            ret = avcodec_receive_packet(codec_ctx, &amp;pkt);&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;                break;&#xA;            }&#xA;            check_av_error(ret, "Error receiving packet");&#xA;&#xA;            pkt.stream_index = stream->index;&#xA;&#xA;            ret = av_interleaved_write_frame(format_ctx, &amp;pkt);&#xA;            check_av_error(ret, "Error writing encoded frame to output file");&#xA;&#xA;            av_packet_unref(&amp;pkt);&#xA;        }&#xA;    }&#xA;&#xA;    // Flush the encoder&#xA;    avcodec_send_frame(codec_ctx, nullptr);&#xA;    while (avcodec_receive_packet(codec_ctx, &amp;pkt) == 0) {&#xA;        pkt.stream_index = stream->index;&#xA;        av_interleaved_write_frame(format_ctx, &amp;pkt);&#xA;        av_packet_unref(&amp;pkt);&#xA;    }&#xA;&#xA;    av_write_trailer(format_ctx);&#xA;&#xA;    av_frame_free(&amp;frame);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    avformat_free_context(format_ctx);&#xA;&#xA;    return std::ofstream(out_f, std::ios::binary);&#xA;}&#xA;</const></int></int>

    &#xA;

    The runtime error is :

    &#xA;

    Exception thrown at 0x00007FF945137C9B (avcodec-61.dll) in cppdub_test.exe : 0xC0000005 : Access violation reading location 0x0000024CBCD25080.

    &#xA;

    for line :

    &#xA;

        ret = avcodec_send_frame(codec_ctx, frame);&#xA;

    &#xA;

    Call stack :

    &#xA;

        avcodec-61.dll!00007ff945137c9b()   Unknown&#xA;    avcodec-61.dll!00007ff9451381bb()   Unknown&#xA;    avcodec-61.dll!00007ff945139679()   Unknown&#xA;    avcodec-61.dll!00007ff94371521d()   Unknown&#xA;    avcodec-61.dll!00007ff9434a80c2()   Unknown&#xA;    avcodec-61.dll!00007ff9434a84a6()   Unknown&#xA;    avcodec-61.dll!00007ff9434a8749()   Unknown&#xA;>   cppdub_test.exe!cppdub::AudioSegment::export_segment(std::string &amp; out_f, const std::string &amp; format, const std::string &amp; codec, const std::string &amp; bitrate, const std::vector> &amp; parameters, const std::map,std::allocator>> &amp; tags, const std::string &amp; id3v2_version, const std::string &amp; cover) Line 572  C&#x2B;&#x2B;&#xA;    cppdub_test.exe!main() Line 33  C&#x2B;&#x2B;&#xA;    [External Code] &#xA;&#xA;

    &#xA;

    Autos :

    &#xA;

    &#x2B;       this    0x000000d3a08ff690 {data_="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0... ...} cppdub::AudioSegment *&#xA;&#x2B;       bitrate "128000"    const std::string &amp;&#xA;&#x2B;       ch_layout_1 {order=AV_CHANNEL_ORDER_NATIVE (1) nb_channels=2 u={mask=3 map=0x0000000000000003 {id=??? name=... opaque=...} } ...}   AVChannelLayout&#xA;&#x2B;       codec   "libmp3lame"    const std::string &amp;&#xA;&#x2B;       codec_ctx   0x0000024cbc78c240 {av_class=avcodec-61.dll!0x00007ff94789c760 {class_name=0x00007ff94789c740 "AVCodecContext" ...} ...}    AVCodecContext *&#xA;&#x2B;       codec_obj   avcodec-61.dll!0x00007ff9477fa4c0 (load symbols for additional information) {name=0x00007ff9477fa47c "libmp3lame" ...}  const AVCodec *&#xA;&#x2B;       cover   ""  const std::string &amp;&#xA;        data_offset 9216    unsigned __int64&#xA;&#x2B;       format  "mp3"   const std::string &amp;&#xA;&#x2B;       format_ctx  0x0000024cbc788a40 {av_class=avformat-61.dll!0x00007ff99eb09fe0 {class_name=0x00007ff99eb09fc0 "AVFormatContext" ...} ...}  AVFormatContext *&#xA;&#x2B;       frame   0x0000024cbc787380 {data=0x0000024cbc787380 {0x0000024cbcd25080 <error reading="reading" characters="characters" of="of">, ...} ...}    AVFrame *&#xA;        frame_size  9216    int&#xA;&#x2B;       id3v2_version   "4" const std::string &amp;&#xA;&#x2B;       out_f   "ha-ha-ha.mp3"  std::string &amp;&#xA;&#x2B;       parameters  { size=0 }  const std::vector> &amp;&#xA;&#x2B;       pkt {buf=0x0000000000000000 <null> pts=-9223372036854775808 dts=-9223372036854775808 ...}   AVPacket&#xA;        ret 9216    int&#xA;        samples_to_process  9216    int&#xA;&#x2B;       stream  0x0000024cbc789bc0 {av_class=avformat-61.dll!0x00007ff99eb09840 {class_name=0x00007ff99eb09820 "AVStream" ...} ...} AVStream *&#xA;&#x2B;       tags    { size=0 }  const std::map,std::allocator>> &amp;&#xA;</null></error>

    &#xA;

  • How to optimize this code that gets video frame as image

    14 août 2024, par TSR

    I am quite new to mp4 file. But here is my working attempt to extract image frame given video url and a timestamp.

    &#xA;

    In reality the input url is an 8K 10hours 200GB video, so I can't download it all, I can't load it to memory, and this is an API call so it has to be fast.

    &#xA;

    Is there anything else I can optimize ?

    &#xA;

    My doubts :

    &#xA;

      &#xA;
    • This line ffprobe -v error -select_streams v:0 -show_entries packet=pos,size,dts_time -read_intervals ${timestamp}%&#x2B;5 -of csv=p=0 "${url}" I chose this clingy 5s, in which case would this fail ?

      &#xA;

    • &#xA;

    • Same line, I don't know what's going on under the hood of this ffprobe command, but I tested it with the big 8K video and it seems to complete fast. So is it safe to assume that the entire 200GB was not downloaded ? An explanation of how this ffprobe command work would be appreciated

      &#xA;

    • &#xA;

    • Based on trial and error, I concluded that the intervals returned is parsable by ffmpeg only if the first frame until the timestamp is included. If I include only that the single frame interval, ffmpeg says it is an invalid file. (Makes sense cuz I don't think I'll get an image from a 4byte data.) However, how can I be sure that I am selecting the least number of intervals.

      &#xA;

    • &#xA;

    • Worse bottleneck : The function extractFrame takes 6seconds on the big video. It seems to read the entire video segment fetched (by the preceding subrange step). I couldn't find a way to jump to the last frame without computing. Is this how MP4 work ? I read somewhere that MP4 computes the current frame based on the previous. If that is true, does it mean there is no way to compute a specific frame without reading everything since the last keyframe ?

      &#xA;

    • &#xA;

    • Finally, this ffmpeg line is fishy (I got it from SO Extract final frame from a video using ffmpeg) it says that it ovewrites the output at everyframe . Does it mean it is writing to the disk every time ? I experience severe degradation in performance when I used .png instead of .jpg. This tells me that the image is computed every&#xA;frame. Can we compute only the final image at the very end ?

      &#xA;

    • &#xA;

    &#xA;

    Here is the working code to optimize.

    &#xA;

    import path from "path";&#xA;import axios from "axios";&#xA;import ffmpeg from "fluent-ffmpeg";&#xA;import fs from "fs";&#xA;import {promisify} from &#x27;util&#x27;;&#xA;import {exec} from &#x27;child_process&#x27;;&#xA;&#xA;const execPromise = promisify(exec);&#xA;&#xA;&#xA;// URL of the video and desired timestamp (in seconds)&#xA;&#xA;const videoUrl = &#x27;https://raw.githubusercontent.com/tolotrasamuel/assets/main/videoplayback.mp4&#x27;;&#xA;&#xA;console.log(videoUrl);&#xA;const timestamp = 30; // Example: 30 seconds into the video&#xA;&#xA;&#xA;// Function to get the byte range using ffprobe&#xA;const getByteRangeForTimestamp = async (url, timestamp) => {&#xA;    // Use ffprobe to get the offset and size of the frame at the given timestamp&#xA;    const command = `ffprobe -v error -select_streams v:0 -show_entries packet=pos,size,dts_time -read_intervals ${timestamp}%&#x2B;5 -of csv=p=0 "${url}"`;&#xA;    console.log(&#x27;Running command:&#x27;, command);&#xA;    const {stdout} = await execPromise(command);&#xA;&#xA;&#xA;    // Parse the output&#xA;    const timeStamps = stdout.trim().split("\n");&#xA;    const frames = timeStamps.map(ts => {&#xA;        const [dts_time, size, offset] = ts.split(&#x27;,&#x27;);&#xA;        const timeInt = parseFloat(dts_time);&#xA;        const offsetInt = parseInt(offset);&#xA;        const sizeInt = parseInt(size);&#xA;        return {dts_time: timeInt, size: sizeInt, offset: offsetInt};&#xA;    })&#xA;&#xA;    if (frames.length === 0) {&#xA;        throw new Error(&#x27;No frames found in the specified interval&#x27;);&#xA;    }&#xA;&#xA;    let closest;&#xA;&#xA;&#xA;    let i = 0&#xA;    while (i &lt; frames.length) {&#xA;        if (i === frames.length) {&#xA;            throw new Error(&#x27;No frames found in the specified 5s interval&#x27;);&#xA;        }&#xA;        if (frames[i].dts_time >= timestamp) {&#xA;            const oldDiff = Math.abs(closest.dts_time - timestamp);&#xA;            const newDiff = Math.abs(frames[i].dts_time - timestamp);&#xA;            if (newDiff &lt; oldDiff) {&#xA;                closest = frames[i];&#xA;            }&#xA;            break;&#xA;        }&#xA;        closest = frames[i];&#xA;        i&#x2B;&#x2B;;&#xA;    }&#xA;&#xA;    // I experimented with this, but it seems that the first frame is always at the beginning of a valid atom&#xA;    // anything after that will make the video unplayable&#xA;    const startByte = frames[0].offset;&#xA;    const endByte = closest.offset &#x2B; closest.size - 1;&#xA;&#xA;    const diff = Math.abs(closest.dts_time - timestamp);&#xA;    const size = endByte - startByte &#x2B; 1;&#xA;    console.log("Start: ", startByte, "End: ", endByte, "Diff: ", diff, "Timestamp: ", timestamp, "Closest: ", closest.dts_time, "Size to fetch: ", size)&#xA;&#xA;&#xA;    const startTime = closest.dts_time - frames[0].dts_time;&#xA;    return {startByte, endByte, startTime};&#xA;};&#xA;&#xA;// Download the specific segment&#xA;const downloadSegment = async (url, startByte, endByte, outputPath) => {&#xA;    console.log(`Downloading bytes ${startByte}-${endByte}...`);&#xA;    const response = await axios.get(url, {&#xA;        responseType: &#x27;arraybuffer&#x27;,&#xA;        headers: {&#xA;            Range: `bytes=${startByte}-${endByte}`,&#xA;        },&#xA;    });&#xA;&#xA;    console.log(&#x27;Segment downloaded!&#x27;, response.data.length, "Written to: ", outputPath);&#xA;    fs.writeFileSync(outputPath, response.data);&#xA;};&#xA;&#xA;// Extract frame from the segment&#xA;const extractFrameRaw = async (videoPath, timestamp, outputFramePath) => {&#xA;&#xA;&#xA;    const command = `ffmpeg -sseof -3 -i ${videoPath} -update 1 -q:v 1 ${outputFramePath} -y`;&#xA;    console.log(&#x27;Running command:&#x27;, command);&#xA;    const startTime = new Date().getTime();&#xA;    await execPromise(command);&#xA;    const endTime = new Date().getTime();&#xA;    console.log(&#x27;Processing time:&#x27;, endTime - startTime, &#x27;ms&#x27;);&#xA;    console.log(&#x27;Frame extracted to:&#x27;, outputFramePath);&#xA;};&#xA;const extractFrame = (videoPath, timestamp, outputFramePath) => {&#xA;    ffmpeg(videoPath)&#xA;        .inputOptions([&#x27;-sseof -5&#x27;])  // Seeks to 3 seconds before the end of the video&#xA;        .outputOptions([&#xA;            &#x27;-update 1&#x27;, // Continuously update the output file with new frames&#xA;            &#x27;-q:v 1&#x27;     // Set the highest JPEG quality&#xA;        ])&#xA;        .output(outputFramePath)  // Set the output file path&#xA;&#xA;        // log&#xA;        .on(&#x27;start&#x27;, function (commandLine) {&#xA;            console.log(&#x27;Spawned Ffmpeg with command: &#x27; &#x2B; commandLine);&#xA;        })&#xA;        .on(&#x27;progress&#x27;, function (progress) {&#xA;            console.log(&#x27;Processing: &#x27; &#x2B; progress.timemark &#x2B; &#x27;% done&#x27;, progress, &#x27;frame: &#x27;, progress.frames);&#xA;        })&#xA;        .on(&#x27;end&#x27;, function () {&#xA;            console.log(&#x27;Processing finished !&#x27;);&#xA;        })&#xA;        .on(&#x27;error&#x27;, function (err, stdout, stderr) {&#xA;            console.error(&#x27;Error:&#x27;, err);&#xA;            console.error(&#x27;ffmpeg stderr:&#x27;, stderr);&#xA;        })&#xA;        .run();&#xA;};&#xA;&#xA;&#xA;const __dirname = path.resolve();&#xA;&#xA;// Main function to orchestrate the process&#xA;(async () => {&#xA;    try {&#xA;        // ffmpeg.setFfmpegPath(&#x27;/usr/local/bin/ffmpeg&#x27;);&#xA;        const {startByte, endByte, startTime} = await getByteRangeForTimestamp(videoUrl, timestamp);&#xA;        const tmpVideoPath = path.resolve(__dirname, &#x27;temp_video.mp4&#x27;);&#xA;        const outputFramePath = path.resolve(__dirname, `frame_${timestamp}.jpg`);&#xA;&#xA;        await downloadSegment(videoUrl, startByte, endByte, tmpVideoPath);&#xA;        await extractFrame(tmpVideoPath, startTime, outputFramePath);&#xA;    } catch (err) {&#xA;        console.error(&#x27;Error:&#x27;, err);&#xA;    }&#xA;})();&#xA;

    &#xA;