Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Flask send_file not sending file

    12 décembre, par jackmerrill

    I'm using Flask with send_file() to have people download a file off the server.

    My current code is as follows:

    @app.route('/', methods=["GET", "POST"])
    def index():
        if request.method == "POST":
            link = request.form.get('Link')
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                info_dict = ydl.extract_info(link, download=False)
                video_url = info_dict.get("url", None)
                video_id = info_dict.get("id", None)
                video_title = info_dict.get('title', None)
                ydl.download([link])
            print("sending file...")
            send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
            print("file sent, deleting...")
            os.remove("dl/"+video_title+".f137.mp4")
            print("done.")
            return render_template("index.html", message="Success!")
        else:
            return render_template("index.html", message=message)
    

    The only reason I have .f137.mp4 added is because I am using AWS C9 to be my online IDE and I can't install FFMPEG to combine the audio and video on Amazon Linux. However, that is not the issue. The issue is that it is not sending the download request.

    Here is the console output:

    127.0.0.1 - - [12/Dec/2018 16:17:41] "POST / HTTP/1.1" 200 -
    [youtube] 2AYgi2wsdkE: Downloading webpage
    [youtube] 2AYgi2wsdkE: Downloading video info webpage
    [youtube] 2AYgi2wsdkE: Downloading webpage
    [youtube] 2AYgi2wsdkE: Downloading video info webpage
    WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.
    [download] Destination: dl/Meme Awards v244.f137.mp4
    [download] 100% of 73.82MiB in 00:02
    [download] Destination: dl/Meme Awards v244.f140.m4a
    [download] 100% of 11.63MiB in 00:00
    sending file...
    file sent, deleting...
    done.
    127.0.0.1 - - [12/Dec/2018 16:18:03] "POST / HTTP/1.1" 200 -
    

    Any and all help is appreciated. Thanks!

  • How to write H264 raw stream into mp4 using ffmpeg directly

    12 décembre, 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!

  • Change bitrate of USB camera

    12 décembre, par Nick Saw

    I use ELP USB camera with h264-codec output.

    Ffmpeg-command to take the video stream:

    ffmpeg -i /dev/video1 -c:v copy output.ts
    

    As result I have a video with 4Mb bitrate - this value is too high for my task.

    There is the same situation when I use GSTreamer:

    gst-launch-1.0 v4l2src device=/dev/video1 ! video/x-h264,width=1280,height=720,framerate=30/1 ! mpegtsmux ! filesink location=output.ts
    

    I know that it's possible to change the bitrate if we decode h264 the stream firstly and then encode it to h264 again. This operation requires too much CPU-power of my NanoPI device.

    The main question is: Is it possible to change a USB-camera's bitrate without decoding?

    Thanks in advance!

  • Rotate video according to rotation metadata and strip off metadata using ffmpeg [on hold]

    12 décembre, par this.lau_

    I've got a video with the following metadata:

    Metadata:
      rotate          : 90
      creation_time   : 2018-12-03T16:13:47.000000Z
      handler_name    : VideoHandle
    Side data:
      displaymatrix: rotation of -90.00 degrees
    

    And I would like to remove this metadata and rotate the video by whatever rotation value is found there (in this case -90 but it can be anything).

    The result will be a video with the correct orientation and that works with all video players (including those that don't auto-rotate based on the metadata).

    I've tried various commands but couldn't find anything that would work. Any idea how to do this?

  • Stabilize ffmpeg rtsp to hls transmuxing

    12 décembre, par Gust

    I'm using ffmpeg to convert a RTSP stream (from a security camera) into a HLS stream which I then play on a website using hls.js.

    I start the transmuxing with: ffmpeg -i rtsp: -fflags flush_packets -max_delay 1 -an -flags -global_header -hls_time 1 -hls_list_size 3 -hls_wrap 3 -vcodec copy -y .m3u8

    I can get the stream to play, but the quality isn't good at all... Sometimes the stream jumps on time or freezes for a while. If I open it using VLC I get the same kind of problems.

    Any idea why? Or how can I stabilize it?