Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Process is being paused until I close the program

    27 mai 2017, par user924

    I made a simple program and UI for it. It has one button for starting ffmpeg.exe to decode a video:

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ProcessBuilder pb = new ProcessBuilder("D:\\ffmpeg.exe", "-i", "\"D:\\video\\input.mp4\"", "\"output.mp4\"");
                try {
                    Process p = pb.start();
                } catch (IOException error) {
                    //
                }
            }
        }
    

    The problem is that after clicking the button ffmpeg starts but it doesn't do anything (in task manager it doesn't use cpu - 0%) until I close the program (UI) and then ffmpeg process starts to decode a video (only after closing the program ffmpeg starts using cpu - e.g. 24%)

    It's not duplicate: older questions proposed by Andy Thomas doesn't have an answer (solution) for my problem

  • WriteToUDP only works on local machine

    26 mai 2017, par Anderson Scouto da Silva

    This script captures a flow entry by ffmpeg and writes to a UDP address.

    The error is that it does not go to the internet, it only stays on the local machine.

    It is not a problem in my internet network, because I made a stream through VLC and opened in another PC and it works normally, with the same parameters, but in this my script is only in the local PC.

    Follow the pictures to help with the question:

    http://imgh.us/1_4575.jpg

    http://imgh.us/2_3278.jpg

    http://imgh.us/3_2694.jpg

    package main
    
    import (
        "os/exec"
        "io"
        "net"
        "log"
        "fmt"
    )
    
    func main() {
        // UDP connections
        conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
        if err != nil {
            log.Fatal(err)
        }
    
        // Set the write buffer on UDP
        err = conn.SetWriteBuffer(40 * 1024)
        if err != nil {
            log.Fatal(err)
        }
    
        inputSource := "C:/Users/Administrador/GoglandProjects/CASEncoder/in/teste-4k.mp4"
    
        // Starts ffmpeg capturing input
        cmdName := "ffmpeg"
        argsPipe1 := []string{
            "-hide_banner",
            "-loglevel", "panic",
            "-re",
            "-i",
            inputSource,
            "-preset",
            "superfast",
            "-c:v",
            "h264",
            "-crf",
            "0",
            "-c",
            "copy",
            "-f", "mpegts", "pipe:1",
        }
        cmdPipe1 := exec.Command(cmdName, argsPipe1...)
    
        stdoutPipe1, err := cmdPipe1.StdoutPipe()
        if err != nil {
            log.Fatal(err)
        }
        err = cmdPipe1.Start()
        if err != nil {
            log.Fatal(err)
        }
    
        chunk := make([]byte, 40 * 1024)
        for {
            // reads the output pipe from ffmpeg
            nr, err5 := stdoutPipe1.Read(chunk)
            fmt.Printf("Readed %d bytes\n", nr)
    
            if nr > 0 {
                validData := chunk[:nr]
    
                // write to UDP
                nw, err := conn.WriteToUDP(validData, &net.UDPAddr{IP: net.IP{233, 10, 10, 13}, Port: 1234})
                fmt.Printf("Writed %d bytes\n", nw)
                if err != nil {
                    log.Fatal(err)
                }
            }
    
            if err5 != nil {
                // end of file
                if err5 == io.EOF {
                    break
                }
                continue
            }
        }
    }
    
  • FFmpeg conversion to Photo-JPEG is blocky [duplicate]

    26 mai 2017, par Richard210363

    This question already has an answer here:

    I am converting .mp4 files to .mov with the Photo-JPEG codec to use in After Effects to reduce render times.

    I have looked up the FFmpeg commands to do this and come up with:

    ffmpeg -i tower-bridge.mp4 -an -pix_fmt yuvj420p -vcodec mjpeg -f mov -y tower-bridge.mov 
    

    This works but when I look at the output video it is quite blocky; like a mosaic.

    During the conversion I get this message:

    [swscaler @ 0000000008c97fe0] deprecated pixel format used, make sure you did set range correctly
    

    Research on-line suggests I can ignore this message.

    What can I do to improve the output quality?


    MediaInfo report on Input file:

    Complete name : F:\london-tower-bridge.mp4
    Format : MPEG-4
    Format profile : Base Media
    Codec ID : isom (isom/avc1/mp42)
    File size : 18.7 MiB
    Duration : 13 s 625 ms
    Overall bit rate : 11.5 Mb/s

    ID : 1
    Format: AVC
    Format/Info : Advanced Video Codec
    Format profile : Baseline@L4
    Format settings, CABAC : No
    Format settings, ReFrames : 3 frames
    Codec ID : avc1
    Codec ID/Info : Advanced Video Coding
    Duration : 13 s 625 ms
    Bit rate : 11.5 Mb/s
    Maximum bit rate : 26.8 Mb/s
    Width : 1 920 pixels
    Height : 1 080 pixels
    Display aspect ratio : 16:9
    Frame rate mode : Constant
    Frame rate : 24.000 FPS
    Color space : YUV
    Chroma subsampling : 4:2:0
    Bit depth : 8 bits
    Scan type : Progressive
    Bits/(Pixel*Frame) : 0.231
    Stream size : 18.7 MiB (100%)
    Writing library : Zencoder Video Encoding System

    MediaInfo report on Output file:

    Complete name : F:\london-tower-bridge.mov
    Format : MPEG-4
    Format profile : QuickTime
    Codec ID : qt 0000.02 (qt )
    File size : 16.1 MiB
    Duration : 13 s 625 ms
    Overall bit rate mode : Variable
    Overall bit rate : 9 912 kb/s
    Writing application : Lavf57.72.101

    ID : 1 Format : JPEG
    Codec ID : jpeg
    Duration : 13 s 625 ms
    Bit rate mode : Variable
    Bit rate : 9 910 kb/s
    Width : 1 920 pixels
    Height : 1 080 pixels
    Display aspect ratio : 16:9
    Frame rate mode : Constant
    Frame rate : 24.000 FPS
    Color space : YUV
    Chroma subsampling : 4:2:0
    Bit depth : 8 bits
    Scan type : Progressive
    Compression mode : Lossy
    Bits/(Pixel*Frame) : 0.199
    Stream size : 16.1 MiB (100%)
    Language : English

  • ffmpeg av_read_frame "Invalid data found"

    26 mai 2017, par DweebsUnited

    I am using ffmpeg to cut part of a file, following the remux example provided. However, I am having issues reading from files. I am given a file descriptor as input and an output filename, and can successfully set up the output file, read the stream info, copy all the streams, etc, etc. On the first call to av_read_frame though, I get an "Invalid data found when processing input" error. I have tried this with many different video files, and have not been able to get a single one to process correctly.

    Here is my code. I get no errors anywhere, except av_read_frame, which gives Invalid data found no matter what video file I give it.

    char path[512];
    sprintf(path, "pipe:%d", fileno(fp));
    
    
    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = avformat_alloc_context(), *ofmt_ctx = NULL;
    AVPacket pkt;
    int ret, i;
    
    av_register_all();
    avcodec_register_all();
    
    if ((ret = avformat_open_input(&ifmt_ctx, path, av_find_input_format("mp4"), NULL)) < 0) {
        LOG("Could not open input file '%s'", path);
        goto end;
    }
    
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        LOG("Failed to retrieve input stream information", "");
        goto end;
    }
    
    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        LOG("Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }
    
    ofmt = ofmt_ctx->oformat;
    
    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
    
        if (!out_stream) {
            LOG("Failed allocating output stream\n");
            goto end;
        }
    
        ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
        if (ret < 0) {
            LOG("Failed to copy context from input to output stream codec context\n");
            goto end;
        }
        out_stream->codecpar->codec_tag = 0;
    }
    
    if (!(ofmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            LOG("Could not open output file '%s'", out_filename);
            goto end;
        }
    }
    
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        LOG("Error occurred when opening output file\n");
        goto end;
    }
    
    //    int indexs[8] = {0};
    
    //    int64_t start_from = 8*AV_TIME_BASE;
    ret = av_seek_frame(ifmt_ctx, -1, from_seconds*AV_TIME_BASE, AVSEEK_FLAG_ANY);
    if (ret < 0) {
        LOG("Error seek\n");
        goto end;
    }
    
    int64_t *dts_start_from;
    int64_t *pts_start_from;
    dts_start_from = (int64_t *) malloc(sizeof(int64_t) * ifmt_ctx->nb_streams);
    memset(dts_start_from, 0, sizeof(int64_t) * ifmt_ctx->nb_streams);
    pts_start_from = (int64_t *) malloc(sizeof(int64_t) * ifmt_ctx->nb_streams);
    memset(pts_start_from, 0, sizeof(int64_t) * ifmt_ctx->nb_streams);
    
    while (1) {
        AVStream *in_stream, *out_stream;
    
        ret = av_read_frame(ifmt_ctx, &pkt);
        if (ret < 0)
            break;
    
        in_stream = ifmt_ctx->streams[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];
    
        if (av_q2d(in_stream->time_base) * pkt.pts > end_seconds) {
            av_packet_unref(&pkt);
            break;
        }
    
        if (dts_start_from[pkt.stream_index] == 0)
            dts_start_from[pkt.stream_index] = pkt.dts;
        if (pts_start_from[pkt.stream_index] == 0)
            pts_start_from[pkt.stream_index] = pkt.pts;
    
        /* copy packet */
        pkt.pts = ::av_rescale_q_rnd(pkt.pts - pts_start_from[pkt.stream_index], in_stream->time_base, out_stream->time_base, (AVRounding) (AV_ROUND_NEAR_INF |
                                                                                                                                            AV_ROUND_PASS_MINMAX));
        pkt.dts = ::av_rescale_q_rnd(pkt.dts - dts_start_from[pkt.stream_index], in_stream->time_base, out_stream->time_base, (AVRounding) (AV_ROUND_NEAR_INF |
                                                                                                                                            AV_ROUND_PASS_MINMAX));
        if (pkt.pts < 0) {
            pkt.pts = 0;
        }
        if (pkt.dts < 0) {
            pkt.dts = 0;
        }
        pkt.duration = (int) av_rescale_q((int64_t) pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
    
        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
        if (ret < 0) {
            LOG("Error muxing packet\n");
            break;
        }
        av_packet_unref(&pkt);
    }
    free(dts_start_from);
    free(pts_start_from);
    
    av_write_trailer(ofmt_ctx);
    
    end:
    LOG("END");
    
    avformat_close_input(&ifmt_ctx);
    
    /* close output */
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
        avio_closep(&ofmt_ctx->pb);
    avformat_free_context(ofmt_ctx);
    
    if (ret < 0 && ret != AVERROR_EOF) {
        LOG("-- Error occurred: %s\n", av_err2str(ret));
        return 1;
    }
    

    Thank you for any help, I'm still getting used to ffmpeg. Did I miss some setting on the input? Am I not setting something up that I need to be? This problem is really stumping me.

  • ffmpeg, opencv broken pipe

    26 mai 2017, par dopplesoldner

    I am trying to output opencv (version 2.4) frames to ffmpeg via pipes and am getting IOError: [Errno 32] Broken pipe error.

    import sys
    import cv2
    import numpy as np
    import subprocess
    
    cap = cv2.VideoCapture(0)
    fc = 0
    
    command = [ 'ffmpeg',
        '-y',
        '-f', 'rawvideo',
        '-vcodec','rawvideo',
        '-s', '420x360',
        '-pix_fmt', 'rgb24',
        '-r', '24', 
        '-i', '-', # The imput comes from a pipe
        '-an', # no audio
        '-vcodec', 'mpeg',
        'output.mp4' ]
    
    pipe = subprocess.Popen( command, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    
    while(cap.isOpened() and fc < 10):
        _, frame = cap.read()
        frame = cv2.resize(frame, (420, 360))
        _, buf = cv2.imencode('.jpg', frame)
        jpg = buf.tostring()
        pipe.stdin.write(jpg)
        fc += 1
    cap.release()
    

    Has anyone come across this before?