Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • FFmpeg file conversion exceeds maximum execution time

    5 mai 2014, par Paul Ledger

    I have a file upload system the checks the file format, etc and converts to an mp4 if necessary. This works fine as long as the video file(s) total length is less than 30 seconds. I have been testing this two short clips about 10 seconds each and it work fine but when I test this with a clip that this 33 seconds I get the error :

    Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\own_it_all\global.func\file_upload.php on line 59

    I could just increase the maximum execution time in the php.ini file but as the max length of a video is 20 mins this wouldn't seem very user friendly making the user wait 20 mins per video. Is there a way of converting the video instantly or as near as?

    This is the exec cmd i have:

    $cmd = "ffmpeg -i $input -vcodec h264 -acodec aac -strict -2 $o";
    

    As the up-loader allows multiple uploads this is inside a for loop.

    foreach($_FILES['file']['name'] as $key => $name){
            if($_FILES['file']['error'][$key] === 0){
                $temp = $_FILES['file']['tmp_name'][$key];
                $ext = explode('.',$name);
                $ext = strtolower(end($ext));
                $_file = md5($temp).time();
                $file = $_file.'.'.$ext;
                if(in_array($ext,$allowed) === true &&  move_uploaded_file($temp,"../uploads/{$file}") === true){
                    $file_type = explode('/',$_FILES['file']['type'][$key]);
                    if($file_type[0] === 'image'){
                        $succedeed[] = array('name' => $name,'file' => $file, 'type' => 'image');               
                    }else{
                        $ffmpeg = 'ffmpeg';
                        $output = dirname(__DIR__).'/uploads/thumbs/'.$_file.'.jpg';
                        $input = dirname(__DIR__).'/uploads/'.$file;
                        $mov = new ffmpeg_movie($input);
                        $d =  $mov->getDuration();
                        $iscopy = $mov->getCopyright();
                        $h = $mov->getFrameHeight();
                        $w = $mov->getFrameWidth();
                        $pos = ceil((int)$d /3);
                        $size = $w.'x'.$h;
                        $i = explode('.',$input);
                        $o = $i[0].'.mp4';
                        if(ceil($d) < 1200){
                            if($ext != 'mp4'){
                                $cmd = "ffmpeg -i $input -vcodec h264 -acodec aac -strict -2 $o";
                                //$cmd = "ffmpeg -i $input -vcodec h264 -acodec aac -s $size $o";
                                shell_exec($cmd);
                                $toclear[] = array('file' => $file);
                            }
                            $cmd = "ffmpeg -ss $pos -i $o -an -s $size $output";
                            shell_exec($cmd);
                            $total_time += $pos;
                            $succedeed[] = array('name' => $name,'file' => 'thumbs/'.$_file.'.jpg', 'type' => 'mp4');                           
    
                        }else{
                            $failed[] = array('name' => $name, 'file' => $file, 'error' => 'Video length cannot exceed 20mins.');
                        }           
                    }
    
                }else{
                    $failed[] = array('name' => $name, 'file' => $file, 'error' => 'File type not allowed');
                }
            }
        }
    
  • How to write H264 raw stream into mp4 using ffmpeg directly

    5 mai 2014, par Yelsin

    I want to wrap the H264 Nalus(x264 encoded) into mp4 using ffmpeg(SDK 2.1), but the output mp4 file could not play. I don't know how to set the pts and dts. Here's my code, using the code from Raw H264 frames in mpegts container using libavcodec and muxing.c from www.ffmpeg.org. My H264 stream has no B-Frame, every nalu starts with 00 00 00 01,the stream begins with sps pps then the h264 data.

    #include "stdafx.h"
    #include 
    #include 
    #include "Stream2Mp4.h"
    
    #include opt.h>
    #include mathematics.h>
    #include timestamp.h>
    #include avformat.h>
    #include swresample.h>
    #include swresample.h>
    
    #define STREAM_FRAME_RATE 25
    #define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */
    
    static int ptsInc = 0;
    static int vi = -1;
    static int waitkey = 1;
    
    // < 0 = error
    // 0 = I-Frame
    // 1 = P-Frame
    // 2 = B-Frame
    // 3 = S-Frame
    int getVopType( const void *p, int len )
    {   
    if ( !p || 6 >= len )
        return -1;
    
    unsigned char *b = (unsigned char*)p;
    
    // Verify NAL marker
    if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] )
    {   b++;
    if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] )
        return -1;
    } // end if
    
    b += 3;
    
    // Verify VOP id
    if ( 0xb6 == *b )
    {   b++;
    return ( *b & 0xc0 ) >> 6;
    } // end if
    
    switch( *b )
    {   case 0x65 : return 0;
    case 0x61 : return 1;
    case 0x01 : return 2;
    } // end switch
    
    return -1;
    }
    
    int get_nal_type( void *p, int len )
    {
    if ( !p || 5 >= len )
        return -1;
    
    unsigned char *b = (unsigned char*)p;
    
    // Verify NAL marker
    if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] )
    {   b++;
    if ( b[ 0 ] || b[ 1 ] || 0x01 != b[ 2 ] )
        return -1;
    } // end if
    
    b += 3;
    
    return *b;
    }
    
    
    /* Add an output stream */
    AVStream *add_stream(AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id)
    {
    AVCodecContext *c;
    AVStream *st;
    
    /* find the encoder */
    *codec = avcodec_find_encoder(codec_id);
    if (!*codec)
    {
        printf("could not find encoder for '%s' \n", avcodec_get_name(codec_id));
        exit(1);
    }
    
    st = avformat_new_stream(oc, *codec);
    if (!st)
    {
        printf("could not allocate stream \n");
        exit(1);
    }
    st->id = oc->nb_streams-1;
    c = st->codec;
    vi = st->index;
    
    switch ((*codec)->type)
    {
    case AVMEDIA_TYPE_AUDIO:
        c->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
        c->bit_rate = 64000;
        c->sample_rate = 44100;
        c->channels = 2;
        break;
    
    case AVMEDIA_TYPE_VIDEO:
        c->codec_id = codec_id;
        c->bit_rate = 90000;
        c->width = 480;
        c->height = 354;
        c->time_base.den = 15;
        c->time_base.num = 1;
        c->gop_size = 12;
        c->pix_fmt = STREAM_PIX_FMT;
        if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO)
        {
            c->max_b_frames = 2;
        }
        if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO)
        {
            c->mb_decision = 2;
        }
        break;
    
    default:
        break;
    }
    
    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
    {
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }
    
    return st;
    }
    
    
    
    void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
    {
    int ret;
    AVCodecContext *c = st->codec;
    
    /* open the codec */
    ret = avcodec_open2(c, codec, NULL);
    if (ret < 0)
    {
        printf("could not open video codec");
        //exit(1);
    }
    
    }
    
    int CreateMp4(AVFormatContext *&m_pOc, void *p, int len)
    {
    int ret; 
    const char* pszFileName = "output002.mp4";
    AVOutputFormat *fmt;
    AVCodec *video_codec;
    AVStream *m_pVideoSt;
    
    if (0x67 != get_nal_type(p, len))
    {
        printf("can not detect nal type");
        return -1;
    }
    av_register_all();
    
    avformat_alloc_output_context2(&m_pOc, NULL, NULL, pszFileName);
    if (!m_pOc)
    {
        printf("Could not deduce output format from file extension: using MPEG. \n");
        avformat_alloc_output_context2(&m_pOc, NULL, "mpeg", pszFileName);
    }
    if (!m_pOc)
    {
        return 1;
    }
    
    fmt = m_pOc->oformat;
    
    if (fmt->video_codec != AV_CODEC_ID_NONE)
    {
        m_pVideoSt = add_stream(m_pOc, &video_codec, fmt->video_codec);
    }
    
    if (m_pVideoSt)
    {
        open_video(m_pOc, video_codec, m_pVideoSt); 
    }
    
    av_dump_format(m_pOc, 0, pszFileName, 1);
    
    /* open the output file, if needed */
    if (!(fmt->flags & AVFMT_NOFILE))
    {
        ret = avio_open(&m_pOc->pb, pszFileName, AVIO_FLAG_WRITE);
        if (ret < 0)
        {
            printf("could not open '%s': %s\n", pszFileName);
            return 1;
        }
    }
    
    /* Write the stream header, if any */
    ret = avformat_write_header(m_pOc, NULL);
    if (ret < 0)
    {
        printf("Error occurred when opening output file");
        return 1;
    }
    }
    
    
    /* write h264 data to mp4 file*/
    
    
    void WriteVideo(AVFormatContext *&m_pOc,void* data, int nLen)
    {
    int ret;
    
    if ( 0 > vi )
    {
        printf("vi less than 0");
        //return -1;
    }
    AVStream *pst = m_pOc->streams[ vi ];
    
    // Init packet
    AVPacket pkt;
    
    AVCodecContext *c = pst->codec;
    
    av_init_packet( &pkt );
    pkt.flags |= ( 0 >= getVopType( data, nLen ) ) ? AV_PKT_FLAG_KEY : 0;   
    
    pkt.stream_index = pst->index;
    pkt.data = (uint8_t*)data;
    pkt.size = nLen;
    
    // Wait for key frame
    if ( waitkey )
        if ( 0 == ( pkt.flags & AV_PKT_FLAG_KEY ) )
            return ;
        else
            waitkey = 0;
    
    
    pkt.pts = (ptsInc++) * (90000/STREAM_FRAME_RATE);
    //pkt.dts = (ptsInc++) * (90000/STREAM_FRAME_RATE);
    
    ret = av_interleaved_write_frame( m_pOc, &pkt );
    if (ret < 0)
    {
        printf("cannot write frame");
    }
    
    
    }
    
    void CloseMp4(AVFormatContext *&m_pOc)
    {
    waitkey = -1;
    vi = -1;
    
    if (m_pOc)
        av_write_trailer(m_pOc);
    
    if (m_pOc && !(m_pOc->oformat->flags & AVFMT_NOFILE))
        avio_close(m_pOc->pb);
    
    if (m_pOc)
    {
        avformat_free_context(m_pOc);
        m_pOc = NULL;
    }
    
    }
    

    could anybody help me? Thank you very much!

  • ffmpeg -acodec and -vcodec paramter inputs list

    5 mai 2014, par Sajith Dilshan Jamal

    Is there a list somewhere for ffmpeg which lists what the libraries we enter into the -acodec/ -vcodec parameters support?

    eg: ffmpeg -vcodec x264 -acodec libmp3lame

    I would like a list showing which formats x264/libmp3lame support.

    Also is there a list that shows all possible inputs to the -vcodec & -acodec parameters.

  • ffmpeg in a bash pipe

    5 mai 2014, par Martin Wang

    I have a rmvb file path list, and want to convert this files to mp4 files. So I hope to use bash pipeline to handle it. The code is

    Convert() {
        ffmpeg -i "$1" -vcodec mpeg4 -sameq -acodec aac -strict experimental "$1.mp4"
    }
    
    Convert_loop(){
        while read line; do
           Convert $line
        done
    }
    
    cat list.txt | Convert_loop
    

    However, it only handle the first file and the pipe exits.

    So, does ffmpeg affect the bash pipe?

  • FFmpeg : How to generate a .mp4 with h.265 Codec ?

    5 mai 2014, par skull3r7

    I want to encode a video file to H.265. The last version of ffmpeg should be support H.265 (Source).

    However, I could not find any information about the exact command.

    I do not know, which library I should indicate after -vcodec.

    EDIT: I use the lastest Zeranoe FFmpeg Build (git-c78a416 (2013-10-26)).