You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求批量裁剪不同时长视频末尾6秒的脚本方案

Batch Trim Last 6 Seconds from Multiple Videos

Hey there! I’ve got you covered with a reliable solution using FFmpeg—it’s the industry standard for command-line video processing, perfect for batch tasks like this. Here’s how to set it up for different operating systems:

Prerequisites

First, make sure FFmpeg is installed on your system:

  • For Windows: Download the official FFmpeg build, extract it, and add the bin folder to your system PATH.
  • For macOS: Use Homebrew with brew install ffmpeg.
  • For Linux: Use your package manager, e.g., sudo apt install ffmpeg on Debian/Ubuntu.

Batch Scripts

Windows (Command Prompt or PowerShell)

Create a .bat file (e.g., trim_videos.bat) in the folder with your videos, paste this code, and run it:

@echo off
setlocal enabledelayedexpansion

set "trim_duration=6"

for %%f in (*.mp4 *.mov *.avi *.mkv) do (
    echo Processing "%%f"...
    :: Get total duration of the video
    for /f "tokens=*" %%d in ('ffmpeg -i "%%f" 2^>^&1 ^| findstr /i "Duration"') do (
        set "duration_line=%%d"
        set "duration=!duration_line:~12,11!"
    )
    :: Convert duration to total seconds
    for /f "tokens=1-3 delims=:" %%a in ("!duration!") do (
        set /a total_seconds=%%a*3600 + %%b*60 + %%c
    )
    :: Calculate new duration (total - 6 seconds)
    set /a new_seconds=total_seconds - trim_duration
    :: Convert back to HH:MM:SS.ss format
    set /a hours=new_seconds/3600
    set /a remaining=new_seconds%%3600
    set /a minutes=remaining/60
    set /a seconds=remaining%%60
    set "new_duration=!hours!:!minutes!:!seconds!.00"

    :: Trim the video (copy codec to avoid re-encoding if possible)
    ffmpeg -i "%%f" -t "!new_duration!" -c:v copy -c:a copy "trimmed_%%f"
)

echo All videos processed!
pause

Linux/macOS (Bash Script)

Create a .sh file (e.g., trim_videos.sh) in your video folder, paste this code, make it executable (chmod +x trim_videos.sh), then run it:

#!/bin/bash

TRIM_DURATION=6

for file in *.mp4 *.mov *.avi *.mkv; do
    if [ -f "$file" ]; then
        echo "Processing $file..."
        # Get total duration in seconds
        total_seconds=$(ffmpeg -i "$file" 2>&1 | grep -oP 'Duration: \K[0-9:.]+' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
        # Calculate new duration
        new_seconds=$(echo "$total_seconds - $TRIM_DURATION" | bc)
        # Convert back to HH:MM:SS format
        new_duration=$(printf "%02d:%02d:%06.3f" $(echo "$new_seconds/3600" | bc) $(echo "($new_seconds%3600)/60" | bc) $(echo "$new_seconds%60" | bc))
        
        # Trim the video (copy codecs to preserve quality)
        ffmpeg -i "$file" -t "$new_duration" -c:v copy -c:a copy "trimmed_$file"
    fi
done

echo "All videos processed!"

Key Notes

  • Codec Copy: The -c:v copy -c:a copy flags copy the original video/audio codecs directly, so there’s no quality loss and processing is fast. If this fails (e.g., the video has misaligned keyframes), remove those flags—FFmpeg will re-encode the video, which takes longer but works reliably.
  • Supported Formats: The scripts handle common formats like MP4, MOV, AVI, MKV. Add more extensions to the for loop if you need others.
  • Backup: Always back up your original videos before running batch scripts—better safe than sorry!
  • Test First: Try running the script on one video first to make sure it works as expected before processing all files.

内容的提问来源于stack exchange,提问作者Marvin von Rappard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:43:48