Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • ffmpeg - I want to concat multiple videos (front / rear) with overlay and one second delay

    12 mai, par Andrej Florjančič

    I try to write bash script to concat front and rear video files with delay 1 second at start and overlay rear over front.

    I wrote the following bash script:

    # Define the output file
    output_file="${VIDEO}/final_output_${choice}.mp4"
    
    # Collect and sort the video files
    front_files=( $(ls *_F.MP4 | sort) )
    rear_files=( $(ls *_R.MP4 | sort) )
    
    # Create a file to hold ffmpeg input commands and filter scripts
    input_list="input_files.txt"
    filter_script="filter_complex.txt"
    inputs=""
    
    # Clear the input list and filter script files
    echo "" > "$input_list"
    echo "" > "$filter_script"
    
    # Number of files
    num_files=${#front_files[@]}
    
    # Iterate through the sorted files to build the filter script
    for i in "${!front_files[@]}"; do
        echo "file '${front_files[$i]}'" >> "$input_list"
        echo "file '${rear_files[$i]}'" >> "$input_list"
        
        echo "[$((2*i)):v]trim=start=1,scale=1920:1080,setpts=PTS-STARTPTS[f_$i]; [$((2*i)):a]atrim=start=1,asetpts=PTS-STARTPTS[a_$i];" >> "$filter_script"
        echo "[$((2*i+1)):v]trim=start=1,scale=640:360,setpts=PTS-STARTPTS[r_$i];" >> "$filter_script"  # Rear video only
        echo "[f_$i][r_$i]overlay=x=(main_w-overlay_w-10):y=(main_h-overlay_h-10)[mix_$i];" >> "$filter_script"
    
        input+="[mix_$i][a_$i]"
    done
    
    # Concatenate all combined labels
    echo "${input}concat=n=${#front_files[@]}:v=1:a=1[v][a]" >> "$filter_script"
    
    # Run FFmpeg with filter script
    ffmpeg -f concat -safe 0 -i "$input_list" \
        -filter_complex_script "$filter_script" \
        -map "[v]" -map "[a]" \
        -y "$output_file"
    

    This script generates two files input_list.txt:

    file '20200826_084407_F.MP4'
    file '20200826_084407_R.MP4'
    file '20200826_084708_F.MP4'
    file '20200826_084708_R.MP4'
    file '20200826_084910_F.MP4'
    file '20200826_084910_R.MP4'
    

    filter_script.txt:

    [0:v]trim=start=1,scale=1920:1080,setpts=PTS-STARTPTS[f_0]; [0:a]atrim=start=1,asetpts=PTS-STARTPTS[a_0];
    [1:v]trim=start=1,scale=640:360,setpts=PTS-STARTPTS[r_0];
    [f_0][r_0]overlay=x=(main_w-overlay_w-10):y=(main_h-overlay_h-10)[mix_0];
    [2:v]trim=start=1,scale=1920:1080,setpts=PTS-STARTPTS[f_1]; [2:a]atrim=start=1,asetpts=PTS-STARTPTS[a_1];
    [3:v]trim=start=1,scale=640:360,setpts=PTS-STARTPTS[r_1];
    [f_1][r_1]overlay=x=(main_w-overlay_w-10):y=(main_h-overlay_h-10)[mix_1];
    [4:v]trim=start=1,scale=1920:1080,setpts=PTS-STARTPTS[f_2]; [4:a]atrim=start=1,asetpts=PTS-STARTPTS[a_2];
    [5:v]trim=start=1,scale=640:360,setpts=PTS-STARTPTS[r_2];
    [f_2][r_2]overlay=x=(main_w-overlay_w-10):y=(main_h-overlay_h-10)[mix_2];
    [mix_0][a_0][mix_1][a_1][mix_2][a_2]concat=n=3:v=1:a=1[v][a]
    

    I'w got error:

    Input #0, concat, from 'input_files.txt':
      Duration: N/A, start: 0.000000, bitrate: 11973 kb/s
      Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 11877 kb/s, 30 fps, 30 tbr, 60k tbn, 60 tbc
        Metadata:
          creation_time   : 2020-08-26T08:47:07.000000Z
          handler_name    : VideoHandler
          vendor_id       : [0][0][0][0]
          encoder         : h264
      Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 32000 Hz, mono, fltp, 96 kb/s
        Metadata:
          creation_time   : 2020-08-26T08:47:07.000000Z
          handler_name    : SoundHandler
          vendor_id       : [0][0][0][0]
    Invalid file index 1 in filtergraph description
    [0:v]trim=start=1,scale=1920:1080,setpts=PTS-STARTPTS[f_0]; [0:a]atrim=start=1,asetpts=PTS-STARTPTS[a_0];
    [1:v]trim=start=1,scale=640:360,setpts=PTS-STARTPTS[r_0];
    [f_0][r_0]overlay=x=(main_w-overlay_w-10):y=(main_h-overlay_h-10)[mix_0];
    [2:v]trim=start=1,scale=1920:1080,setpts=PTS-STARTPTS[f_1]; [2:a]atrim=start=1,asetpts=PTS-STARTPTS[a_1];
    [3:v]trim=start=1,scale=640:360,setpts=PTS-STARTPTS[r_1];
    [f_1][r_1]overlay=x=(main_w-overlay_w-10):y=(main_h-overlay_h-10)[mix_1];
    [4:v]trim=start=1,scale=1920:1080,setpts=PTS-STARTPTS[f_2]; [4:a]atrim=start=1,asetpts=PTS-STARTPTS[a_2];
    [5:v]trim=start=1,scale=640:360,setpts=PTS-STARTPTS[r_2];
    [f_2][r_2]overlay=x=(main_w-overlay_w-10):y=(main_h-overlay_h-10)[mix_2];
    [mix_0][a_0][mix_1][a_1][mix_2][a_2]concat=n=3:v=1:a=1[v][a]
    

    What am I doing wrong?

  • Error loading trimmed video produced by FFMPEG

    12 mai, par Bryan Carty

    I've been trying to trim a video using FFMPEG within some javascript code.

    The code executes without throwing any errors.

      try {
        const command = `ffmpeg -i ${
          CROPPING_DIR + CROPPED_VIDEO_PREFIX + id + ".mp4"
        } -ss 00:00:04 -to 00:00:06 -c copy ${
          CROPPING_DIR + TRIMMED_VIDEO_PREFIX + id + ".mp4"
        }`;
        console.log(command);
        const { stdout, stderr } = await exec(command);
        return "NA";
      } catch (error) {
        console.error("Error executing ffmpeg command:", error);
      }
    

    In VsCode, I am unable to view the trimmed video. Instead I see 'An error occurred while loading the video file'.

    Viewing from the file explorer, no video frames are displayed, but I can see the video progress bar moving.

    I've attempted a number of FFMPEG commands but am getting the same result.

    ffmpeg -i C:/Users/bryan/Desktop/Other/project/code/project/app/cropping_images/_cropped_d372d8ff-84c8-4cab-9359-513ee6ad7536.mp4 -ss 00:00:04 -to 00:00:06 -c copy C:/Users/bryan/Desktop/Other/project/code/project/app/cropping_images/_trimmed_d372d8ff-84c8-4cab-9359-513ee6ad7536.mp4
    
    ffmpeg -i C:/Users/bryan/Desktop/Other/project/code/project/app/cropping_images/_cropped_87f094f2-b06f-4c21-bd41-ae5e4ca222ae.mp4 -ss 1.05 -to 2.45 -c copy C:/Users/bryan/Desktop/Other/project/code/project/app/cropping_images/_trimmed_87f094f2-b06f-4c21-bd41-ae5e4ca222ae.mp4
    
    ffmpeg -i C:/Users/bryan/Desktop/Other/project/code/project/app/cropping_images/_cropped_f5b89e96-3efc-4a6e-bba4-428751fb38eb.mp4 -ss 0.00 -to 6.93 -c:v copy -c:a copy C:/Users/bryan/Desktop/Other/project/code/project/app/cropping_images/_trimmed_f5b89e96-3efc-4a6e-bba4-428751fb38eb.mp4
    
  • Losslessly Split Videos by Size [closed]

    11 mai, par user1897354

    How do I losslessly split videos by file size? For example, I have a 1.92GB video and I want to split it into 200MB videos.

    I tried using ffmpeg and putting in -fs, -c copy, and 200000000 for 200MB, but I only got one file that's not even the correct file size. How do I properly use it?

  • How to stream only a part of a video without downloading the whole thing ?

    11 mai, par Abbas

    I am building a YouTube video trimmer, and currently, it takes the YouTube video ID, downloads the whole video from the server (Even it is like 10 hours long), and then it trims it according to user's inputted timeframe using ffmpeg.

    Now the problem with this is that it takes so much time to download the whole video even if we want a small piece of it hence it is highly impractical.

    I was thinking to implement something like an HTML5 video player does when you seek the video forward. It just jumps to the part where you seek-ed to without downloading the part you skipped over. How can I only download a part of a video file from a server in form of a buffer and then generate an already trimmed video file from that? I don't know if that is even possible on the server-side, but video players do it on the client-side.

  • Xvfb records a black screen

    11 mai, par Vivek

    I am trying a record a video by running xvfb inside a docker image. No matter what I do it gives me black screen.

    Screen size same in xvfb and ffmpeg and puppeteer.

    It will would really great if someone can help.

    
    start-xvfb.sh
    ---------------------------------------------------------------------
    # Start Xvfb
    Xvfb :99 -screen 0 1280x720x24 &
    
    # Set the display environment variable
    export DISPLAY=:99
    
    # Run the application (assuming it starts with npm start)
    npm run dev
    

    Dockerfile

    FROM node:lts-alpine3.19
    
    # Install dependencies using apk
    RUN apk update && \
        apk add --no-cache \
        gnupg \
        ffmpeg \
        libx11 \
        libxcomposite \
        libxdamage \
        libxi \
        libxtst \
        nss \
        cups-libs \
        libxrandr \
        alsa-lib \
        pango \
        gtk+3.0 \
        xvfb \
        bash \
        curl \
        udev \
        ttf-freefont \
        chromium \
        chromium-chromedriver
    
    # Set working directory
    WORKDIR /app
    
    # Copy package.json and install dependencies
    COPY package.json .
    RUN npm install --force
    
    # Copy remaining source code
    COPY . .
    
    # Add a script to start Xvfb
    COPY start-xvfb.sh /app/start-xvfb.sh
    RUN chmod +x /app/start-xvfb.sh
    
    # Expose the port
    EXPOSE 4200
    EXPOSE 3000
    
    # Command to start Xvfb and run the application
    CMD ["./start-xvfb.sh"]
    

    Below

    this is code code that launches puppeteer and from a nodejs application and create spawns a process for ffmpeg

    export class UnixBrowserRecorder implements Recorder {
    
      url = 'https://stackoverflow.com/questions/3143698/uncaught-syntaxerror-unexpected-token'; // Replace with your URL
      outputFilePath = `/app/output_video.mp4`; // Output file path within the container
      durationInSeconds = 6; // Duration of the video in seconds
      resolution = '1280x720';
    
      public async capture(): Promise {
        const browser = await puppeteer.launch({
          args: [
            '--no-sandbox', // Required in Docker
            '--disable-setuid-sandbox', // Required in Docker
            '--disable-dev-shm-usage', // Required in Docker
            '--headless', // Run browser in headless mode
            '--disable-gpu', // Disable GPU acceleration
            `--window-size=${this.resolution}` // Set window size
          ],
          executablePath: '/usr/bin/chromium' // Specify the path to Chromium executable
        });
    
        const page = await browser.newPage();
        await page.goto(this.url);
    
        await page.screenshot({
          "type": "png", // can also be "jpeg" or "webp" (recommended)
          "path": `/app/screenshot.png`,  // where to save it
          "fullPage": true,  // will scroll down to capture everything if true
        });
    
        //ffmpeg -video_size `DISPLAY=:5 xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -framerate 30 -f x11grab -i :5.0+0,0 output.mpg
    
        const ffmpegProcess = spawn('ffmpeg', [
          '-video_size', this.resolution,
          '-framerate', '30',
          '-f', 'x11grab',
          '-i', ':99', // Use display :99 (assuming Xvfb is running on this display)
          '-t', this.durationInSeconds.toString(),
          '-c:v', 'libx264',
          '-loglevel', 'debug',
          '-pix_fmt', 'yuv420p',
          this.outputFilePath
        ]);
    
        // Log ffmpeg output
        ffmpegProcess.stdout.on('data', data => {
          console.log(`ffmpegProcess stdout: ${data}`);
        });
    
        ffmpegProcess.stderr.on('data', data => {
          console.error(`ffmpegProcess stderr: ${data}`);
        });
    
        // Handle ffmpegProcess process exit
        ffmpegProcess.on('close', code => {
          console.log(`ffmpeg process exited with code ${code}`);
        });
    
        // Wait for the duration to complete
        await new Promise(resolve => setTimeout(resolve, this.durationInSeconds * 1000));
    
        // Close the FFmpeg stream and process
        ffmpegProcess.stdin.end();
        // Close Puppeteer
        await page.close();
        await browser.close();
    
        return "Video generated successfully";
      }
    }
    

    enter image description here