Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • avcodec_find_encoder(AV_CODEC_ID_H264) returns null

    24 mai, par Monjura Rumi

    I am building an android application which will encode image captured from camera preview and later decode it. I am using ffmpeg library to encode and decode. To build static library with x264 I have used this tutorial. http://dl.dropbox.com/u/22605641/ffmpeg_android/main.html. As a source code of ffmpeg if I use the one downloaded from the link given in tutorial I can built it but can't build library if i use source code downloaded from here git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg. I have built library in ubuntu and using it in windows 7 in Eclipse. As I need only h264 encoder and decoder I have used following code for ffmpeg, slightly modified from tutorial.

    #!/bin/bash
    
    NDK=~/Documents/android-ndk-r8e
    PLATFORM=$NDK/platforms/android-8/arch-arm
    PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
    PREFIX=/home/android-ffmpeg
    
    function build_one
    {
        ./configure --target-os=linux --prefix=$PREFIX \
        --enable-cross-compile \
        --enable-runtime-cpudetect \
        --disable-asm \
        --arch=arm \
        --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
        --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
        --disable-stripping \
        --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
        --sysroot=$PLATFORM \
        --enable-nonfree \
        --enable-version3 \
        --disable-everything \
        --enable-gpl \
        --disable-doc \
        --enable-avresample \
        --disable-ffplay \
        --disable-ffserver \
        --enable-ffmpeg \
        --disable-ffprobe \
        --enable-avcodec \
        --enable-libx264 \
        --enable-encoder=libx264 \
        --enable-encoder=libx264rgb \
        --enable-decoder=h263 \
        --enable-decoder=h264 \
        --enable-decoder=svq3 \   
        --enable-zlib \
        --enable-gpl \
        --enable-pic \
        --disable-devices \
        --disable-avdevice \
        --extra-cflags="-I/home/android-ffmpeg/include -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=armv7-a" \
        --extra-ldflags="-L/home/android-ffmpeg/lib"
    make -j4 install
    $PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
    $PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavfilter/libavfilter.a libavresample/libavresample.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog -lx264 --warn-once --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
    }
    
    build_one
    

    After building library I have been able to build android ndk. A little part of my JNI code is here.

    JNIEXPORT jint JNICALL Java_com_example_testjava_TestjniActivity_initencoder(JNIEnv* env,jobject obj){
    
        av_register_all();
        avcodec_register_all();
        codec = avcodec_find_encoder(AV_CODEC_ID_H264);
        if (!codec) {
          __android_log_write(ANDROID_LOG_INFO, "debug", "not found");                
           return -1;
        }
    .
    .
    .
    }
    

    When I run my java source code that calls initencoder() I get -1 as return value and logcat prints "not found". That means avcodec_find_encoder() returns null and if condition is being ok. I don't know what's wrong. Why this function is returning null? I have searched a lot but did not find any solution that could guide me to right direction. some says to use avcodec_init(). But ndk-build command fails and shows error saying undefined reference to 'avcodec_init()'. I have started with library build because I thought may be I am doing wrong from the first stage. Did I make any mistake in library building like not enabling things that I should? Please help me here. This is kind of urgent for me.

  • I use ffmpeg to encode and create MPEG file from a source MP4 file. In Mac the encoding is good but in windows it creates random green pixels [closed]

    24 mai, par Amudhan R

    I use ffmpeg 3.4.2 version to encode and create an MPEG-TS file using H.264 encoding. In both Windows and Mac the ffmpeg creates the video without any error. But the resultant video from windows having a green pixels on certain frames. No idea why this is happening.

    Below is the encoding command:

    ffmpeg -i VIDEO_SOURCE.mp4 -vf scale=720:480 -c:v libx264 -b:v 2M -minrate 2M -maxrate 2M -bufsize 2M -level:v 40 -profile:v main -bf:v 2 -r 30 -crf 22 -x264-params 'keyint=3:min-keyint=3:ref=2:8x8dct=0:weightp=1:subme=6:rc-lookahead=30' -c:a aac -b:a 130k -ar 48000 -f mpegts -filter_complex alimiter=level_in=1:level_out=1:limit=0.15:attack=7:release=100:level=0 VIDEO_ENCODED.mpeg

    But the resultant video from windows having green pixels on certain frames. No idea why this is happening.

  • Efficient Video Compression with ffmpeg.js

    24 mai, par John Mark

    I attempted to upload and compress a video using ffmpeg.js. However, it appears that this process is ineffective on modern browsers. I have already implemented console logging to verify if the video compression is occurring, yet thus far, no compression has been achieved. Could anyone suggest an alternative library for video compression that might better suit the requirements?

    
    
    <script>&#xA;import { FFmpeg } from &#x27;@ffmpeg/ffmpeg&#x27;;&#xA;&#xA;export default {&#xA;  methods: {&#xA;    async handleFileInputChange(event) {&#xA;      const file = event.target.files[0];&#xA;      if (!file) return;&#xA;&#xA;      const video = this.$refs.video;&#xA;      const url = URL.createObjectURL(file);&#xA;      video.src = url;&#xA;&#xA;      const inputVideoPath = &#x27;input.mp4&#x27;;&#xA;      const outputVideoPath = &#x27;compressed_output.mp4&#x27;;&#xA;&#xA;      console.log("Compressing video...");&#xA;      await this.compressVideo(file, inputVideoPath, outputVideoPath);&#xA;      console.log("Compression completed.");&#xA;&#xA;      video.src = URL.createObjectURL(await this.downloadFile(outputVideoPath));&#xA;    },&#xA;    async compressVideo(file, inputVideoPath, outputVideoPath) {&#xA;      const ffmpeg = new FFmpeg();&#xA;      await ffmpeg.load();&#xA;&#xA;      // Writing the input file to the FFmpeg file system&#xA;      await ffmpeg.writeFile(inputVideoPath, file);&#xA;&#xA;      // Execute FFmpeg command for compression&#xA;      await ffmpeg.exec([&#x27;-i&#x27;, inputVideoPath, &#x27;-vcodec&#x27;, &#x27;libx264&#x27;, &#x27;-crf&#x27;, &#x27;28&#x27;, outputVideoPath]);&#xA;    },&#xA;    async downloadFile(filePath) {&#xA;      const ffmpeg = new FFmpeg();&#xA;      await ffmpeg.load();&#xA;&#xA;      // Read the compressed file from FFmpeg file system&#xA;      const data = await ffmpeg.readFile(filePath);&#xA;&#xA;      return new Blob([data.buffer], { type: &#x27;video/mp4&#x27; });&#xA;    }&#xA;  }&#xA;};&#xA;</script>
    
    
    
  • Decoding pcm_s16le with FFMPEG ?

    24 mai, 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 Ghosting between RGB color channels [closed]

    24 mai, par Blikpils

    This seems like a very specific question so i hope there is someone who can help me out. Within FFMPEG i am trying to add (channel pack) 3 different grayscale videos in RGB within a webm or mp4 video. I got it to work to a degree via Merge planes. This is what i tried:

    ffmpeg -y -i video1.webm -i video2.webm -i video3.webm -filter_complex "[0:v]format=gray[r]; [1:v]format=gray[g]; [2:v]format=gray[b]; [g][b][r]mergeplanes=0x001020:format=gbrp" -pix_fmt yuv420p -t 00:00:03 output.webm
    

    This is the result: enter image description here

    As you see the 9 is visible in the red and blue channel and so are each of the other channels

    I have tried just extracting a single channel in a video, change the compression change the format change the color space, make sure that its all linear but each seem to have the limitation of some smearing/artifacts between the other channels.

    Is it at all possible to add different grayscale streams in the color channels without any artifacts and if so in which direction should i look?

    Any help would be greatly appreciated!