Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Ffmpeg with nginx-rtmp module requires several restarts to start working

    21 janvier, par KonstSergP

    I have rtmp stream created with

    ffmpeg -i rtp://192.168.1.74:8000/live -vcodec libx264 -f flv rtmp://192.168.1.74:1935/picam/stream

    but after command Start it will not send video rtmpdump -z.

    If I restart ffmpeg 7-8 times it might work. There is how it look in wireshark when I want to see video from other device: wireshark

    I have recompiled nginx from source but it has no effect Port 1935 is opened

    Please help

  • How to Add Spacing Between Bars in showfreqs Visualization in FFmpeg

    21 janvier, par Jad Alaouie

    I'm trying to create a video visualization using FFmpeg that shows an audio waveform with bars representing frequency data using the showfreqs filter. However, I would like to add some space between the bars in the bar chart-style visualization, but the showfreqs filter doesn't seem to provide a built-in option for controlling the spacing.

    filter_complex = (
            # Create bar chart style visualization with reduced overlap for spacing
            "[0:a]aformat=channel_layouts=mono," +
            "showfreqs=mode=bar:ascale=log:fscale=log:win_size=900:size=1920x300:" +  # Adjust win_size for overall effect
            "colors=white:win_func=blackman:overlap=0.05[vis];" +  # Reduce overlap
            # Insert space by scaling and resizing the visualization
            "[1][vis]overlay=(W-w)/2:(H-h)/2-150:format=auto[bg_with_wave];" +
            # Add play button
            "[2]scale={0}:{1}[play_button];" +
            # Combine everything, with added space between bars by manipulating output size
            "[bg_with_wave][play_button]overlay=(W-w)/2:(H-h)/2:format=auto[v];" +
            "[v]scale=1.2*iw:1.2*ih[v_rescaled]"  # Increase space by scaling video output
        ).format(play_button_width, play_button_height)
    
  • Decoding pcm_s16le with FFMPEG ?

    21 janvier, par Davide Caresia

    i have a problem decoding a wav file using ffmpeg. I'm new to it and i'm not quite used to it.

    In my application i have to input the audio file and get an array of samples to work on. I used ffmpeg to create a function that gets in input the path of the file, the position in time where to start to output the samples and the lenght of the chunk to decode in seconds.

    I have no reputation, so I had to make a gdrive directory where you can see the problem and the files on which I worked.

    Here it is: https://goo.gl/8KnjAj

    When I try to decode the file harp.wav everything runs fine, and I can plot the samples as in the image plot-harp.png

    The file is a WAV file encoded as: pcm_u8, 11025 Hz, 1 channels, u8, 88 kb/s

    The problems comes when i try to decode the file demo-unprocessed.wav. It outputs a series of samples that has no sense. It outputs a serie of samples plotted as the image graph1-demo.jpg shows.

    The file is a WAV file encoded as: pcm_s16le, 44100 Hz, 1 channels, s16, 705 kb/s

    IDK where the problem in my code is, I already checked the code before and after the decoding with FFMPEG, and it works absolutely fine.

    Here is the code for the dataReader.cpp :

    /* Start by including the necessary */
    #include "dataReader.h"
    #include 
    #include 
    #include 
    
    #ifdef __cplusplus
    extern "C" {
    #endif
        #include avcodec.h> 
        #include avformat.h>
        #include avutil.h>
    #ifdef __cplusplus 
    }
    #endif
    
    using namespace std;
    
    /* initialization function for audioChunk */
    audioChunk::audioChunk(){
        data=NULL;
        size=0;
        bitrate=0;
    }
    
    /* function to get back chunk lenght in seconds */
    int audioChunk::getTimeLenght(){
        return size/bitrate;
    }
    
    /* initialization function for audioChunk_dNorm */
    audioChunk_dNorm::audioChunk_dNorm(){
        data=NULL;
        size=0;
        bitrate=0;
    }
    
    /* function to get back chunk lenght in seconds */
    int audioChunk_dNorm::getTimeLenght(){
        return size/bitrate;
    }
    
    /* function to normalize audioChunk into audioChunk_dNorm */
    void audioChunk_dNorm::fillAudioChunk(audioChunk* cnk){
    
        size=cnk->size;
        bitrate=cnk->bitrate;
    
        double min=cnk->data[0];
        double max=cnk->data[0];
    
        for(int i=0;isize;i++){
            if(*(cnk->data+i)>max) max=*(cnk->data+i);
            else if(*(cnk->data+i)data+i);
        }
    
        data=new double[size];
    
        for(int i=0;i/data[i]=cnk->data[i]+256*data[i+1];
            if(data[i]!=255) data[i]=2*((cnk->data[i])-(max-min)/2)/(max-min);
            else data[i]=0;
        }
        cout<<"bitrate "<* inizialize audioChunk */
        audioChunk output;
    
        /* Check input times */
        if((start_time<0)||(lenght<0)) {
            cout<<"Input times should be positive";
            return output;
        }
    
        /* Start FFmpeg */
        av_register_all();
    
        /* Initialize the frame to read the data and verify memory allocation */
        AVFrame* frame = av_frame_alloc();
        if (!frame)
        {
            cout << "Error allocating the frame" << endl;
            return output;
        }
    
        /* Initialization of the Context, to open the file */
        AVFormatContext* formatContext = NULL;
        /* Opening the file, and check if it has opened */
        if (avformat_open_input(&formatContext, path_name, NULL, NULL) != 0)
        {
            av_frame_free(&frame);
            cout << "Error opening the file" << endl;
            return output;
        }
    
        /* Find the stream info, if not found, exit */
        if (avformat_find_stream_info(formatContext, NULL) < 0)
        {
            av_frame_free(&frame);
            avformat_close_input(&formatContext);
            cout << "Error finding the stream info" << endl;
            return output;
        }
    
        /* Check inputs to verify time input */
        if(start_time>(formatContext->duration/1000000)){
            cout<< "Error, start_time is over file duration"<* Chunk = number of samples to output */
        long long int chunk = ((formatContext->bit_rate)*lenght/8);
        /* Start = address of sample where start to read */
        long long int start = ((formatContext->bit_rate)*start_time/8);
        /* Tot_sampl = number of the samples in the file */
        long long int tot_sampl = (formatContext->bit_rate)*(formatContext->duration)/8000000;
    
        /* Set the lenght of chunk to avoid segfault and to read all the file */
        if (start+chunk>tot_sampl) {chunk = tot_sampl-start;}
        if (lenght==0) {start = 0; chunk = tot_sampl;}
    
        /* initialize the array to output */
        output.data = new unsigned char[chunk];
        output.bitrate = formatContext->bit_rate;
        output.size=chunk;
    
        av_dump_format(formatContext,0,NULL,0);
        cout<* Find the audio Stream, if no audio stream are found, clean and exit */
        AVCodec* cdc = NULL;
        int streamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &cdc, 0);
        if (streamIndex < 0)
        {
            av_frame_free(&frame);
            avformat_close_input(&formatContext);
            cout << "Could not find any audio stream in the file" << endl;
            return output;
        }
    
        /* Open the audio stream to read data  in audioStream */
        AVStream* audioStream = formatContext->streams[streamIndex];
    
        /* Initialize the codec context */
        AVCodecContext* codecContext = audioStream->codec;
        codecContext->codec = cdc;
        /* Open the codec, and verify if it has opened */
        if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
        {
            av_frame_free(&frame);
            avformat_close_input(&formatContext);
            cout << "Couldn't open the context with the decoder" << endl;
            return output;
        }
    
        /* Initialize buffer to store compressed packets */
        AVPacket readingPacket;
        av_init_packet(&readingPacket);
    
    
        int j=0;
        int count = 0; 
    
        while(av_read_frame(formatContext, &readingPacket)==0){
            if((count+readingPacket.size)>start){
                if(readingPacket.stream_index == audioStream->index){
    
                    AVPacket decodingPacket = readingPacket;
    
                    // Audio packets can have multiple audio frames in a single packet
                    while (decodingPacket.size > 0){
                        // Try to decode the packet into a frame
                        // Some frames rely on multiple packets, so we have to make sure the frame is finished before
                        // we can use it
                        int gotFrame = 0;
                        int result = avcodec_decode_audio4(codecContext, frame, &gotFrame, &decodingPacket);
    
                        count += result;
    
                        if (result >= 0 && gotFrame)
                        {
                            decodingPacket.size -= result;
                            decodingPacket.data += result;
                            int a;
    
                            for(int i=0;idata[0][i];
    
                                j++;
                                if(j>=chunk) break;
                            }
    
                            // We now have a fully decoded audio frame
                        }
                        else
                        {
                            decodingPacket.size = 0;
                            decodingPacket.data = NULL;
                        }
                        if(j>=chunk) break;
                    }
                }              
            }else count+=readingPacket.size;
    
            // To prevent memory leak, must free packet.
            av_free_packet(&readingPacket);
            if(j>=chunk) break;
        }
    
        // Some codecs will cause frames to be buffered up in the decoding process. If the CODEC_CAP_DELAY flag
        // is set, there can be buffered up frames that need to be flushed, so we'll do that
        if (codecContext->codec->capabilities & CODEC_CAP_DELAY)
        {
            av_init_packet(&readingPacket);
            // Decode all the remaining frames in the buffer, until the end is reached
            int gotFrame = 0;
            int a;
            int result=avcodec_decode_audio4(codecContext, frame, &gotFrame, &readingPacket);
            while (result >= 0 && gotFrame)
            {
                // We now have a fully decoded audio frame
                for(int i=0;idata[0][i];
    
                    j++;
                    if(j>=chunk) break;
                }
                if(j>=chunk) break;
            }
        }
    
        // Clean up!
        av_free(frame);
        avcodec_close(codecContext);
        avformat_close_input(&formatContext);
    
        cout<<"Ended Reading, "<code>

    Here is the dataReader.h

    /* 
     * File:   dataReader.h
     * Author: davide
     *
     * Created on 27 luglio 2015, 11.11
     */
    
    #ifndef DATAREADER_H
    #define DATAREADER_H
    
    /* function that reads a file and outputs an array of samples
     * @ path_name = the path of the file to read
     * @ start_time = the position where to start the data reading, 0 = start
     *                the time is in seconds, it can hold to 10e-6 seconds
     * @ lenght = the lenght of the frame to extract the data, 
     *            0 = read all the file (do not use with big files)
     *            if lenght > of file duration, it reads through the end of file.
     *            the time is in seconds, it can hold to 10e-6 seconds  
     */
    
    #include 
    
    class audioChunk{
    public:
        uint8_t *data;
        unsigned int size;
        int bitrate;
        int getTimeLenght();
        audioChunk();
    };
    
    class audioChunk_dNorm{
    public:
        double* data;
        unsigned int size;
        int bitrate;
        int getTimeLenght();
        void fillAudioChunk(audioChunk* cnk);
        audioChunk_dNorm();
    };
    
    audioChunk readData(const char* path_name, const double start_time, const double lenght);
    
    #endif  /* DATAREADER_H */
    

    And finally there is the main.cpp of the application.

    /* 
     * File:   main.cpp
     * Author: davide
     *
     * Created on 28 luglio 2015, 17.04
     */
    
    #include 
    #include "dataReader.h"
    #include "transforms.h"
    #include "tognuplot.h"
    #include 
    #include 
    
    using namespace std;
    
    /*
     * 
     */
    int main(int argc, char** argv) {
    
        audioChunk *chunk1=new audioChunk;
    
        audioChunk_dNorm *normChunk1=new audioChunk_dNorm;
    
        *chunk1=readData("./audio/demo-unprocessed.wav",0,1);
    
        normChunk1->fillAudioChunk(chunk1);
    
        ofstream file1;
        file1.open("./file/2wave.txt", std::ofstream::trunc);
        if(file1.is_open()) {
            for(int i=0;isize;i++) {
                int a=chunk1->data[i];
                file1<code>

    I can't understand why the outputs goes like this. Is it possible that the decoder can't convert the samples (pcm_16le, 16bits) into FFMPEG AVFrame.data, that stores the samples ad uint8_t? And if it is it is there some way to make FFMPEG work for audio files that stores samples at more than 8 bits?

    The file graph1-demo_good.jpg is how the samples should be, extracted with a working LIBSNDFILE application that I made.

    EDIT: Seems like the program can't convert the decoded data, couples of little endian bytes stored in a couple of uint8_t unsigned char, into the destination format (that i set as unsigned char[]), because it stores the bits as little-endian 16 bytes. So the data into audioChunk.data is right, but I have to read it not as an unsigned char, but as a couple of little-endian bytes.

  • FFmpeg and reserved color primaries [closed]

    21 janvier, par Yoz

    I am trying to get thumbnails from a hevc video downloaded from https://github.com/stashapp/stash/issues/4124#issuecomment-1720057183 and it works with most recent ffmpeg 7.1 (installed via homebrew on mac) printing:

    ffmpeg -i input.mp4 -frames:v 1 out.jpg
    
    ffmpeg version 7.1 Copyright (c) 2000-2024 the FFmpeg developers
      built with Apple clang version 16.0.0 (clang-1600.0.26.4)
      configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.1_4 --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-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
      libavutil      59. 39.100 / 59. 39.100
      libavcodec     61. 19.100 / 61. 19.100
      libavformat    61.  7.100 / 61.  7.100
      libavdevice    61.  3.100 / 61.  3.100
      libavfilter    10.  4.100 / 10.  4.100
      libswscale      8.  3.100 /  8.  3.100
      libswresample   5.  3.100 /  5.  3.100
      libpostproc    58.  3.100 / 58.  3.100
    [hevc @ 0x134f07530] VPS 0 does not exist
    [hevc @ 0x134f07530] SPS 0 does not exist.
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
      Metadata:
        major_brand     : mp42
        minor_version   : 512
        compatible_brands: mp42iso2mp41
        creation_time   : 2023-09-14T19:46:05.000000Z
        encoder         : HandBrake 1.5.1 2022011000
      Duration: 00:01:26.05, start: 0.000000, bitrate: 231 kb/s
      Stream #0:0[0x1](und): Video: hevc (Main) (hvc1 / 0x31637668), yuv420p(tv, bt709/reserved/bt709), 648x648 [SAR 1:1 DAR 1:1], 188 kb/s, 30 fps, 30 tbr, 90k tbn (default)
          Metadata:
            creation_time   : 2023-09-14T19:46:05.000000Z
            handler_name    : VideoHandler
            vendor_id       : [0][0][0][0]
      Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 36 kb/s (default)
          Metadata:
            creation_time   : 2023-09-14T19:46:05.000000Z
            handler_name    : Mono
            vendor_id       : [0][0][0][0]
    [hevc @ 0x1358065c0] VPS 0 does not exist
    [hevc @ 0x1358065c0] SPS 0 does not exist.
    Stream mapping:
      Stream #0:0 -> #0:0 (hevc (native) -> mjpeg (native))
    Press [q] to stop, [?] for help
    Output #0, image2, to 'out.jpg':
      Metadata:
        major_brand     : mp42
        minor_version   : 512
        compatible_brands: mp42iso2mp41
        encoder         : Lavf61.7.100
      Stream #0:0(und): Video: mjpeg, yuv420p(pc, bt709/reserved/bt709, progressive), 648x648 [SAR 1:1 DAR 1:1], q=2-31, 200 kb/s, 30 fps, 30 tbn (default)
          Metadata:
            creation_time   : 2023-09-14T19:46:05.000000Z
            handler_name    : VideoHandler
            vendor_id       : [0][0][0][0]
            encoder         : Lavc61.19.100 mjpeg
          Side data:
            cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
    [image2 @ 0x134f16080] The specified filename 'out.jpg' does not contain an image sequence pattern or a pattern is invalid.
    [image2 @ 0x134f16080] Use a pattern such as %03d for an image sequence or use the -update option (with -frames:v 1 if needed) to write a single image.
    [out#0/image2 @ 0x134f10480] video:5KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: unknown
    frame=    1 fps=0.0 q=5.1 Lsize=N/A time=00:00:00.03 bitrate=N/A speed=4.07x   
    

    however, when I use custom compiled ffmpeg.wasm it fails with:

    ffmpeg version N-118050-ga518b5540d Copyright (c) 2000-2024 the FFmpeg developers
      built with emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.73 (ac676d5e437525d15df5fd46bc2c208ec6d376a3)
      configuration: --target-os=none --arch=x86_32 --enable-cross-compile --enable-version3 --enable-zlib --enable-libaom --disable-encoder=libaom_av1 --enable-libopenh264 --enable-libkvazaar --enable-libvpx --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libopus --enable-libwebp --enable-libsvtav1 --enable-librubberband --disable-x86asm --disable-inline-asm --disable-stripping --disable-programs --disable-doc --disable-debug --disable-runtime-cpudetect --disable-autodetect --extra-cflags='-O3 -flto -I/ffmpeg-wasm/build/include -pthread -msimd128' --extra-cxxflags='-O3 -flto -I/ffmpeg-wasm/build/include -pthread -msimd128' --extra-ldflags='-O3 -flto -I/ffmpeg-wasm/build/include -pthread -msimd128 -L/ffmpeg-wasm/build/lib' --pkg-config-flags=--static --nm=emnm --ar=emar --ranlib=emranlib --cc=emcc --cxx=em++ --objcc=emcc --dep-cc=emcc --enable-gpl --enable-libx264 --enable-libx265
      libavutil      59. 49.100 / 59. 49.100
      libavcodec     61. 26.100 / 61. 26.100
      libavformat    61.  9.100 / 61.  9.100
      libavdevice    61.  4.100 / 61.  4.100
      libavfilter    10.  6.101 / 10.  6.101
      libswscale      8. 12.100 /  8. 12.100
      libswresample   5.  4.100 /  5.  4.100
      libpostproc    58.  4.100 / 58.  4.100
    [hevc @ 0x38d0000] VPS 0 does not exist
    [hevc @ 0x38d0000] SPS 0 does not exist.
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
      Metadata:
        major_brand     : mp42
        minor_version   : 512
        compatible_brands: mp42iso2mp41
        creation_time   : 2023-09-14T19:46:05.000000Z
        encoder         : HandBrake 1.5.1 2022011000
      Duration: 00:01:26.05, start: 0.000000, bitrate: 231 kb/s
      Stream #0:0[0x1](und): Video: hevc (Main) (hvc1 / 0x31637668), yuv420p(tv, bt709/reserved/bt709), 648x648 [SAR 1:1 DAR 1:1], 188 kb/s, 30 fps, 30 tbr, 90k tbn (default)
        Metadata:
          creation_time   : 2023-09-14T19:46:05.000000Z
          handler_name    : VideoHandler
          vendor_id       : [0][0][0][0]
      Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 36 kb/s (default)
        Metadata:
          creation_time   : 2023-09-14T19:46:05.000000Z
          handler_name    : Mono
          vendor_id       : [0][0][0][0]
    [hevc @ 0x38d0300] VPS 0 does not exist
    [hevc @ 0x38d0300] SPS 0 does not exist.
    Stream mapping:
      Stream #0:0 -> #0:0 (hevc (native) -> mjpeg (native))
    Press [q] to stop, [?] for help
    [swscaler @ 0x8ca0000] Unsupported input (Not supported): fmt:yuv420p csp:bt709 prim:reserved trc:bt709 -> fmt:yuv420p csp:bt709 prim:reserved trc:bt709
    [vf#0:0 @ 0x3830900] Error while filtering: Not supported
    [vf#0:0 @ 0x3830900] Task finished with error code: -138 (Not supported)
    [vost#0:0/mjpeg @ 0x385ae40] [enc:mjpeg @ 0x3878b80] Could not open encoder before EOF
    [vf#0:0 @ 0x3830900] Terminating thread with return code -138 (Not supported)
    [vost#0:0/mjpeg @ 0x385ae40] Task finished with error code: -28 (Invalid argument)
    [vost#0:0/mjpeg @ 0x385ae40] Terminating thread with return code -28 (Invalid argument)
    [out#0/image2 @ 0x3851580] Nothing was written into output file, because at least one of its streams received no packets.
    frame=    0 fps=0.0 q=0.0 Lsize=       0KiB time=N/A bitrate=N/A speed=N/A    
    Conversion failed!
    Process finished with exit code -138.
    

    I figured out the issue is color primaries prim:reserved, and the command can be updated to a working one by re-writing input primaries as following:

    ffmpeg -i input.mp4 -vf "colorspace=all=bt709:iprimaries=bt709" -frames:v 1 out.jpg
    

    However, I would like to compile ffmpeg.wasm so that it handles reserved primaries just like the one from homebrew.

    Any idea what the compiled ffmpeg.wasm is missing?

  • How to get the information of bit-rate from YouTube videos ?

    21 janvier, par Ashutosh Singla

    I was using YouTube videos for my test and I was wondering how can I get the information of bit-rate of the played video?

    I used 2 methods to know the information about the bit-rate but didn't get any information.

    1. Right-click on a video and choose "Stats for nerds".
    2. ffmpeg -i input_video -f ffmetadata metadata.txt
    

    I don't know if by doing the right click on the video and then properties, then details would give me the correct way of showing the bit-rate.

    Any suggestions?