Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • I don't know the time unit to use for av_dict_set to set a timeout

    16 juin, par ICYMYM

    I am confused. I am using the av_dict_set function to set a time-out, but when I searched for information about av_dict_set, the time unit seems to be different. I don't know how to set it now. Can anyone help?

    I found some code like the following:

    pFormatCtx = avformat_alloc_context();
    
    av_dict_set(&opts, "rtsp_transport", "tcp", 0);
    //av_dict_set(&opts, "timeout", "5000000", 0);
    if(strncmp(stream_url, "rtmp:", sizeof("rtmp:")) == 0){
        av_dict_set(&opts, "timeout", "6", 0); // in secs
    }
    else if(strncmp(stream_url, "http:", sizeof("http:")) == 0){
        av_dict_set(&opts, "timeout", "6000", 0); // in ms
    }
    
    if(avformat_open_input(&pFormatCtx, stream_url, NULL, &opts)!=0)
    {
      return 1;
    }
    

    Maybe it should set the time unit according to the different protocols (http or rtsp).

    Is the code above right ?

  • Piping pi's opencv video to ffmpeg for Youtube streaming

    15 juin, par Mango Plaster

    This is a small python3 script reading off picam using OpenCV :

    #picamStream.py
    
    import sys, os
    from picamera.array import PiRGBArray
    from picamera import PiCamera
    import time
    import cv2
     
    # initialize the camera and grab a reference to the raw camera capture
    camera = PiCamera()
    camera.resolution = (960, 540)
    camera.framerate = 30
    rawCapture = PiRGBArray(camera, size=(960, 540))
     
    # allow the camera to warmup
    time.sleep(0.1)
     
    # capture frames from the camera
    for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    
        image = frame.array
     
        # ---------------------------------
        # .
        # Opencv image processing goes here
        # .
        # ---------------------------------
    
        os.write(1, image.tostring())
     
        # clear the stream in preparation for the next frame
        rawCapture.truncate(0)
    
    # end
    

    And I am trying to pipe it to ffmpeg to Youtube stream

    My understanding is that I need to reference below two commands to somehow come up with a new ffmpeg command.

    Piping picam live video to ffmpeg for Youtube streaming.

    raspivid -o - -t 0 -vf -hf -w 960 -h 540 -fps 25 -b 1000000 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/[STREAMKEY]

    Piping OPENCV raw video to ffmpeg for mp4 file.

    python3 picamStream.py | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 960x540 -framerate 30 -i - foo.mp4

    So far I've had no luck. Can anyone help me with this?

  • Recording of rtsp stream with automatic video segments [closed]

    14 juin, par ToD

    It is necessary to capture video in ffmpeg so that it works continuously without restarting. I don’t need to re-encode it, just capture it, and I need it to automatically divide the video into 2-minute segments and name the files according to the current time. I managed to do it, but without segmentation, by running a loop, but the gap between pieces of video is about 9 seconds. Stream rtsp from IP-camera. Help please.

  • AWS Lambda failed to resolve hostname in child_process spawn

    14 juin, par warly

    i'm trying to get ffmpeg working in AWS Lambda reading from and writing to S3. I crawled through dozens of aws documentations, stackoverflow questions and blog posts. All do it slightly different, but in the end none of them worked for me. So I hope to find help here :) I sticked to the tutorial from InPlainEnglish: https://plainenglish.io/community/automated-video-processing-with-aws-lambda-and-ffmpeg-2834b7

    The ffprobe command does work, so ffmpeg is callable. But when calling ffmpeg within a child_process.spawn command i get the error Failed to resolve hostname my-bucket.s3.eu-central-1.amazonaws.com: System error

    export const handler = async (event) => {
        const s3 = new S3Client({});
        const bucket = event.Records[0].s3.bucket.name;
        const sourceKey = event.Records[0].s3.object.key;
        const sourceURL = await getSignedUrl(s3, new GetObjectCommand({
            Bucket: bucket,
            Key: sourceKey,
        }), {
            expiresIn: 900,
        });
    
        const tmpFilePath = 'tmp/video.mp4';
        const { convertOut } = await commander(`/opt/ffmpeg -i "${sourceURL}" -vf scale=1080:-2,format=yuv420p ${tmpFilePath}`);
    
        return {
            statusCode: 200,
            body: {
                result: JSON.stringify(convertOut),
            },
        };
    };
    

    the signed url is fine. I've tested it with vanilla js await fetch(sourceURL) and it returned the file. The problem is that for some reason the child_process has no internet access.

    if anyone is able to point me in the right direction i would appreciate it :D

    Full error message:

    {
      "errorType": "Error",
      "errorMessage": "Command failed: /opt/ffmpeg -i \"https://my-bucket.s3.eu-central-1.amazonaws.com/original/Testvideo.MOV?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential={redacted}&X-Amz-Date=20240209T212255Z&X-Amz-Expires=900&X-Amz-Security-Token={redacted}&X-Amz-Signature={redacted}&X-Amz-SignedHeaders=host&x-id=GetObject\" -vf scale=1080:-2,crop=1080:1350,format=yuv420p tmp/video.mp4\nffmpeg version 6.1-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2023 the FFmpeg developers\n  built with gcc 8 (Debian 8.3.0-6)\n  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzimg\n  libavutil      58. 29.100 / 58. 29.100\n  libavcodec     60. 31.102 / 60. 31.102\n  libavformat    60. 16.100 / 60. 16.100\n  libavdevice    60.  3.100 / 60.  3.100\n  libavfilter     9. 12.100 /  9. 12.100\n  libswscale      7.  5.100 /  7.  5.100\n  libswresample   4. 12.100 /  4. 12.100\n  libpostproc    57.  3.100 / 57.  3.100\n[tcp @ 0x141950d0] Failed to resolve hostname my-bucket.s3.eu-central-1.amazonaws.com: System error\n[in#0 @ 0x14190900] Error opening input: Input/output error\nError opening input file https://my-bucket.s3.eu-central-1.amazonaws.com/original/Testvideo.MOV?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential={redacted}&X-Amz-Date=20240209T212255Z&X-Amz-Expires=900&X-Amz-Security-Token={redacted}&X-Amz-Signature={redacted}&X-Amz-SignedHeaders=host&x-id=GetObject.\nError opening input files: Input/output error\n",
      "trace": [
        "Error: Command failed: /opt/ffmpeg -i \"https://my-bucket.s3.eu-central-1.amazonaws.com/original/Testvideo.MOV?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential={redacted}&X-Amz-Date=20240209T212255Z&X-Amz-Expires=900&X-Amz-Security-Token={redacted}&X-Amz-Signature={redacted}&X-Amz-SignedHeaders=host&x-id=GetObject\" -vf scale=1080:-2,crop=1080:1350,format=yuv420p tmp/video.mp4",
        "ffmpeg version 6.1-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2023 the FFmpeg developers",
        "  built with gcc 8 (Debian 8.3.0-6)",
        "  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzimg",
        "  libavutil      58. 29.100 / 58. 29.100",
        "  libavcodec     60. 31.102 / 60. 31.102",
        "  libavformat    60. 16.100 / 60. 16.100",
        "  libavdevice    60.  3.100 / 60.  3.100",
        "  libavfilter     9. 12.100 /  9. 12.100",
        "  libswscale      7.  5.100 /  7.  5.100",
        "  libswresample   4. 12.100 /  4. 12.100",
        "  libpostproc    57.  3.100 / 57.  3.100",
        "[tcp @ 0x141950d0] Failed to resolve hostname my-bucket.s3.eu-central-1.amazonaws.com: System error",
        "[in#0 @ 0x14190900] Error opening input: Input/output error",
        "Error opening input file https://my-bucket.s3.eu-central-1.amazonaws.com/original/Testvideo.MOV?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential={redacted}&X-Amz-Date=20240209T212255Z&X-Amz-Expires=900&X-Amz-Security-Token={redacted}&X-Amz-Signature={redacted}&X-Amz-SignedHeaders=host&x-id=GetObject.",
        "Error opening input files: Input/output error",
        "",
        "    at ChildProcess.exithandler (node:child_process:422:12)",
        "    at ChildProcess.emit (node:events:518:28)",
        "    at maybeClose (node:internal/child_process:1105:16)",
        "    at ChildProcess._handle.onexit (node:internal/child_process:305:5)"
      ]
    }
    enter code here
    

    Update: Turns out it is not S3 or internet, but the child_process which does not seem to have internet access. I've update my description above.

  • Extract clips from different videos, and merge them into one video, using ffmpeg [closed]

    14 juin, par mikef

    I want to extract multiple clips from different videos (in different encoding schemes/formats), and then merge them into one video.

    The inputs are a list of files and precise timestamps of the clips:

    [
        ("1.mp4", ["00:05:02.230", "00:05:05.480"]),
        ("4.mp4", ["00:03:25.456", "00:03:28.510"]),
        ("2.mp4", ["00:12:23.891", "00:12:32.642"]),
        ("2.mp4", ["00:12:44.236", "00:12:46.920"]),
        ("3.mp4", ["00:02:06.520", "00:02:11.324"]),
        ("1.mp4", ["00:06:23.783", "00:06:25.458"]),
        ("2.mp4", ["00:03:53.976", "00:03:56.853"]),
        ...
    ]
    

    Option 1: Use ffmpeg -filter_complex and concat.

    ffmpeg -y -i ./f19dbe55-b4cd-4cb5-a4f1-701b6864fea5.mp4 -filter_complex "[0:v]trim=start=1009.24:end=1022.53,setpts=PTS-STARTPTS[v0];[0:a]atrim=start=1009.24:end=1022.53,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a0];[0:v]trim=start=904.49:end=921.3,setpts=PTS-STARTPTS[v1];[0:a]atrim=start=904.49:end=921.3,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a1];[0:v]trim=start=1718.95:end=1737.19,setpts=PTS-STARTPTS[v2];[0:a]atrim=start=1718.95:end=1737.19,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a2];[0:v]trim=start=1692.64:end=1712.53,setpts=PTS-STARTPTS[v3];[0:a]atrim=start=1692.64:end=1712.53,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a3];[0:v]trim=start=322.34:end=330.13,setpts=PTS-STARTPTS[v4];[0:a]atrim=start=322.34:end=330.13,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a4];[0:v]trim=start=333.97:end=342.38,setpts=PTS-STARTPTS[v5];[0:a]atrim=start=333.97:end=342.38,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a5];[0:v]trim=start=352.33:end=359.74,setpts=PTS-STARTPTS[v6];[0:a]atrim=start=352.33:end=359.74,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a6];[0:v]trim=start=364.11:end=375.99,setpts=PTS-STARTPTS[v7];[0:a]atrim=start=364.11:end=375.99,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a7];[0:v]trim=start=395.56:end=407.88,setpts=PTS-STARTPTS[v8];[0:a]atrim=start=395.56:end=407.88,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a8];[0:v]trim=start=417.95:end=437.45,setpts=PTS-STARTPTS[v9];[0:a]atrim=start=417.95:end=437.45,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a9];[0:v]trim=start=443.81:end=452.68,setpts=PTS-STARTPTS[v10];[0:a]atrim=start=443.81:end=452.68,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a10];[0:v]trim=start=473.17:end=489.4,setpts=PTS-STARTPTS[v11];[0:a]atrim=start=473.17:end=489.4,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a11];[0:v]trim=start=506.35:end=510.53,setpts=PTS-STARTPTS[v12];[0:a]atrim=start=506.35:end=510.53,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a12];[0:v]trim=start=521.5360000000001:end=527.6610000000001,setpts=PTS-STARTPTS[v13];[0:a]atrim=start=521.5360000000001:end=527.6610000000001,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a13];[0:v]trim=start=542.7:end=558.58,setpts=PTS-STARTPTS[v14];[0:a]atrim=start=542.7:end=558.58,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a14];[0:v]trim=start=566.19:end=568.81,setpts=PTS-STARTPTS[v15];[0:a]atrim=start=566.19:end=568.81,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a15];[0:v]trim=start=588.01:end=602.13,setpts=PTS-STARTPTS[v16];[0:a]atrim=start=588.01:end=602.13,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a16];[0:v]trim=start=614.56:end=618.98,setpts=PTS-STARTPTS[v17];[0:a]atrim=start=614.56:end=618.98,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a17];[0:v]trim=start=725.62:end=737.95,setpts=PTS-STARTPTS[v18];[0:a]atrim=start=725.62:end=737.95,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a18];[0:v]trim=start=753.11:end=774.83,setpts=PTS-STARTPTS[v19];[0:a]atrim=start=753.11:end=774.83,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a19];[0:v]trim=start=778.85:end=782.14,setpts=PTS-STARTPTS[v20];[0:a]atrim=start=778.85:end=782.14,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a20];[0:v]trim=start=788.61:end=806.68,setpts=PTS-STARTPTS[v21];[0:a]atrim=start=788.61:end=806.68,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a21];[0:v]trim=start=835.81:end=856.58,setpts=PTS-STARTPTS[v22];[0:a]atrim=start=835.81:end=856.58,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a22];[0:v]trim=start=872.96:end=893.08,setpts=PTS-STARTPTS[v23];[0:a]atrim=start=872.96:end=893.08,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a23];[0:v]trim=start=937.9:end=951.32,setpts=PTS-STARTPTS[v24];[0:a]atrim=start=937.9:end=951.32,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a24];[0:v]trim=start=1045.13:end=1052.19,setpts=PTS-STARTPTS[v25];[0:a]atrim=start=1045.13:end=1052.19,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a25];[0:v]trim=start=1065.84:end=1101.57,setpts=PTS-STARTPTS[v26];[0:a]atrim=start=1065.84:end=1101.57,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a26];[0:v]trim=start=1137.44:end=1176.84,setpts=PTS-STARTPTS[v27];[0:a]atrim=start=1137.44:end=1176.84,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a27];[0:v]trim=start=1204.0:end=1212.93,setpts=PTS-STARTPTS[v28];[0:a]atrim=start=1204.0:end=1212.93,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a28];[0:v]trim=start=1272.17:end=1283.93,setpts=PTS-STARTPTS[v29];[0:a]atrim=start=1272.17:end=1283.93,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a29];[0:v]trim=start=1302.97:end=1331.42,setpts=PTS-STARTPTS[v30];[0:a]atrim=start=1302.97:end=1331.42,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a30];[0:v]trim=start=1389.77:end=1438.87,setpts=PTS-STARTPTS[v31];[0:a]atrim=start=1389.77:end=1438.87,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a31];[0:v]trim=start=1477.55:end=1491.53,setpts=PTS-STARTPTS[v32];[0:a]atrim=start=1477.55:end=1491.53,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a32];[0:v]trim=start=1503.42:end=1524.9,setpts=PTS-STARTPTS[v33];[0:a]atrim=start=1503.42:end=1524.9,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a33];[0:v]trim=start=1552.35:end=1608.12,setpts=PTS-STARTPTS[v34];[0:a]atrim=start=1552.35:end=1608.12,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a34];[0:v]trim=start=1616.93:end=1636.14,setpts=PTS-STARTPTS[v35];[0:a]atrim=start=1616.93:end=1636.14,asetpts=PTS-STARTPTS,afade=t=in:st=0:d=0.05[a35];[v0][a0][v1][a1][v2][a2][v3][a3][v4][a4][v5][a5][v6][a6][v7][a7][v8][a8][v9][a9][v10][a10][v11][a11][v12][a12][v13][a13][v14][a14][v15][a15][v16][a16][v17][a17][v18][a18][v19][a19][v20][a20][v21][a21][v22][a22][v23][a23][v24][a24][v25][a25][v26][a26][v27][a27][v28][a28][v29][a29][v30][a30][v31][a31][v32][a32][v33][a33][v34][a34][v35][a35]concat=n=36:v=1:a=1[outv][outa]" -map [outv] -map [outa] -c:v libx264 -c:a aac out.mp4
    

    Note: afade=t=in:st=0:d=0.05 is used to mitigate the cramp video in the transition between clips.

    Drawback: very slow, memory intensive (cause OOM)

    Option 2: use ffmpeg -ss to extract, and then use -concat to merge.

    ffmpeg -y -ss 00:00:10.550 -i .\remastered_video.mp4 -to 00:00:10.710 -c:v h264_qsv -global_quality 20 -c:a aac -af afade=t=in:st=0:d=0.05 ./o1.mp4
    
    ffmpeg -y -f concat -safe 0 -i videos.txt -c copy out.mp4
    

    Drawback: the audio and video are not progressing synchronously. They start synchronously but then diverge over time. It seems the tiny time difference inside each clip gets accumulated over time.

    Trials we've made (but didn't help):

    • "-vf setpts=PTS-STARTPTS", "-af afade=t=in:st=0:d=0.05,asetpts=PTS-STARTPTS", "-shortest", "-avoid_negative_ts make_zero", "-start_at_zero", ts format+"-bsf:v", "h264_mp4toannexb"
    • Some suggests to put -ss after -i. But we don't want it because it will take a long time to position the frame (from the beginning of the video).

    Option 3: Use Python (pyav) and seek.

    • The intuition is simple: extract clips by timestamps, and then merge together.
    • However, the complexity is beyond our capability. We will have to handle different frames (PTS/DTS), frame resolutions, audio sampling rates, from different video files.
    • We've tried to convert all clips into the same resolution, audio sampling rate (48k), and format (mp4/h264). But the output video still has time mismatch (due to mis-positioned PTS).
    • We're stuck at this point, and not sure if it's on the right track either.

    What is the best (simplest)* way to solve this problem, using ffmpeg and/or python?

    *best/simplest in terms of less codes, faster execution time, and fewer hardware requirements.

    Any advice will be greatly appreciated!