Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • FFmpeg overwrite an AVPacket

    26 janvier 2019, par Alejandro Ramírez

    Hello guys I am working with video. I wan to encrypt the I-frames of one video compressed in H.264. So I get the AVPacket from the video and compare if (AVPacket.flags & AV_PKT_FLAG_KEY) to know if the packet has an I frame, but when I try to print the AVPacket.data I don't have any information to encrypt. Where can I get the information regarding to I-frame. ahead a put my code, thank you.

    #include 
    extern "C" {
    #include avcodec.h>
    #include avformat.h>
    #include swscale.h>
    #include avutil.h>
    }
    #define INBUF_SIZE 4096
    //#define AV_INPUT_BUFFER_PADDING_SIZE 32
    int main (int argc, char * argv[])
    {
        //av_register_all();//omit
        //revisar el número de argumentos en el video
        AVFormatContext *pFormatCtx = NULL;
        AVCodec *dec = NULL;
        AVCodecContext *pCodecCtx = NULL;
        AVStream *st = NULL;
        AVDictionary *opts = NULL;
        AVFrame *frame;
        AVPacket avpkt;
        uint8_t inbuf [INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
        FILE *f;
        int frame_count;
        int video_stream_index = -1;
    
        //av_init_packet (&avpkt);
    
        memset (inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
        if (avformat_open_input (&pFormatCtx, argv[1], NULL, NULL) != 0)
            return -1;
        if (avformat_find_stream_info (pFormatCtx, NULL) < 0)
            return -1;
        //video_stream_index = av_find_best_stream (pFormatCtx,AVMEDIA_TYPE_VIDEO, -1, -1, &dec,0);
        video_stream_index = av_find_best_stream (pFormatCtx,AVMEDIA_TYPE_VIDEO, -1, -1, &dec,0);
        if ( video_stream_index < 0)
            return -1;
        std::cout << "video_stream; " << video_stream_index << "\n";
        st = pFormatCtx -> streams [video_stream_index];
        std::cout << "number of frames " << st -> nb_frames << "\n";
        std::cout << "event_flags " << st -> event_flags << "\n";
        //pCodecCtx = st -> codec;//deprecated
    
        dec = avcodec_find_decoder (st -> codecpar -> codec_id);
        std::cout << "codec_id: " << st -> codecpar -> codec_id << "\n";
        std::cout << "AV_CODEC_ID_H264: " << AV_CODEC_ID_H264 << "\n";
        if (!dec)
            return -1;
        pCodecCtx = avcodec_alloc_context3 (dec);
        if (!pCodecCtx)
            return -1;
        //av_dict_set (&opts, "refcounted_frames", "0", 0);
        avcodec_parameters_to_context (pCodecCtx, st -> codecpar);
    
    
        std::cout << "todo bien \n";    
        if (avcodec_open2 (pCodecCtx, dec, &opts) < 0)
            return -1;
        /*************hasta aqui buen codigo*********************************************/ 
        frame = av_frame_alloc ();
        if (!frame)
            return -1;
        av_init_packet (&avpkt);
        avpkt.data = NULL;
        avpkt.size = 0;
        f = fopen (argv[1], "r");
        int times = 1;
        while (av_read_frame (pFormatCtx, &avpkt) >= 0){
            AVPacket oripkt = avpkt;
            if (oripkt.stream_index == video_stream_index){
                if (oripkt.flags & AV_PKT_FLAG_KEY){
                    std::cout << "times: " << times ++ << "\n";
                    std::cout << "avpkt.flags: " << oripkt.flags << "\n";
                    std::cout << "tam avpkt.data: " << sizeof(oripkt.data) << "\n";
                    std::cout << "tam avpkt.data: " << oripkt.data << "\n";
                    std::cout << "oripkt.size: " << oripkt.size << "\n";
                    std::cout << "oripkt.side_date_eme: " << oripkt.side_data_elems << "\n";
                    if (!oripkt.data)
                        std::cout << "no tengo dinero \n";
                }
            }
        }
    
        std::cout << "Fin del programa " << "\n";
    }
    
  • Remove audio streams from a .m2ts video file

    26 janvier 2019, par Nedrozak

    I have a video which has 3 audio streams in the file. The first one is English and the other ones are in different languages. How can I get rid of these audio streams without losing the quality of the video and the English stream. I think ffmpeg should be used but I don't know how to do it.

    Video

    Bit rate mode: Variable
    Overall bit rate: 38.6 Mb/s
    Chroma subsampling: 4:2:0

    Audio

    Format: DTS-HD
    Compression mode: Lossless

  • Catch refreshing Terminal output in Python

    26 janvier 2019, par Aaroknight

    I have written a python script which converts movies with ffmpeg from anything to h265 (hevc). Works fine so far now and I get catch a terminal output (How can I get terminal output in python? - Stackoverflow). I already tried this solution as well: Catching Terminal Output in Python - Stackoverflow But none of them is really what I need.

    Current code is following:

    def convert(path):
    if os.path.getsize(path) < 500000000:
        pass
    name = path.split("/")[-1]
    os.mkdir(path.replace(name, "hevc/"))
    outvid = path.replace(name, "hevc/" + name)
    cmd = ["ffmpeg", "-hwaccel", "cuvid", "-i", path, "-c:v", "hevc_nvenc", "-preset",
           "slow", "-rc", "vbr_hq", "-max_muxing_queue_size", "1000", "-map", "0", "-map_metadata",
           "0", "-map_chapters", "0", "-c:a", "copy", "-c:s", "copy", outvid]
    
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
    print(process)
    

    While ffmpeg is converting something, the bottom terminal line usually actualizes itself every second showing fps, time, etc. See screenshot bottom line.

    Normal ffmpeg output

    In Python I just get a static output:

    Python IDE output

    So do you guys have any idea how to catch that refreshing output?

  • HEVC File bigger after converting from h264

    26 janvier 2019, par Aaroknight

    I'm currently working an an automated Python script for indexing and converting all my movies and episodes with ffmpeg. I use subprocess.call() for running the ffmpeg command and tested this command with some movies. As expected the big h264 files were converted to merely one third of what they used to have.

    But now that I was testing the method I found that a converted episode (about 400MB in h264) had over 1,6GB in hevc. I have absolutely no idea why the new file would be that much bigger in hevc. This is my code:

    def convert(path):
        outvid = path.strip(".mkv") + "h265.mkv"
    
        cmd = ["ffmpeg", "-i", path, "-map", "0", "-map_metadata", "0", "-map_chapters", "0", "-c:v", "libx265",
               "-c:a", "copy", "-c:s", "copy", "-preset", "ultrafast", "-x265-params", "lossless=1", outvid]
        subprocess.call(cmd)
    
    convert("/Volumes/2TB/Black Butler/Season 1/Black Butler S01E01.mkv")
    

    I don't have that much experience with ffmpeg, nor with subprocess. This is one of my first bigger projects. I hope someone can tell me what the problem might be.

    UPDATE Problem applies only for small video files. I now just check for the file size and skip the small files. Wouldn't have made much of a difference anyway.

    Size Comparison

  • How to replace all specified images in a video with ffmpeg?

    26 janvier 2019, par andy

    Can ffmpeg be implemented? If so, how should it be achieved? Is it a frame to frame replacement?plz