Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Convert video while uploading

    19 septembre 2012, par dskanth

    I want to convert a video (say from wmv format to flv) while a user uploads it. I can either convert a video or upload it individually, but how to do them at once?

    Iam using ffmpeg for conversion, like:

    ffmpeg -i WMV.wmv WMV.flv

    But iam unsure about the steps to be followed to convert and upload a video. Do i need to use Ajax or something like that?

  • How give information about file with binary path /usr/bin/ffmpeg ? [closed]

    19 septembre 2012, par John Smith

    How give information about file with binary path /usr/bin/ffmpeg?

    Peoples its not answer - its code for help all peoples-)

    Long time i search the correct code, now i write here. Washes it be useful to someone.

    1) get duration file

    ob_start();
    passthru("ffmpeg-9260.exe -i \"". $videofile . "\" 2>&1");
    $duration = ob_get_contents();
    ob_end_clean();
    
    
    preg_match('/Duration: (.*?),/', $duration, $matches);
    $duration = $matches[1];
    $duration_array = split(':', $duration);
    $duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
    $time = $duration * $percent / 100;
    $time = intval($time/3600) . ":" . intval(($time-(intval($time/3600)*3600))/60) . ":" . sprintf("%01.3f", ($time-(intval($time/60)*60)));
    

    2) get a picture of a particular frame of the video (flv or avi or another)

    ffmpeg -i video.avi -an -ss 00:01:30 -r 1 -vframes 1 -s 320×240 -y -f mjpeg screenshot.jpg
    

    3) how to convert video file to format .ogg

    a) ffmpeg -an -deinterlace -s 400×300 -r 20.00 -i CapeCodMarsh.avi -vcodec rawvideo -pix_fmt yuv420p -f rawvideo – |  ffmpeg -an -f rawvideo -s 400×300 -r 20.00 -i – -f yuv4mpegpipe – |  libtheora-1.0/lt-encoder_example –video-rate-target 512k – -o tmp.ogv
    
    b)ffmpeg -y -i CapeCodMarsh.avi -vn -acodec libvorbis -ac 2 -ab 128k -ar 44100 audio.ogg
    

    4) How I can get this value for the buffer?

    var v = document.getElementById('file_id');
    var r = v.buffered.end(0);
    

    Enjoy=)

  • PHP ffmpeg how give information about video file if binary path /usr/bin/ffmpeg ? [closed]

    19 septembre 2012, par John Smith

    Tell me please how give information about video file if binary path /usr/bin/ffmpeg ? me need code on php. Found not give results for this question...

  • How to create a video from still images but with low framerate (like a slideshow) ?

    18 septembre 2012, par AKE

    I have some still images which I would like to have in a video that shows each image for a reasonable length of time, say 1 second, or 0.5 second.

    I'm using the command:

    ffmpeg -f image2 -r 1 "Imgp%004d.jpg" -s 640x480 -b:v 1024k result.avi
    

    where -r 1 supposedly sets the framerate to 1 fps.

    This does indeed produce a result (avi), but playback (on vlc) gets stuck on the first frame and never advances!

    If I use -r 3 instead, I get a result that plays a bit too fast, i.e. at 3 frames per second. Moreover, it doesn't include all of the still images.

    Two questions:

    1) How can I force ffmpeg to compile the video at 1 fps?

    2) How can I force all 7 frames to appear? (Preferably without duplicating frames, i.e. trial and error)

    I suspect I'm missing something basic around the relationship between perhaps input read rates, output frame rates, and number of frames to be included.

    Would appreciate any insights.

  • how to stream h.264 video with mp3 audio using libavcodec ?

    18 septembre 2012, par dasg

    I read h.264 frames from webcamera and capture audio from microphone. I need to stream live video to ffserver. During debug I read video from ffserver using ffmpeg with following command:

    ffmpeg -i http://127.0.0.1:12345/robot.avi -vcodec copy -acodec copy out.avi
    

    My video in output file is slightly accelerated. If I add a audio stream it is accelerated several times. Sometimes there is no audio in the output file.

    Here is my code for encoding audio:

    #include "v_audio_encoder.h"
    
    extern "C" {
    #include avcodec.h>
    }
    #include 
    
    struct VAudioEncoder::Private
    {
        AVCodec *m_codec;
        AVCodecContext *m_context;
    
        std::vector m_outBuffer;
    };
    
    VAudioEncoder::VAudioEncoder( int sampleRate, int bitRate )
    {
        d = new Private( );
        d->m_codec = avcodec_find_encoder( CODEC_ID_MP3 );
        assert( d->m_codec );
        d->m_context = avcodec_alloc_context3( d->m_codec );
    
        // put sample parameters
        d->m_context->channels = 2;
        d->m_context->bit_rate = bitRate;
        d->m_context->sample_rate = sampleRate;
        d->m_context->sample_fmt = AV_SAMPLE_FMT_S16;
        strcpy( d->m_context->codec_name, "libmp3lame" );
    
        // open it
        int res = avcodec_open2( d->m_context, d->m_codec, 0 );
        assert( res >= 0 );
    
        d->m_outBuffer.resize( d->m_context->frame_size );
    }
    
    VAudioEncoder::~VAudioEncoder( )
    {
        avcodec_close( d->m_context );
        av_free( d->m_context );
        delete d;
    }
    
    void VAudioEncoder::encode( const std::vector& samples, std::vector& outbuf )
    {
        assert( (int)samples.size( ) == d->m_context->frame_size );
    
        int outSize = avcodec_encode_audio( d->m_context, d->m_outBuffer.data( ),
                                            d->m_outBuffer.size( ), reinterpret_cast( samples.data( ) ) );
        if( outSize ) {
            outbuf.resize( outSize );
            memcpy( outbuf.data( ), d->m_outBuffer.data( ), outSize );
        }
        else
            outbuf.clear( );
    }
    
    int VAudioEncoder::getFrameSize( ) const
    {
        return d->m_context->frame_size;
    }
    

    Here is my code for streaming video:

    #include "v_out_video_stream.h"
    
    extern "C" {
    #include avformat.h>
    #include opt.h>
    #include avstring.h>
    #include avio.h>
    }
    
    #include 
    #include 
    
    struct VStatticRegistrar
    {
        VStatticRegistrar( )
        {
            av_register_all( );
            avformat_network_init( );
        }
    };
    
    VStatticRegistrar __registrar;
    
    struct VOutVideoStream::Private
    {
        AVFormatContext * m_context;
        int m_videoStreamIndex;
        int m_audioStreamIndex;
    
        int m_videoBitrate;
        int m_width;
        int m_height;
        int m_fps;
        int m_bitrate;
    
        bool m_waitKeyFrame;
    };
    
    VOutVideoStream::VOutVideoStream( int width, int height, int fps, int bitrate )
    {
        d = new Private( );
        d->m_width = width;
        d->m_height = height;
        d->m_fps = fps;
        d->m_context = 0;
        d->m_videoStreamIndex = -1;
        d->m_audioStreamIndex = -1;
        d->m_bitrate = bitrate;
        d->m_waitKeyFrame = true;
    }
    
    bool VOutVideoStream::connectToServer( const std::string& uri )
    {
        assert( ! d->m_context );
    
        // initalize the AV context
        d->m_context = avformat_alloc_context();
        if( !d->m_context )
            return false;
        // get the output format
        d->m_context->oformat = av_guess_format( "ffm", NULL, NULL );
        if( ! d->m_context->oformat )
            return false;
    
        strcpy( d->m_context->filename, uri.c_str( ) );
    
        // add an H.264 stream
        AVStream *stream = avformat_new_stream( d->m_context, NULL );
        if ( ! stream )
            return false;
        // initalize codec
        AVCodecContext* codec = stream->codec;
        if( d->m_context->oformat->flags & AVFMT_GLOBALHEADER )
            codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
        codec->codec_id = CODEC_ID_H264;
        codec->codec_type = AVMEDIA_TYPE_VIDEO;
        strcpy( codec->codec_name, "libx264" );
    //    codec->codec_tag = ( unsigned('4') << 24 ) + (unsigned('6') << 16 ) + ( unsigned('2') << 8 ) + 'H';
        codec->width = d->m_width;
        codec->height = d->m_height;
        codec->time_base.den = d->m_fps;
        codec->time_base.num = 1;
        codec->bit_rate = d->m_bitrate;
        d->m_videoStreamIndex = stream->index;
    
        // add an MP3 stream
        stream = avformat_new_stream( d->m_context, NULL );
        if ( ! stream )
            return false;
        // initalize codec
        codec = stream->codec;
        if( d->m_context->oformat->flags & AVFMT_GLOBALHEADER )
            codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
        codec->codec_id = CODEC_ID_MP3;
        codec->codec_type = AVMEDIA_TYPE_AUDIO;
        strcpy( codec->codec_name, "libmp3lame" );
        codec->sample_fmt = AV_SAMPLE_FMT_S16;
        codec->channels = 2;
        codec->bit_rate = 64000;
        codec->sample_rate = 44100;
        d->m_audioStreamIndex = stream->index;
    
        // try to open the stream
        if( avio_open( &d->m_context->pb, d->m_context->filename, AVIO_FLAG_WRITE ) < 0 )
             return false;
    
        // write the header
        return avformat_write_header( d->m_context, NULL ) == 0;
    }
    
    void VOutVideoStream::disconnect( )
    {
        assert( d->m_context );
    
        avio_close( d->m_context->pb );
        avformat_free_context( d->m_context );
        d->m_context = 0;
    }
    
    VOutVideoStream::~VOutVideoStream( )
    {
        if( d->m_context )
            disconnect( );
        delete d;
    }
    
    int VOutVideoStream::getVopType( const std::vector& image )
    {
        if( image.size( ) < 6 )
            return -1;
        unsigned char *b = (unsigned char*)image.data( );
    
        // Verify NAL marker
        if( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] ) {
            ++b;
            if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] )
                return -1;
        }
    
        b += 3;
    
        // Verify VOP id
        if( 0xb6 == *b ) {
            ++b;
            return ( *b & 0xc0 ) >> 6;
        }
    
        switch( *b ) {
        case 0x65: return 0;
        case 0x61: return 1;
        case 0x01: return 2;
        }
    
        return -1;
    }
    
    bool VOutVideoStream::sendVideoFrame( std::vector& image )
    {
        // Init packet
        AVPacket pkt;
        av_init_packet( &pkt );
        pkt.flags |= ( 0 >= getVopType( image ) ) ? AV_PKT_FLAG_KEY : 0;
    
        // Wait for key frame
        if ( d->m_waitKeyFrame ) {
            if( pkt.flags & AV_PKT_FLAG_KEY )
                d->m_waitKeyFrame = false;
            else
                return true;
        }
    
        pkt.stream_index = d->m_videoStreamIndex;
        pkt.data = image.data( );
        pkt.size = image.size( );
        pkt.pts = pkt.dts = AV_NOPTS_VALUE;
    
        return av_write_frame( d->m_context, &pkt ) >= 0;
    }
    
    bool VOutVideoStream::sendAudioFrame( std::vector& audio )
    {
        // Init packet
        AVPacket pkt;
        av_init_packet( &pkt );
        pkt.stream_index = d->m_audioStreamIndex;
        pkt.data = audio.data( );
        pkt.size = audio.size( );
        pkt.pts = pkt.dts = AV_NOPTS_VALUE;
    
        return av_write_frame( d->m_context, &pkt ) >= 0;
    }
    

    Here is how I use it:

    BOOST_AUTO_TEST_CASE(testSendingVideo)
    {
        const int framesToGrab = 90000;
    
        VOutVideoStream stream( VIDEO_WIDTH, VIDEO_HEIGHT, FPS, VIDEO_BITRATE );
        if( stream.connectToServer( URI ) ) {
            VAudioEncoder audioEncoder( AUDIO_SAMPLE_RATE, AUDIO_BIT_RATE );
            VAudioCapture microphone( MICROPHONE_NAME, AUDIO_SAMPLE_RATE, audioEncoder.getFrameSize( ) );
    
            VLogitecCamera camera( VIDEO_WIDTH, VIDEO_HEIGHT );
            BOOST_REQUIRE( camera.open( CAMERA_PORT ) );
            BOOST_REQUIRE( camera.startCapturing( ) );
    
            std::vector image, encodedAudio;
            std::vector voice;
            boost::system_time startTime;
            int delta;
            for( int i = 0; i < framesToGrab; ++i ) {
                startTime = boost::posix_time::microsec_clock::universal_time( );
    
                BOOST_REQUIRE( camera.read( image ) );
                BOOST_REQUIRE( microphone.read( voice ) );
                audioEncoder.encode( voice, encodedAudio );
    
                BOOST_REQUIRE( stream.sendVideoFrame( image ) );
                BOOST_REQUIRE( stream.sendAudioFrame( encodedAudio ) );
    
                delta = ( boost::posix_time::microsec_clock::universal_time( ) - startTime ).total_milliseconds( );
                if( delta < 1000 / FPS )
                    boost::thread::sleep( startTime + boost::posix_time::milliseconds( 1000 / FPS - delta ) );
            }
    
            BOOST_REQUIRE( camera.stopCapturing( ) );
            BOOST_REQUIRE( camera.close( ) );
        }
        else
            std::cout << "failed to connect to server" << std::endl;
    }
    

    I think my problem is in PTS and DTS. Can anyone help me?