Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • How to fix ffmpeg 'Assertion error' error in C++

    9 mai 2019, par evn

    I'm writing a program to speed up quiet sections of videos, and I want to support videos up to 2 hours long.

    This is for a command line based program. I'm building it in XCode, then copying the compiled program to a folder with the input video and running it from terminal.

    I'm basically generating a command to run a very large complex filter based on times from ffmpeg's silence_detect feature, to speed up every other 'section' of video and then concatenate all those sections together.

    #include 
    #include 
    #include 
    #include 
    #include "CoreFoundation/CoreFoundation.h"
    
    using namespace std;
    
    int main(int argc, const char * argv[]) {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
        char path[PATH_MAX];
        if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
        {
            // error!
        }
        CFRelease(resourcesURL);
    
        /************************************/
        /***********Getting Input************/
        /************************************/
    
        chdir(path);
        std::cout << "Current Path: " << path << std::endl;
    
        for (int i = 0; i < argc; i++){
            printf("argv[%d] = %s\n", i, argv[i]);
        }
        string inpath = "input.mp4";
        string outpath = "output.mp4";
        if(argc >= 1){
            inpath = argv[1];
        }
        if(argc >= 2){
            outpath = argv[2];
        }
        string volcall = "ffmpeg -i \"" + inpath + "\" -af silencedetect=noise=-30dB:d=0.5 -f null - 2> " + inpath + "vol.txt";
        system(volcall.c_str());
    
        /************************************/
        /***********File Analysis************/
        /************************************/
        cout<<"Starting File Analysis"</loads entire document into sTotal
        while(!in.eof()) {
            getline(in, s);
            sTotal += s + "\n";
        }
    
        size_t pos = sTotal.find("silence_start"); //find location of first time
        sNew = sTotal.erase(0,pos); //delete everything prior to location found
    
        //START TIME EXTRACTION
        vector startArr;
        vector endArr;
    
        string term = "silence_start";
        int bpos = 0;
    
        while ((bpos = sNew.find(term,bpos)) != -1)
        {
            int posStart = 15 + bpos;
            string timeStart = sNew.substr (posStart,7);
    
            float timeSrt = stof (timeStart);
            startArr.push_back(timeSrt);
            cout<< (timeSrt / 2) </END TIME EXTRACTION
        string term2 = "silence_end";
        int bpos2 = 0;
    
        while ((bpos2 = sNew.find(term2,bpos2)) != -1)
        {
            int posStart = 13 + bpos2;
            string timeEnd = sNew.substr (posStart,7);
    
            float timeNd = stof (timeEnd);
            endArr.push_back(timeNd);
            cout<< (timeNd / 2) < totalArr(startArr);
    
        totalArr.insert( totalArr.end(), endArr.begin(), endArr.end() );
        sort(totalArr.begin(), totalArr.end());
    
        in.close();
    
        /************************************/
        /***********Video Editing************/
        /************************************/
    
        cout<<"Starting Video Editing"< times(totalArr); //starts with next timecode after 0, ends with last timecode 
        int numTimes = times.size();
    
        cmd= cmd + "[0:v]trim=0:" + to_string(times[0]) + ",setpts=" + to_string(first_speed) + "*(PTS-STARTPTS)[v0]; ";
        acmd= acmd + "[0:a]atrim=0:" + to_string(times[0]) + ",asetpts=PTS-STARTPTS,atempo=" + to_string(first_speed) + "[a0]; ";
    
        double mySpeed;
        int i;
        for(i = 0; i < numTimes - 1; i++){
            mySpeed = i%2==1? first_speed : second_speed;
            cmd= cmd + "[0:v]trim=" + to_string(times[0 + i]) + ":" + to_string(times[1 + i]) + ",setpts=" + to_string(1 / mySpeed) + "*(PTS-STARTPTS)[v" + to_string(i+1) + "]; ";
            acmd= acmd + "[0:a]atrim=" + to_string(times[0 + i]) + ":" + to_string(times[1 + i]) + ",asetpts=PTS-STARTPTS,atempo=" + to_string(mySpeed) + "[a" + to_string(i+1) + "]; ";
        }
        cmd = cmd + "[0:v]trim=" + to_string(times[i]) + ",setpts=" + to_string(1/(i%2==1? first_speed : second_speed)) + "*(PTS-STARTPTS)[v" + to_string(i + 1) + "]; ";
        acmd= acmd + "[0:a]atrim=" + to_string(times[i]) + ",asetpts=PTS-STARTPTS,atempo=" + to_string(i%2==1? first_speed : second_speed) + "[a" + to_string(i + 1) + "]; ";
    
        string cnc = "";
        for(int j = 0; j < i + 2; j++){
            cnc = cnc + "[v" + to_string(j) + "][a" + to_string(j) + "]";
        }
        cnc = cnc + "concat=n=" + to_string(i + 2) + ":v=1:a=1";
    
        cmd = cmd + acmd + cnc + "\" -preset superfast -profile:v baseline " + outpath;
    
        system(cmd.c_str());
        return 0;
    }
    

    I expect the output to be a video of shorter length to the input. (or equal length if the input video has no quiet parts).

    This sometimes works perfectly:

    Here is the actual command that worked in shortening Jacksfilms' 'woooooww' video from 17.200 seconds to 13.434 seconds.

    ffmpeg -loglevel verbose -i WOOOW.mp4 -filter_complex "[0:v]trim=0:0.978250,setpts=1.000000*(PTS-STARTPTS)[v0]; [0:v]trim=0.978250:2.452020,setpts=0.500000*(PTS-STARTPTS)[v1]; [0:v]trim=2.452020:3.706520,setpts=1.000000*(PTS-STARTPTS)[v2]; [0:v]trim=3.706520:5.565560,setpts=0.500000*(PTS-STARTPTS)[v3]; [0:v]trim=5.565560:7.122250,setpts=1.000000*(PTS-STARTPTS)[v4]; [0:v]trim=7.122250:8.272020,setpts=0.500000*(PTS-STARTPTS)[v5]; [0:v]trim=8.272020:8.892350,setpts=1.000000*(PTS-STARTPTS)[v6]; [0:v]trim=8.892350:10.034100,setpts=0.500000*(PTS-STARTPTS)[v7]; [0:v]trim=10.034100:11.695400,setpts=1.000000*(PTS-STARTPTS)[v8]; [0:v]trim=11.695400:13.783300,setpts=0.500000*(PTS-STARTPTS)[v9]; [0:v]trim=13.783300,setpts=1.000000*(PTS-STARTPTS)[v10]; [0:a]atrim=0:0.978250,asetpts=PTS-STARTPTS,atempo=1.000000[a0]; [0:a]atrim=0.978250:2.452020,asetpts=PTS-STARTPTS,atempo=2.000000[a1]; [0:a]atrim=2.452020:3.706520,asetpts=PTS-STARTPTS,atempo=1.000000[a2]; [0:a]atrim=3.706520:5.565560,asetpts=PTS-STARTPTS,atempo=2.000000[a3]; [0:a]atrim=5.565560:7.122250,asetpts=PTS-STARTPTS,atempo=1.000000[a4]; [0:a]atrim=7.122250:8.272020,asetpts=PTS-STARTPTS,atempo=2.000000[a5]; [0:a]atrim=8.272020:8.892350,asetpts=PTS-STARTPTS,atempo=1.000000[a6]; [0:a]atrim=8.892350:10.034100,asetpts=PTS-STARTPTS,atempo=2.000000[a7]; [0:a]atrim=10.034100:11.695400,asetpts=PTS-STARTPTS,atempo=1.000000[a8]; [0:a]atrim=11.695400:13.783300,asetpts=PTS-STARTPTS,atempo=2.000000[a9]; [0:a]atrim=13.783300,asetpts=PTS-STARTPTS,atempo=1.000000[a10]; [v0][a0][v1][a1][v2][a2][v3][a3][v4][a4][v5][a5][v6][a6][v7][a7][v8][a8][v9][a9][v10][a10]concat=n=11:v=1:a=1" -preset superfast -profile:v baseline shortWOWOUT.mp4 2> shortWOWOUT.mp4_log.txt
    

    But sometimes the code stops and returns an error:

    Assertion start_sample < end_sample || (start_sample == end_sample && !frame->nb_samples) failed at libavfilter/trim.c:303

    This seems to only happen when ffmpeg is rendering somewhere around the 3 minute mark. There does not seem to be a correlation of file size, resolution, bitrate, framerate, or length of the generated command. There does seem to be a correlation with the length of the input video, but I'm not sure what it is e.g. a 3m56s video worked fine but a 5m57 video threw an error.

    Is there a way to fix this? Or is this program doomed to fail when the input video passes some unknown cut-off time?

  • FFMPEG Hardware Encoding H264

    9 mai 2019, par nameless

    I'm currently trying to use FFMPEG with Hardware/GPU Encoding with the H264 Codec.

    What I do is, I pipe raw data direclty into ffmpeg to output them to a udp stream. Those are my settings:

    var ffmpegArgs = [
        '-c:v', 'rawvideo',// input container
        '-f', 'rawvideo',
        '-pix_fmt', 'rgba', // input pixel format
        '-s', '600x600', //input size
        '-video_size', '600x600',
        '-i', 'pipe:0', // input source
        '-f', 'mpegts', // output container format
        '-s', '600x600',
        '-video_size', '600x600',
        '-c:v', 'libx264', // output video codec
        '-b:v', '1m', // output bitrate
        'udp://239.255.123.46:1234' // output destination
    ];
    

    And in generally it is working, but with really miserable quality and latency. The frames are like 5 seconds behind and then have lots of bugs in them so it takes at least 10 or 15 seconds to see the hole frame (the video is a "live stream" from a canvas).

    However I thought that GPU Encoding might help here, but I don't get this working. I'm trying to use VAAPI, but no matter which command from ffmpeg I'm trying to use (descirbed here), it's not working....

    I'm trying to run this on a Intel NUC (this one) on an Ubuntu 16.04.

    Are there any tips on how I can get this running?

  • Live Video Facebook API with FFMPEG nodejs

    8 mai 2019, par user3709908

    I have created Object Live Video as Facebook's document.

    "stream_url": "rtmp://rtmp-api.facebook.com:80/rtmp/641310872699778?ds=1&a=AaYx3JYoFLTXAvBK"

    I using https://github.com/fluent-ffmpeg/node-fluent-ffmpeg for stream but I failed.

    Does anyone have solutions to stream video file (eg: mp4) to Object Video Facebook API?

    var ffmpeg = require('fluent-ffmpeg'),
      fs = require('fs');
    
      // open input stream
    var infs = fs.createReadStream(__dirname + '/2.mp4');
    
    infs.on('error', function(err) {
      console.log(err);
    });
    var publish = "rtmp://rtmp-api.facebook.com:80/rtmp/641310872699778?ds=1&a=AaYx3JYoFLTXAvBK";
    // make sure you set the correct path to your video file
    var proc = ffmpeg(infs)
    
      .format('mp4')
      .size('320x?')
      .videoBitrate('512k')
      .videoCodec('libx264')
      .fps(24)
      .audioBitrate('96k')
      .audioCodec('aac')
      .audioFrequency(22050)
      .audioChannels(2)
      // setup event handlers
      .on('end', function() {
        console.log('file has been converted succesfully');
      })
      .on('error', function(err) {
        console.log('an error happened: ' + err.message);
      })
      // save to stream
      .save(publish); //end = true, close output stream after writing
    

    an error happened: ffmpeg exited with code 1: rtmp://rtmp-api.facebook.com:80/rtmp/641310872699778?ds=1&a=AaYx3JYoFLTXAvBK: Operation not permitted

  • how can i merge two videos with one mp3 with ffmpeg ?

    8 mai 2019, par Magno

    I hope you can help me with on this.

    I have a code in Php where I need to merged two MP4 videos and one MP3. Right now my code replace the Audio of the video with mp3 like this example:

    ffmpeg -i "videoFile.mp4" -i "audioFile.mp3" -shortest outPutFile.mp4
    

    I have found the way to merge the two mp4 files and removing the audio using this code:

    ffmpeg -i videofile1.mp4 -i videofile2.mp4  -filter_complex "[0:v:0][1:v:0]concat=n=2:v=1[outv]" -map "[outv]"  output.mp4
    

    What I would like to do is to merge this two mp4 files in one single command with the mp3 file cause I am using a php script to achieve this, so it takes some time to render the output and I don't want to send a Sleep command in PHP to wait to finish the conversion cause I will use this in cycle in the php script.

    Any suggestions?

  • FFmpeg audio stream extraction on non-interleaved AVI - slow compared to AviSynth

    8 mai 2019, par LLL

    I want to extract the audio stream of an avi file as a wav file, it works but it is really slow (~4-5fps) although I just want to copy the stream.

    Here is the type of stream I want to extract (ffprobe info):
    Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s

    Going through AviSynth does it about 100 times faster, but I would prefer a pure FFmpeg solution. Why such a speed difference? It looks like FFmpeg is reading and processing through the whole file whereas AviSynth can just extract the data without reading it.

    Example:
    ffmpeg -i file.avi -vn -ac 2 -c:a copy audio.wav
    or
    ffmpeg -i file.avi -map 0:a -ac 2 -c:a copy audio.wav
    both work fine but take time.

    Using an AviSynth script as input:
    ffmpeg -i script.avs -map 0:a -ac 2 -c:a copy audio.wav
    with script.avs containing just:
    AviSource("file.avi")
    does the same but almost instantaneously!

    Any idea why AviSynth is so much faster and if there is a way to get the same speed in FFmpeg?

    Edit: adding logs
    Using FFmpeg directly:

    E:\>ffmpeg -i "file.avi" -map 0:a -c:a copy -y -benchmark "output.wav"
    ffmpeg version N-92936-ged3b64402e Copyright (c) 2000-2019 the FFmpeg developers
      built with gcc 8.2.1 (GCC) 20181201
      configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
      libavutil      56. 25.100 / 56. 25.100
      libavcodec     58. 43.100 / 58. 43.100
      libavformat    58. 25.100 / 58. 25.100
      libavdevice    58.  6.101 / 58.  6.101
      libavfilter     7. 47.100 /  7. 47.100
      libswscale      5.  4.100 /  5.  4.100
      libswresample   3.  4.100 /  3.  4.100
      libpostproc    55.  4.100 / 55.  4.100
    [avi @ 0000018d3c38a680] non-interleaved AVI
    Guessed Channel Layout for Input Stream #0.1 : stereo
    Input #0, avi, from 'file.avi':
      Duration: 00:18:37.49, start: 0.000000, bitrate: 534682 kb/s
        Stream #0:0: Video: rawvideo, bgr24, 1280x720, 533183 kb/s, 24.11 fps, 24.11 tbr, 24.10 tbn, 24.10 tbc
        Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Output #0, wav, to 'output.wav':
      Metadata:
        ISFT            : Lavf58.25.100
        Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Stream mapping:
      Stream #0:1 -> #0:0 (copy)
    Press [q] to stop, [?] for help
    size=  192445kB time=00:18:37.12 bitrate=1411.2kbits/s speed=4.77x
    video:0kB audio:192445kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000040%
    bench: utime=1.188s stime=50.766s rtime=234.254s
    bench: maxrss=17468kB
    

    Using AviSynth:

    E:\>ffmpeg -i "soundout.avs" -map 0:a -c:a copy -y -benchmark "output.wav"
    ffmpeg version N-92936-ged3b64402e Copyright (c) 2000-2019 the FFmpeg developers
      built with gcc 8.2.1 (GCC) 20181201
      configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
      libavutil      56. 25.100 / 56. 25.100
      libavcodec     58. 43.100 / 58. 43.100
      libavformat    58. 25.100 / 58. 25.100
      libavdevice    58.  6.101 / 58.  6.101
      libavfilter     7. 47.100 /  7. 47.100
      libswscale      5.  4.100 /  5.  4.100
      libswresample   3.  4.100 /  3.  4.100
      libpostproc    55.  4.100 / 55.  4.100
    Guessed Channel Layout for Input Stream #0.1 : stereo
    Input #0, avisynth, from 'soundout.avs':
      Duration: 00:18:37.49, start: 0.000000, bitrate: N/A
        Stream #0:0: Video: rawvideo (BGR[24] / 0x18524742), bgr24, 1280x720, 24.11 fps, 24.11 tbr, 24.10 tbn, 24.10 tbc
        Stream #0:1: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
    Output #0, wav, to 'output.wav':
      Metadata:
        ISFT            : Lavf58.25.100
        Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Stream mapping:
      Stream #0:1 -> #0:0 (copy)
    Press [q] to stop, [?] for help
    size=  192445kB time=00:18:37.11 bitrate=1411.2kbits/s speed= 155x
    video:0kB audio:192445kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000040%
    bench: utime=0.234s stime=1.047s rtime=7.236s
    bench: maxrss=23792kB
    

    Edit: tests after "reencoding" AVI file:
    Onto something...
    Say my original file is f.avi. Here is ffprobe's results:

    [avi @ 0x55a9c4b1e740] non-interleaved AVI
    Input #0, avi, from 'f.avi':
      Duration: 00:00:38.18, start: 0.000000, bitrate: 1104582 kb/s
        Stream #0:0: Video: rawvideo, bgr24, 1632x1200, 1104265 kb/s, 23.47 fps, 23.47 tbr, 23.47 tbn, 23.47 tbc
        Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
    

    Extracting audio takes a long time.
    Now if I "reencode" the file in another AVI:

    ffmpeg -i f.avi -c copy f2.avi
    

    I can extract the audio from f2.avi in milliseconds!
    FFprobe on f2.avi:

    Input #0, avi, from 'f2.avi':
      Metadata:
        encoder         : Lavf57.56.101
      Duration: 00:00:38.18, start: 0.000000, bitrate: 1104456 kb/s
        Stream #0:0: Video: rawvideo, bgr24, 1632x1200, 1104265 kb/s, 23.47 fps, 23.47 tbr, 23.47 tbn, 23.47 tbc
        Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
    

    It's the same apart from the Metadata, which shouldn't make a difference, but with this comparison I see the problem must have to do with the fact that the original is non-interleaved!
    I would assume it was easier to read and extract the audio from a non-interleaved file but maybe this is not conforming to AVI standards, hence the extra work needed?