Recherche avancée

Médias (1)

Mot : - Tags -/wave

Autres articles (98)

  • MediaSPIP 0.1 Beta version

    25 avril 2011, par

    MediaSPIP 0.1 beta is the first version of MediaSPIP proclaimed as "usable".
    The zip file provided here only contains the sources of MediaSPIP in its standalone version.
    To get a working installation, you must manually install all-software dependencies on the server.
    If you want to use this archive for an installation in "farm mode", you will also need to proceed to other manual (...)

  • ANNEXE : Les plugins utilisés spécifiquement pour la ferme

    5 mars 2010, par

    Le site central/maître de la ferme a besoin d’utiliser plusieurs plugins supplémentaires vis à vis des canaux pour son bon fonctionnement. le plugin Gestion de la mutualisation ; le plugin inscription3 pour gérer les inscriptions et les demandes de création d’instance de mutualisation dès l’inscription des utilisateurs ; le plugin verifier qui fournit une API de vérification des champs (utilisé par inscription3) ; le plugin champs extras v2 nécessité par inscription3 (...)

  • 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

Sur d’autres sites (10780)

  • Record Audio using ALSA in mp4 format

    18 novembre 2024, par teena meheren

    I am working on to record audio using ALSA library. I am able to record the audio using the same library in .wav file, but what I need is to record an .mp4 file. For that I initialize the FFmpeg encoder to create MP4 file and trying to record the audio by writing the audio frames into the file. The result which I am getting is an empty MP4 file with no audio.

    


    Here I am attaching the code which I have tried

    


    #include &#xA;#include &#xA;#include &#xA;#include <alsa></alsa>asoundlib.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libswresample></libswresample>swresample.h>&#xA;&#xA;int terminate = 0;&#xA;int channels = 2;&#xA;&#xA;// Function to handle termination signal&#xA;void sigint_handler(int sig) {&#xA;    terminate = 1;&#xA;}&#xA;&#xA;// Function to initialize the FFmpeg encoder and writer&#xA;AVFormatContext* init_ffmpeg_writer(const char *filename, AVCodecContext **audio_codec_ctx) {&#xA;    AVFormatContext *fmt_ctx = NULL;&#xA;    AVCodec *audio_codec = NULL;&#xA;    AVStream *audio_stream = NULL;&#xA;&#xA;    // Initialize the output format context&#xA;    if (avformat_alloc_output_context2(&amp;fmt_ctx, NULL, "mp4", filename) &lt; 0) {&#xA;        fprintf(stderr, "Could not create output context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Find the codec&#xA;    audio_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);&#xA;    if (!audio_codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Create a new stream&#xA;    audio_stream = avformat_new_stream(fmt_ctx, audio_codec);&#xA;    if (!audio_stream) {&#xA;        fprintf(stderr, "Could not create audio stream\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Set up codec context&#xA;    *audio_codec_ctx = avcodec_alloc_context3(audio_codec);&#xA;    (*audio_codec_ctx)->channels = 2;&#xA;    (*audio_codec_ctx)->channel_layout = AV_CH_LAYOUT_STEREO;&#xA;    (*audio_codec_ctx)->sample_rate = 44100;&#xA;    (*audio_codec_ctx)->sample_fmt = AV_SAMPLE_FMT_FLTP; // 32-bit float for input format&#xA;    (*audio_codec_ctx)->bit_rate = 128000; // Bitrate for AAC encoding&#xA;&#xA;    // Open the codec&#xA;    if (avcodec_open2(*audio_codec_ctx, audio_codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Copy codec parameters from codec context to the stream&#xA;    if (avcodec_parameters_from_context(audio_stream->codecpar, *audio_codec_ctx) &lt; 0) {&#xA;        fprintf(stderr, "Could not copy codec parameters\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Open the output file&#xA;    if (!(fmt_ctx->oformat->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;    // Write the file header&#xA;    if (avformat_write_header(fmt_ctx, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Error occurred when writing header\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    return fmt_ctx;&#xA;}&#xA;&#xA;void write_audio_frame(AVFormatContext *fmt_ctx, AVCodecContext *audio_codec_ctx, uint8_t *buffer, int buffer_size) {&#xA;    AVPacket pkt;&#xA;    AVFrame *frame;&#xA;    int ret;&#xA;    static int64_t frame_count = 0; // Ensure this is initialized correctly&#xA;    static double stream_time = 0;&#xA;&#xA;    // Initialize the packet&#xA;    av_init_packet(&amp;pkt);&#xA;    pkt.data = NULL;&#xA;    pkt.size = 0;&#xA;&#xA;    // Allocate and set up frame&#xA;    frame = av_frame_alloc();&#xA;    frame->nb_samples = audio_codec_ctx->frame_size;&#xA;    frame->channel_layout = audio_codec_ctx->channel_layout;&#xA;    frame->format = audio_codec_ctx->sample_fmt;&#xA;    frame->sample_rate = audio_codec_ctx->sample_rate;&#xA;&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate frame buffer\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Initialize swresample context&#xA;    SwrContext *swr_ctx = swr_alloc();&#xA;    av_opt_set_int(swr_ctx, "in_channel_layout", frame->channel_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "out_channel_layout", frame->channel_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "in_sample_rate", 44100, 0);&#xA;    av_opt_set_int(swr_ctx, "out_sample_rate", 44100, 0);&#xA;    av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);&#xA;    av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);&#xA;&#xA;    if (swr_init(swr_ctx) &lt; 0) {&#xA;        fprintf(stderr, "Error initializing swresample context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Calculate the number of samples based on buffer size and format&#xA;    int num_samples = buffer_size / (2 * channels); // 2 bytes per sample (S16)&#xA;    uint8_t *out_buffer = (uint8_t *)malloc(num_samples * 4); // 4 bytes per sample (float)&#xA;&#xA;    // Resample audio data&#xA;    ret = swr_convert(swr_ctx, &amp;out_buffer, num_samples, (const uint8_t **)&amp;buffer, num_samples);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Error during resampling\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Copy resampled data to the frame&#x27;s buffer&#xA;    int out_size = num_samples * av_get_bytes_per_sample(audio_codec_ctx->sample_fmt);&#xA;    memcpy(frame->data[0], out_buffer, out_size);&#xA;&#xA;    if (frame->data[0] == NULL) {&#xA;        fprintf(stderr, "Frame data is NULL\n");&#xA;    }&#xA;&#xA;    // Set timestamps for the packet&#xA;    pkt.pts = pkt.dts = (frame_count * audio_codec_ctx->frame_size * AV_TIME_BASE) / audio_codec_ctx->sample_rate;&#xA;    stream_time &#x2B;= (double)frame->nb_samples / audio_codec_ctx->sample_rate;&#xA;&#xA;    // Send the frame for encoding&#xA;    ret = avcodec_send_frame(audio_codec_ctx, frame);&#xA;    if (ret &lt; 0) {&#xA;        if (ret == AVERROR(EAGAIN)) {&#xA;            // Encoder is temporarily unavailable, wait or retry&#xA;            fprintf(stderr, "Encoder temporarily unavailable, retrying...\n");&#xA;            return;&#xA;        } else {&#xA;            // Another error occurred&#xA;            fprintf(stderr, "Error sending audio frame to encoder: %s\n", av_err2str(ret));&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;&#xA;    // Receive the encoded packet&#xA;    ret = avcodec_receive_packet(audio_codec_ctx, &amp;pkt);&#xA;    if (ret &lt; 0) {&#xA;        if (ret == AVERROR(EAGAIN)) {&#xA;            // No packet is available yet, maybe retry later&#xA;            fprintf(stderr, "No packet available, retrying...\n");&#xA;            return;&#xA;        } else {&#xA;            fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;&#xA;    pkt.stream_index = 0;&#xA;&#xA;    // Write the packet to the output&#xA;    ret = av_interleaved_write_frame(fmt_ctx, &amp;pkt);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Error while writing frame\n");&#xA;        exit(1);&#xA;    }else if (ret==0){&#xA;&#xA;    printf("Writing frames successfully\n");&#xA;}&#xA;&#xA;    // Clean up&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_unref(&amp;pkt);&#xA;    free(out_buffer);&#xA;&#xA;    frame_count&#x2B;&#x2B;;  // Increment the frame count to track timestamps&#xA;}&#xA;&#xA;&#xA;&#xA;&#xA;int main() {&#xA;    snd_pcm_t *capture_handle;&#xA;    snd_pcm_hw_params_t *hw_params;&#xA;    int err;&#xA;    unsigned int sample_rate = 44100;&#xA;    snd_pcm_uframes_t frames = 32;&#xA;    char *buffer;&#xA;    int buffer_size;&#xA;&#xA;    // Register signal handler for termination (Ctrl&#x2B;C)&#xA;    signal(SIGINT, sigint_handler);&#xA;&#xA;    // Open the PCM device for recording (capture)&#xA;    if ((err = snd_pcm_open(&amp;capture_handle, "default", SND_PCM_STREAM_CAPTURE, 0)) &lt; 0) {&#xA;        fprintf(stderr, "cannot open audio device %s (%s)\n", "default", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Allocate the hardware parameters structure&#xA;    if ((err = snd_pcm_hw_params_malloc(&amp;hw_params)) &lt; 0) {&#xA;        fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Initialize the hardware parameters with default values&#xA;    if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) &lt; 0) {&#xA;        fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Set the desired hardware parameters&#xA;    if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) &lt; 0) {&#xA;        fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) &lt; 0) {&#xA;        fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if ((err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &amp;sample_rate, 0)) &lt; 0) {&#xA;        fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, channels)) &lt; 0) {&#xA;        fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if ((err = snd_pcm_hw_params(capture_handle, hw_params)) &lt; 0) {&#xA;        fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Free the hardware parameters structure&#xA;    snd_pcm_hw_params_free(hw_params);&#xA;&#xA;    // Prepare the PCM device for use&#xA;    if ((err = snd_pcm_prepare(capture_handle)) &lt; 0) {&#xA;        fprintf(stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror(err));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Calculate buffer size&#xA;    buffer_size = frames * channels * 2; // 2 bytes/sample, 2 channels&#xA;    buffer = (char *) malloc(buffer_size);&#xA;&#xA;    // Initialize FFmpeg&#xA;    av_register_all();&#xA;&#xA;    // Initialize the output file and codec&#xA;    AVCodecContext *audio_codec_ctx = NULL;&#xA;    AVFormatContext *fmt_ctx = init_ffmpeg_writer("recorded_audio.mp4", &amp;audio_codec_ctx);&#xA;&#xA;    printf("Recording...\n");&#xA;&#xA;    // Record audio data until termination signal is received&#xA;    while (!terminate) {&#xA;        printf("entered while\n");&#xA;        if ((err = snd_pcm_readi(capture_handle, buffer, frames)) != frames) {&#xA;            fprintf(stderr, "read from audio interface failed (%s)\n", snd_strerror(err));&#xA;            exit(1);&#xA;        }&#xA;&#xA;        // Write audio frame to the MP4 file&#xA;        write_audio_frame(fmt_ctx, audio_codec_ctx, (uint8_t *)buffer, buffer_size);&#xA;    }&#xA;&#xA;    printf("Recording finished.\n");&#xA;&#xA;    // Write the file footer and close&#xA;    av_write_trailer(fmt_ctx);&#xA;    avcodec_free_context(&amp;audio_codec_ctx);&#xA;    avformat_close_input(&amp;fmt_ctx);&#xA;    avformat_free_context(fmt_ctx);&#xA;&#xA;    // Clean up ALSA resources&#xA;    snd_pcm_close(capture_handle);&#xA;    free(buffer);&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;

    Here I am attaching the logs too

    &#xA;

    Recording...&#xA;entered while&#xA;No packet available, retrying...&#xA;entered while&#xA;[mp4 @ 0x611490ddeb40] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly&#xA;[mp4 @ 0x611490ddeb40] Encoder did not produce proper pts, making some up.&#xA;Writing frames successfully&#xA;entered while&#xA;Writing frames successfully&#xA;entered while&#xA;Writing frames successfully&#xA;entered while&#xA;Writing frames successfully&#xA;

    &#xA;

    Can anyone help me how to resolve the above error by setting up the timestamp properly and record audio in mp4 file using ALSA .

    &#xA;

  • How to record microphone sounds using ffmpeg ?

    9 décembre 2016, par xRobot

    I am using this code :

    ffmpeg -f dshow -i audio="Microphone" output.mp3

    but I get this error :

    [dshow @ 00000000007463c0] Could not find audio only device with name [Microphone] among source devices of type audio.
  • lavd/avfoundation : Introduce device alias ’none’ to allow the user to record only...

    13 novembre 2014, par Thilo Borgmann
    lavd/avfoundation : Introduce device alias ’none’ to allow the user to record only audio or video.
    

    Changes the selection of a default device to none instead of the system default device.

    Signed-off-by : Michael Niedermayer <michaelni@gmx.at>

    • [DH] libavdevice/avfoundation.m
    • [DH] libavdevice/version.h