Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Can't find error in function for changing sampling rate

    29 avril, par kitty uwu

    I have function for changing sampling rate of audio (only one channel):

    int change_sampling_rate(float *audio_input, int input_sample_rate, int output_sample_rate, int input_num_of_samples, float **audio_output, int *result_num_of_samples) {
        AVChannelLayout src_ch_layout = AV_CHANNEL_LAYOUT_MONO;
        AVChannelLayout dst_ch_layout = AV_CHANNEL_LAYOUT_MONO;
    
        struct SwrContext *swr_ctx;
        swr_ctx = swr_alloc();
        int ret;
        if (!swr_ctx) {
            fprintf(stderr, "Could not allocate resampler context\n");
            ret = AVERROR(ENOMEM);
        }
    
        av_opt_set_chlayout(swr_ctx, "in_chlayout",    &src_ch_layout, 0);
        av_opt_set_int(swr_ctx, "in_sample_rate",       input_sample_rate, 0);
        av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
    
        av_opt_set_chlayout(swr_ctx, "out_chlayout",    &dst_ch_layout, 0);
        av_opt_set_int(swr_ctx, "out_sample_rate",       output_sample_rate, 0);
        av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
    
        if ((ret = swr_init(swr_ctx)) < 0) {
            fprintf(stderr, "Failed to initialize the resampling context\n");
            return -1;
        }
    
        int output_samples_count = av_rescale_rnd(swr_get_delay(swr_ctx, input_sample_rate) + input_num_of_samples, output_sample_rate, input_sample_rate, AV_ROUND_UP);
        uint8_t **resampled_data = NULL;
        if (av_samples_alloc_array_and_samples(&resampled_data, NULL, 1, output_samples_count, AV_SAMPLE_FMT_FLT, 0) < 0) {
            fprintf(stderr, "Could not allocate resampled data\n");
            swr_free(&swr_ctx);
            return -1;
        }
    
        const uint8_t *in_samples[1] = {(const uint8_t *)audio_input};
        int frame_count = swr_convert(swr_ctx, resampled_data, output_samples_count, in_samples, input_num_of_samples);
    
        if (frame_count < 0) {
            fprintf(stderr, "Error while resampling\n");
            av_freep(&resampled_data[0]);
            free(resampled_data);
            swr_free(&swr_ctx);
            return -1;
        }
    
        *audio_output = (float *) malloc(frame_count * sizeof(float));
        if (!*audio_output) {
            fprintf(stderr, "Could not allocate memory for output\n");
            av_freep(&resampled_data[0]);
            free(resampled_data);
            swr_free(&swr_ctx);
            return -1;
        }
    
        memcpy(*audio_output, resampled_data[0], frame_count * sizeof(float));
    
        *result_num_of_samples = frame_count;
        av_freep(&resampled_data[0]);
        swr_free(&swr_ctx);
        return SUCCESS;
    }
    

    When I run tests on time lag between two files (mp3) with different sampling rates, it gives answer that differs on about 15-20 ms with right answer. Can anybody, please, help me find mistakes in the code?

    For example, I have two audios: [audio_1] (https://jmp.sh/s/USFPaGnHXVuKFVYarYpm) and [audio_2] (https://jmp.sh/s/jbmWbPTwkdDujAocmi56) - second audio is just a sample of first. The answer should be 35264 ms, but my function gives 35249 ms :(

  • ffmpeg merge audio and video from ytdl-core, outputs an mp4 with no time (no seekable)

    29 avril, par Andreu

    i'm trying to merge video and audio from ytdl-core with ffmpeg and export it to user in mp4. The output video apears without time (inifit duration) and can not seek or navigate to another point of the video.

    app.get('/downloadmp4', async (req, res)=>{
        let url = req.query.url;
            if (!ytdl.validateURL(url)) {
                return res.sendStatus(400);
            }
        let title = 'video';
            let info = await ytdl.getInfo(url);
            title = info.videoDetails.title.replace(/[^\x00-\x7F]/g, "");
            res.header('Content-Disposition', `attachment; filename="${title}.mp4"`);
    
        
              
               let video = ytdl(url,{filter:'videoonly', quality: "highest"})
               let audio = ytdl(url, {filter: 'audioonly', highWaterMark: 1<<25});
              
        
            // create the ffmpeg process for muxing
    const ffmpegProcess = cp.spawn(
        ffmpeg,
        [
              '-loglevel', '0', '-hide_banner',
          // input audio by pipe
          "-i", "pipe:3",
      
          // input video by pipe
          "-i", "pipe:4",
      
          // map audio and video correspondingly
          "-map", "0:a",
          "-map", "1:v",
      
          // change the codec
        '-c:v', 'copy',
        '-c:a', 'flac',
        //   "-c:a", "aac",
        '-crf','27',
        "-preset", "veryfast",
    
        // Allow output to be seekable, which is needed for mp4 output
        '-movflags','frag_keyframe+empty_moov',
      
          // Define output container
          '-f', 'mp4', 'pipe:5',
        ],
        {
          // no popup window for Windows users
          windowsHide: true,
          stdio: [
            // silence stdin/out, forward stderr,
            "inherit", "inherit", "inherit",
            // and pipe audio, video, output
            "pipe", "pipe", "pipe",
          ],
        }
      );
      audio.pipe(ffmpegProcess.stdio[3]);
      video.pipe(ffmpegProcess.stdio[4]);
      ffmpegProcess.stdio[5].pipe(res);
    
        })
    

    i tried various formats of video and audio and also the following command '-movflags','frag_keyframe+empty_moov', without result

  • How to get the video resolution from a File in Dart/Flutter ?

    29 avril, par Fractale

    I was using video_player to do the rest of the metadata extraction I need but I didn't find a way to get the resolution with this package.

  • How do I properly include libraries in meson ?

    29 avril, par inThe-FLesh

    I am building a project that uses the ffmpeg libav libraries and I can't seem to get meson to compile with them.

    I have tried including just the headers but then definitions can't be found. I then compiled all of the source files I needed, and that didn't work either.

    I have compiled the whole thing into objects and plan to link them but I feel there should be an easier way.

  • Why ffmpeg concat results wrong fps ? How to avoid it ?

    29 avril, par Jinglong

    I have several mp4 video files and want to concat there video streams into a new mp4 file. The command I used shows below:

    ffmpeg -f concat -safe 0 -i concat.txt -an -c:v copy -y out.mp4
    

    The fps of result does not meet expectations.

    For example, there are two input wrote in concat.txt:

    file temp_result_0.mp4
    file temp_result_1.mp4
    
    file duration number of frames fps tbr
    temp_result_0.mp4 60.0 1200 20 20
    temp_result_0.mp4 0.1 2 20 20
    out.mp4 60.1 1202 19.97 20

    The out.mp4 has correct duration, nb_frames but wrong avg_frame_rate (expecting 20 but get 6010/301).

    ng01@MacBook-Pro-3 Downloads % ffprobe -select_streams v:0 -show_entries stream=r_frame_rate,avg_frame_rate,nb_frames,duration out.mp4
    ffprobe version 7.0 Copyright (c) 2007-2024 the FFmpeg developers
      built with Apple clang version 15.0.0 (clang-1500.3.9.4)
      configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
      libavutil      59.  8.100 / 59.  8.100
      libavcodec     61.  3.100 / 61.  3.100
      libavformat    61.  1.100 / 61.  1.100
      libavdevice    61.  1.100 / 61.  1.100
      libavfilter    10.  1.100 / 10.  1.100
      libswscale      8.  1.100 /  8.  1.100
      libswresample   5.  1.100 /  5.  1.100
      libpostproc    58.  1.100 / 58.  1.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'out.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        encoder         : Lavf60.14.100
      Duration: 00:01:00.10, start: 0.000000, bitrate: 1645 kb/s
      Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1080x1920 [SAR 1:1 DAR 9:16], 1643 kb/s, 19.97 fps, 20 tbr, 10240 tbn (default)
          Metadata:
            handler_name    : VideoHandler
            vendor_id       : [0][0][0][0]
    [STREAM]
    r_frame_rate=20/1
    avg_frame_rate=6010/301
    duration=60.100000
    nb_frames=1202
    [/STREAM]
    

    I tried use lower version 4.1.3 of ffmpeg, the result has wrong fps and wrong duration. expectation duration is 60.10 but get 60.20