Recherche avancée

Médias (0)

Mot : - Tags -/xmlrpc

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (74)

  • MediaSPIP Core : La Configuration

    9 novembre 2010, par

    MediaSPIP Core fournit par défaut trois pages différentes de configuration (ces pages utilisent le plugin de configuration CFG pour fonctionner) : une page spécifique à la configuration générale du squelettes ; une page spécifique à la configuration de la page d’accueil du site ; une page spécifique à la configuration des secteurs ;
    Il fournit également une page supplémentaire qui n’apparait que lorsque certains plugins sont activés permettant de contrôler l’affichage et les fonctionnalités spécifiques (...)

  • Websites made ​​with MediaSPIP

    2 mai 2011, par

    This page lists some websites based on MediaSPIP.

  • Possibilité de déploiement en ferme

    12 avril 2011, par

    MediaSPIP peut être installé comme une ferme, avec un seul "noyau" hébergé sur un serveur dédié et utilisé par une multitude de sites différents.
    Cela permet, par exemple : de pouvoir partager les frais de mise en œuvre entre plusieurs projets / individus ; de pouvoir déployer rapidement une multitude de sites uniques ; d’éviter d’avoir à mettre l’ensemble des créations dans un fourre-tout numérique comme c’est le cas pour les grandes plate-formes tout public disséminées sur le (...)

Sur d’autres sites (8512)

  • I need some help accurately trimming video using the FFmpeg C api

    20 mars 2015, par Justin Bradley

    I need some help accurately trimming video using the FFmpeg C api.
    What I’m seeing is when the input video stream has a time_base of 1/48000 it correctly selects the trimmed start and end times. However when the input video stream has a time_base other than 1/48000 the trim is incorrect.

    Time bases of 1/30000 and 1/24000 trim half the expected video stream length - 10s instead of 20s.
    1/25 trims almost nothing at all - the output file size is only a few kb.

    The audio stream appears to always be trimmed correctly.

    For example if I try to trim the first 20s of a video whose video stream time base is 1/30000, the output mp4’s length is 20s. It has first 10s of video and first 20s of audio.

    I think I’m incorrectly calculating the end_time but I’m not sure why it’s correct for 1/48000 time_base streams.

    record[i].start_time = av_rescale_q((int64_t)( start_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);
    record[i].end_time = av_rescale_q((int64_t)( end_time   * AV_TIME_BASE ), default_timebase, in_stream->time_base);

    Here is a more complete sample of code :

    int num_of_streams = ifmt_ctx->nb_streams;
    if(num_of_streams > 0) {
       // keeps track of each stream's trimmed start and end times
       struct stream_pts record[num_of_streams];

       for (i = 0; i < num_of_streams; i++) {
           AVStream *in_stream = ifmt_ctx->streams[i];
           AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
           if (!out_stream) {
               LOGE("=> Failed allocating output stream");
               ret = AVERROR_UNKNOWN;
               return close_connection(ret);
           }

           ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
           if (ret < 0) {
               LOGE("=> Failed to copy context from input to output stream codec context");
               return close_connection(ret);
           }
           out_stream->codec->codec_tag = 0;
           if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
               out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;

           AVRational default_timebase;
           default_timebase.num = 1;
           default_timebase.den = AV_TIME_BASE;

           // determine start/end times for each stream
           record[i].index = i;
           record[i].start_time = av_rescale_q((int64_t)( start_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);
           record[i].end_time = av_rescale_q((int64_t)( end_time   * AV_TIME_BASE ), default_timebase, in_stream->time_base);
       }

       av_dump_format(ofmt_ctx, 0, output_file, 1);

       if (!(ofmt->flags & AVFMT_NOFILE)) {
           ret = avio_open(&ofmt_ctx->pb, output_file, AVIO_FLAG_WRITE);
           if (ret < 0) {
               LOGE("=> Could not open output file '%s'", output_file);
               return close_connection(ret);
           }
       }

       ret = avformat_write_header(ofmt_ctx, NULL);
       if (ret < 0) {
           LOGE("=> Error occurred when opening output file");
           return close_connection(ret);
       }

       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];
           out_stream = ofmt_ctx->streams[pkt.stream_index];

           // copy packet
           pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
           pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, 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;

           // write the frames we're looking for
           if(pkt.pts >= record[pkt.stream_index].start_time && pkt.pts <= record[pkt.stream_index].end_time) {
               ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
               if (ret < 0) {
                   LOGE("=> Error muxing packet");
                   break;
               }
           }

           av_free_packet(&pkt);
       }
    }
  • C++ ffmpeg - export to wav error : Invalid PCM packet, data has size 2 but at least a size of 4 was expected

    9 septembre 2024, par Chris P

    C++ code :

    


    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 (!codec_ctx) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate codec context." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#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;        avcodec_free_context(&amp;codec_ctx);&#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;    codec_ctx->sample_rate = 44100;&#xA;    // Set up resampling context to convert to S16 format with 2 bytes per sample&#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", codec_ctx->sample_rate, 0);  // Match input sample rate&#xA;    av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);  // Force S16 format&#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;                        codec_ctx->sample_rate, 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;                        // Make sure output_buffer is valid before freeing&#xA;                        if (output_buffer != nullptr) {&#xA;                            av_free(output_buffer);&#xA;                            output_buffer = nullptr; // Prevent double-free&#xA;                        }&#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;    int frame_width = av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * 2;  // Use 2 bytes per sample and 2 channels&#xA;&#xA;    std::map metadata = {&#xA;        {"sample_width", 2},  // S16 format has 2 bytes per sample&#xA;        {"frame_rate", codec_ctx->sample_rate},  // Use the input sample rate&#xA;        {"channels", 2},  // Assuming stereo output&#xA;        {"frame_width", frame_width}&#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;    return AudioSegment(static_cast<const>(output.data()), output.size(), metadata);&#xA;}&#xA;&#xA;std::ofstream AudioSegment::export_segment_to_wav_file(const std::string&amp; out_f) {&#xA;    std::cout &lt;&lt; this->get_channels() &lt;&lt; std::endl;&#xA;    av_log_set_level(AV_LOG_ERROR);&#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;    // Initialize format context for WAV&#xA;    if (avformat_alloc_output_context2(&amp;format_ctx, nullptr, "wav", out_f.c_str()) &lt; 0) {&#xA;        throw std::runtime_error("Could not allocate format context.");&#xA;    }&#xA;&#xA;    // Find encoder for PCM&#xA;    const AVCodec* codec_ptr = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE);&#xA;    if (!codec_ptr) {&#xA;        throw std::runtime_error("PCM encoder 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 for PCM&#xA;    codec_ctx->bit_rate = 128000;  // Bitrate&#xA;    codec_ctx->sample_rate = this->get_frame_rate();  // Use correct sample rate&#xA;    codec_ctx->ch_layout.nb_channels = this->get_channels();  // Set the correct channel count&#xA;&#xA;    // Set the channel layout: stereo or mono&#xA;    if (this->get_channels() == 2) {&#xA;        av_channel_layout_default(&amp;codec_ctx->ch_layout, 2);  // Stereo layout&#xA;    }&#xA;    else {&#xA;        av_channel_layout_default(&amp;codec_ctx->ch_layout, 1);  // Mono layout&#xA;    }&#xA;&#xA;    codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;  // PCM 16-bit format&#xA;&#xA;    // Open codec&#xA;    if (avcodec_open2(codec_ctx, codec_ptr, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Could not open codec.");&#xA;    }&#xA;&#xA;    // Set codec parameters to the stream&#xA;    if (avcodec_parameters_from_context(stream->codecpar, codec_ctx) &lt; 0) {&#xA;        throw std::runtime_error("Could not initialize stream codec parameters.");&#xA;    }&#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;    if (!(format_ctx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        if (avio_open(&amp;format_ctx->pb, out_f.c_str(), AVIO_FLAG_WRITE) &lt; 0) {&#xA;            throw std::runtime_error("Could not open output file.");&#xA;        }&#xA;    }&#xA;&#xA;    // Write file header&#xA;    if (avformat_write_header(format_ctx, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Error occurred when writing file header.");&#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;    // Set the frame properties&#xA;    frame->format = codec_ctx->sample_fmt;&#xA;    frame->ch_layout = codec_ctx->ch_layout;&#xA;&#xA;    // Number of audio samples available in the data buffer&#xA;    int total_samples = data_.size() / (av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;    int samples_read = 0;&#xA;&#xA;    // Set the number of samples per frame dynamically based on the input data&#xA;    while (samples_read &lt; total_samples) {&#xA;        // Determine how many samples to read in this iteration (don&#x27;t exceed the total sample count)&#xA;        int num_samples = std::min(codec_ctx->frame_size, total_samples - samples_read);&#xA;        if (num_samples == 0) {&#xA;            num_samples = 1024;&#xA;            codec_ctx->frame_size = 1024;&#xA;        }&#xA;        // Ensure num_samples is not zero&#xA;        if (num_samples &lt;= 0) {&#xA;            throw std::runtime_error("Invalid number of samples in frame.");&#xA;        }&#xA;&#xA;        // Set the number of samples in the frame&#xA;        frame->nb_samples = num_samples;&#xA;&#xA;        // Allocate the frame buffer based on the number of samples&#xA;        ret = av_frame_get_buffer(frame, 0);&#xA;        if (ret &lt; 0) {&#xA;            std::cerr &lt;&lt; "Error allocating frame buffer: " &lt;&lt; ret &lt;&lt; std::endl;&#xA;            throw std::runtime_error("Could not allocate audio data buffers.");&#xA;        }&#xA;&#xA;        // Copy the audio data into the frame&#x27;s buffer (interleaving if necessary)&#xA;        /*if (codec_ctx->ch_layout.nb_channels == 2) {&#xA;            // If stereo, interleave planar data into packed format&#xA;            for (int i = 0; i &lt; num_samples; &#x2B;&#x2B;i) {&#xA;                ((int16_t*)frame->data[0])[2 * i] = ((int16_t*)data_.data())[i];                // Left channel&#xA;                ((int16_t*)frame->data[0])[2 * i &#x2B; 1] = ((int16_t*)data_.data())[total_samples &#x2B; i]; // Right channel&#xA;            }&#xA;        }&#xA;        else {&#xA;            // For mono or packed data, directly copy the samples&#xA;            std::memcpy(frame->data[0], data_.data() &#x2B; samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,&#xA;                num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;        }&#xA;        */&#xA;        std::memcpy(frame->data[0], data_.data() &#x2B; samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,&#xA;            num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;&#xA;        // Send the frame for encoding&#xA;        ret = avcodec_send_frame(codec_ctx, frame);&#xA;        if (ret &lt; 0) {&#xA;            std::cerr &lt;&lt; "Error sending frame for encoding: " &lt;&lt; ret &lt;&lt; std::endl;&#xA;            throw std::runtime_error("Error sending frame for encoding.");&#xA;        }&#xA;&#xA;        // Receive and write encoded 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 during encoding.");&#xA;            }&#xA;&#xA;            out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;&#xA;        samples_read &#x2B;= num_samples;&#xA;    }&#xA;&#xA;    // Flush the encoder&#xA;    if (avcodec_send_frame(codec_ctx, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Error flushing the encoder.");&#xA;    }&#xA;&#xA;    while (avcodec_receive_packet(codec_ctx, pkt) >= 0) {&#xA;        out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;&#xA;    // Write file trailer&#xA;    av_write_trailer(format_ctx);&#xA;&#xA;    // Cleanup&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;&#xA;    if (!(format_ctx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        avio_closep(&amp;format_ctx->pb);&#xA;    }&#xA;    avformat_free_context(format_ctx);&#xA;&#xA;    out_file.close();&#xA;    return out_file;&#xA;}&#xA;&#xA;</const></char>

    &#xA;

    Run code :

    &#xA;

    #include "audio_segment.h"&#xA;#include "effects.h"&#xA;#include "playback.h"&#xA;#include "cppaudioop.h"&#xA;#include "exceptions.h"&#xA;#include "generators.h"&#xA;#include "silence.h"&#xA;#include "utils.h"&#xA;&#xA;#include <iostream>&#xA;#include <filesystem>&#xA;&#xA;using namespace cppdub;&#xA;&#xA;int main() {&#xA;    try {&#xA;        // Load the source audio file&#xA;        AudioSegment seg_1 = AudioSegment::from_file("../data/test10.mp3");&#xA;        std::string out_file_name = "ah-ah-ah.wav";&#xA;&#xA;        // Export the audio segment to a new file with specified settings&#xA;        //seg_1.export_segment(out_file_name, "mp3");&#xA;        seg_1.export_segment_to_wav_file(out_file_name);&#xA;&#xA;&#xA;        // Optionally play the audio segment to verify&#xA;        // play(seg_1);&#xA;&#xA;        // Load the exported audio file&#xA;        AudioSegment seg_2 = AudioSegment::from_file(out_file_name);&#xA;&#xA;        // Play segments&#xA;        //play(seg_1);&#xA;        play(seg_2);&#xA;    }&#xA;    catch (const std::exception&amp; e) {&#xA;        std::cerr &lt;&lt; "An error occurred: " &lt;&lt; e.what() &lt;&lt; std::endl;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;</filesystem></iostream>

    &#xA;

    Error in second call of from_file function :

    &#xA;

    [pcm_s16le @ 000002d82ca5bfc0] Invalid PCM packet, data has size 2 but at least a size of 4 was expected

    &#xA;

    The process continue, i call hear the seg_2 with play(seg_2) call, but i can't directly play seg_2 export wav file (from windows explorer).

    &#xA;

    I had a guess that error may be because packed vs plannar formats missmatch but i am not quit sure. Maybe a swr_convert is necessary.

    &#xA;

  • Issues with Video Recording Duration and Smooth Playback when Using v4l2 to MP4 (FFmpeg)

    9 décembre 2024, par Reena

    I'm trying to record a video from a USB device with v4l2 framework and save it in MP4 format using FFmpeg. My sample code successfully captures and saves the video, but I'm running into some issues :

    &#xA;

    The recorded video duration is shorter than expected. For instance :

    &#xA;

    When recording a 1-minute video at 1280x720, the output file only has 58 or 59 seconds.&#xA;For 1920x1080, the duration is even more off — only about 28 or 30 seconds, instead of the expected 1 minute.&#xA;Additionally, the video is not smooth. There are noticeable drops in frames and playback inconsistencies.

    &#xA;

    My setup :

    &#xA;

    Using a USB device with v4l2 framework&#xA;Saving the video in MP4 format&#xA;Tested with different resolutions (1280x720, 1920x1080)&#xA;I've attached my sample code below. Could someone help me figure out why I'm experiencing these issues with video duration and smooth playback ?

    &#xA;

    Any advice, fixes, or suggestions would be greatly appreciated !

    &#xA;

    #include &#xA;#include &#xA;#include &#xA;#include &#xA;#include <sys></sys>ioctl.h>&#xA;#include <sys></sys>mman.h>&#xA;#include <linux></linux>videodev2.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;#include &#xA;#include <sys></sys>time.h>&#xA;#include &#xA;&#xA;#define WIDTH 1280&#xA;#define HEIGHT 720&#xA;#define FPS 30&#xA;#define DURATION 10 // Recording duration in seconds&#xA;#define BUFFER_COUNT 4 // Number of buffers&#xA;&#xA;struct buffer {&#xA;    void *start;&#xA;    size_t length;&#xA;};&#xA;&#xA;struct buffer *buffers;&#xA;&#xA;void open_device(int *fd, const char *device) {&#xA;    *fd = open(device, O_RDWR | O_NONBLOCK);&#xA;    if (*fd &lt; 0) {&#xA;        perror("Cannot open video device");&#xA;        exit(1);&#xA;    }&#xA;}&#xA;&#xA;void init_mmap(int fd) {&#xA;    struct v4l2_requestbuffers req;&#xA;    memset(&amp;req, 0, sizeof(req));&#xA;    req.count = BUFFER_COUNT;&#xA;    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;&#xA;    req.memory = V4L2_MEMORY_MMAP;&#xA;&#xA;    if (ioctl(fd, VIDIOC_REQBUFS, &amp;req) &lt; 0) {&#xA;        perror("Requesting buffer");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    buffers = calloc(req.count, sizeof(*buffers));&#xA;    for (size_t i = 0; i &lt; req.count; &#x2B;&#x2B;i) {&#xA;        struct v4l2_buffer buf;&#xA;        memset(&amp;buf, 0, sizeof(buf));&#xA;        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;&#xA;        buf.memory = V4L2_MEMORY_MMAP;&#xA;        buf.index = i;&#xA;&#xA;        if (ioctl(fd, VIDIOC_QUERYBUF, &amp;buf) &lt; 0) {&#xA;            perror("Querying buffer");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        buffers[i].length = buf.length;&#xA;        buffers[i].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);&#xA;&#xA;        if (MAP_FAILED == buffers[i].start) {&#xA;            perror("mmap");&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;void start_capturing(int fd) {&#xA;    for (size_t i = 0; i &lt; BUFFER_COUNT; &#x2B;&#x2B;i) {&#xA;        struct v4l2_buffer buf;&#xA;        memset(&amp;buf, 0, sizeof(buf));&#xA;        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;&#xA;        buf.memory = V4L2_MEMORY_MMAP;&#xA;        buf.index = i;&#xA;&#xA;        if (ioctl(fd, VIDIOC_QBUF, &amp;buf) &lt; 0) {&#xA;            perror("Queue buffer");&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;&#xA;    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;&#xA;    if (ioctl(fd, VIDIOC_STREAMON, &amp;type) &lt; 0) {&#xA;        perror("Start capture");&#xA;        exit(1);&#xA;    }&#xA;}&#xA;&#xA;void stop_capturing(int fd) {&#xA;    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;&#xA;&#xA;    if (ioctl(fd, VIDIOC_STREAMOFF, &amp;type) &lt; 0) {&#xA;        perror("Stop capture");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    printf("Video capture stopped.\n");&#xA;}&#xA;&#xA;void unmap_buffers() {&#xA;    for (size_t i = 0; i &lt; BUFFER_COUNT; &#x2B;&#x2B;i) {&#xA;        if (munmap(buffers[i].start, buffers[i].length) &lt; 0) {&#xA;            perror("munmap");&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;&#xA;    free(buffers);&#xA;}&#xA;&#xA;void initialize_ffmpeg(AVFormatContext **fmt_ctx, AVCodecContext **codec_ctx, AVStream **video_stream, const char *filename) {&#xA;    av_register_all();&#xA;&#xA;    AVOutputFormat *fmt = av_guess_format(NULL, filename, NULL);&#xA;    if (!fmt) {&#xA;        fprintf(stderr, "Could not determine output format\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if (avformat_alloc_output_context2(fmt_ctx, fmt, NULL, filename) &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate format context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    *video_stream = avformat_new_stream(*fmt_ctx, NULL);&#xA;    if (!*video_stream) {&#xA;        fprintf(stderr, "Could not create stream\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    *codec_ctx = avcodec_alloc_context3(codec);&#xA;    if (!*codec_ctx) {&#xA;        fprintf(stderr, "Could not allocate codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    (*codec_ctx)->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    (*codec_ctx)->width = WIDTH;&#xA;    (*codec_ctx)->height = HEIGHT;&#xA;    (*codec_ctx)->time_base = (AVRational){1, FPS};&#xA;    (*codec_ctx)->framerate = (AVRational){FPS, 1};&#xA;    (*codec_ctx)->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;    (*codec_ctx)->gop_size = 10;&#xA;    (*codec_ctx)->max_b_frames = 1;&#xA;&#xA;    av_opt_set(*codec_ctx, "preset", "fast", 0);&#xA;    av_opt_set_int(*codec_ctx, "crf", 23, 0);&#xA;&#xA;    (*video_stream)->time_base = (*codec_ctx)->time_base;&#xA;    (*video_stream)->codecpar->codec_id = fmt->video_codec;&#xA;    (*video_stream)->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    (*video_stream)->codecpar->width = (*codec_ctx)->width;&#xA;    (*video_stream)->codecpar->height = (*codec_ctx)->height;&#xA;    (*video_stream)->codecpar->format = (*codec_ctx)->pix_fmt;&#xA;&#xA;    if ((*fmt_ctx)->oformat->flags &amp; AVFMT_GLOBALHEADER) {&#xA;        (*codec_ctx)->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;    }&#xA;&#xA;    if (avcodec_open2(*codec_ctx, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if (avcodec_parameters_from_context((*video_stream)->codecpar, *codec_ctx) &lt; 0) {&#xA;        fprintf(stderr, "Could not copy codec parameters\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if (!(fmt->flags &amp; AVFMT_NOFILE)) {&#xA;        if (avio_open(&amp;(*fmt_ctx)->pb, filename, AVIO_FLAG_WRITE) &lt; 0) {&#xA;            fprintf(stderr, "Could not open output file\n");&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;&#xA;    if (avformat_write_header(*fmt_ctx, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not write header\n");&#xA;        exit(1);&#xA;    }&#xA;}&#xA;&#xA;void capture_and_encode(int fd, AVFormatContext *fmt_ctx, AVCodecContext *codec_ctx, struct SwsContext *sws_ctx, AVStream *video_stream, int duration) {&#xA;    struct v4l2_buffer buffer;&#xA;    AVFrame *frame = av_frame_alloc();&#xA;    AVPacket packet;&#xA;    av_init_packet(&amp;packet);&#xA;&#xA;    frame->format = codec_ctx->pix_fmt;&#xA;    frame->width = codec_ctx->width;&#xA;    frame->height = codec_ctx->height;&#xA;    av_image_alloc(frame->data, frame->linesize, codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, 32);&#xA;&#xA;    struct timespec start_time;&#xA;    clock_gettime(CLOCK_MONOTONIC, &amp;start_time);&#xA;    double elapsed_time = 0;&#xA;    int64_t pts_counter = 0;&#xA;    int frame_count = 0;&#xA;&#xA;    while (elapsed_time &lt; duration) {&#xA;        fd_set fds;&#xA;        struct timeval tv;&#xA;        int r;&#xA;&#xA;        FD_ZERO(&amp;fds);&#xA;        FD_SET(fd, &amp;fds);&#xA;&#xA;        tv.tv_sec = 2;&#xA;        tv.tv_usec = 0;&#xA;&#xA;        r = select(fd &#x2B; 1, &amp;fds, NULL, NULL, &amp;tv);&#xA;        if (r == -1) {&#xA;            perror("select");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        if (r == 0) {&#xA;            fprintf(stderr, "select timeout\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        memset(&amp;buffer, 0, sizeof(buffer));&#xA;        buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;&#xA;        buffer.memory = V4L2_MEMORY_MMAP;&#xA;&#xA;        if (ioctl(fd, VIDIOC_DQBUF, &amp;buffer) &lt; 0) {&#xA;            if (errno == EAGAIN) continue;&#xA;            perror("Could not dequeue buffer");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        uint8_t *src_slices[1] = {buffers[buffer.index].start};&#xA;        int src_stride[1] = {WIDTH * 2}; // UYVY is 2 bytes per pixel&#xA;&#xA;        sws_scale(sws_ctx, src_slices, src_stride, 0, HEIGHT, frame->data, frame->linesize);&#xA;&#xA;        frame->pts = pts_counter;&#xA;        pts_counter &#x2B;= av_rescale_q(1, (AVRational){1, FPS}, codec_ctx->time_base);&#xA;&#xA;        if (avcodec_send_frame(codec_ctx, frame) &lt; 0) {&#xA;            fprintf(stderr, "Error sending frame\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        while (avcodec_receive_packet(codec_ctx, &amp;packet) == 0) {&#xA;            av_packet_rescale_ts(&amp;packet, codec_ctx->time_base, video_stream->time_base);&#xA;            packet.stream_index = video_stream->index;&#xA;&#xA;            if (av_interleaved_write_frame(fmt_ctx, &amp;packet) &lt; 0) {&#xA;                fprintf(stderr, "Error writing frame\n");&#xA;                exit(1);&#xA;            }&#xA;&#xA;            av_packet_unref(&amp;packet);&#xA;        }&#xA;        printf("Processed frame %d\n", frame_count);&#xA;&#xA;        if (ioctl(fd, VIDIOC_QBUF, &amp;buffer) &lt; 0) {&#xA;            perror("Could not requeue buffer");&#xA;            exit(1);&#xA;        }&#xA;        frame_count&#x2B;&#x2B;;&#xA;        struct timespec current_time;&#xA;        clock_gettime(CLOCK_MONOTONIC, &amp;current_time);&#xA;        elapsed_time = (current_time.tv_sec - start_time.tv_sec) &#x2B; (current_time.tv_nsec - start_time.tv_nsec) / 1e9;&#xA;        printf("Elapsed time: %f seconds\n", elapsed_time);&#xA;    }&#xA;&#xA;    av_freep(&amp;frame->data[0]);&#xA;    av_frame_free(&amp;frame);&#xA;    printf("Total frames processed: %d\n", frame_count);&#xA;}&#xA;&#xA;int main(int argc, char *argv[]) {&#xA;    if (argc != 2) {&#xA;        fprintf(stderr, "Usage: %s <output file="file">\n", argv[0]);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    const char *output_file = argv[1];&#xA;    int fd;&#xA;    open_device(&amp;fd, "/dev/video2");&#xA;&#xA;    struct v4l2_format fmt;&#xA;    memset(&amp;fmt, 0, sizeof(fmt));&#xA;    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;&#xA;    fmt.fmt.pix.width = WIDTH;&#xA;    fmt.fmt.pix.height = HEIGHT;&#xA;    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;&#xA;    fmt.fmt.pix.field = V4L2_FIELD_NONE;&#xA;&#xA;    if (ioctl(fd, VIDIOC_S_FMT, &amp;fmt) &lt; 0) {&#xA;        perror("Setting pixel format");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY) {&#xA;        fprintf(stderr, "Device does not support UYVY format\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    init_mmap(fd);&#xA;    start_capturing(fd);&#xA;&#xA;    AVFormatContext *fmt_ctx = NULL;&#xA;    AVCodecContext *codec_ctx = NULL;&#xA;    AVStream *video_stream = NULL;&#xA;&#xA;    initialize_ffmpeg(&amp;fmt_ctx, &amp;codec_ctx, &amp;video_stream, output_file);&#xA;&#xA;    struct SwsContext *sws_ctx = sws_getContext(WIDTH, HEIGHT, AV_PIX_FMT_UYVY422,&#xA;                                                WIDTH, HEIGHT, AV_PIX_FMT_YUV420P,&#xA;                                                SWS_BICUBIC, NULL, NULL, NULL);&#xA;&#xA;    if (!sws_ctx) {&#xA;        fprintf(stderr, "Could not initialize SwsContext\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    capture_and_encode(fd, fmt_ctx, codec_ctx, sws_ctx, video_stream, DURATION);&#xA;&#xA;    sws_freeContext(sws_ctx);&#xA;    av_write_trailer(fmt_ctx);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    avformat_free_context(fmt_ctx);&#xA;    stop_capturing(fd);&#xA;    unmap_buffers();&#xA;    close(fd);&#xA;&#xA;    return 0;&#xA;}&#xA;</output>

    &#xA;

    Thank you !

    &#xA;