FFMPEG to trim off last 3 seconds of videos

11,055

Solution 1

Cut video with ffmpeg.

use

ffmpeg -i input.mp4 -ss 3 -i input.mp4 -c copy -map 1:0 -map 0 -shortest -f nut - | ffmpeg -f nut -i - -map 0 -map -0:0 -c copy out.mp4

Solution 2

I don't think ffmpeg allows a "from end" spec for duration. You'll have to detect the video's duration yourself and subtract 3 seconds.

ffprobe -i input.mp4 -show_entries format=duration -v quiet -of csv="p=0"

You can do this in a script. For example in bash:

dur=$(ffprobe -i input.mp4 -show_entries format=duration -v quiet -of csv="p=0")
trim=$((dur - 3))
ffmpeg -t $trim -i input.mp4 output.mp4

Solution 3

Here is a bash script for convenience (adding to above answer):

#!/bin/bash

# Arguments
FILE_RAW=$1
TRIM_EOF_DURATION=${2:-1.0} # Default is 1.0 second trimmed from EOF

# Prepare variables
BASE_PATH=$(dirname $(readlink -f $FILE_RAW))
FILENAME_EXT="$(basename "${FILE_RAW}")"
FILENAME_ONLY="${FILENAME_EXT%.*}"
EXT_ONLY="${FILENAME_EXT#*.}" # Or hardcode it like "mp4"
FILENAME_ONLY_PATH="${BASE_PATH}/${FILENAME_ONLY}"

# Trim EOF duration
INPUT_DURATION=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "${FILENAME_ONLY_PATH}.${EXT_ONLY}")
OUTPUT_DURATION=$(bc <<< "$INPUT_DURATION"-"$TRIM_EOF_DURATION")
ffmpeg -i "${FILENAME_ONLY_PATH}.${EXT_ONLY}" -map 0 -c copy -t "$OUTPUT_DURATION" "${FILENAME_ONLY_PATH}_Trim_${TRIM_EOF_DURATION}.${EXT_ONLY}"

Note: Make script executable: chmod +x trim_video.sh

Usage (Output File: <PATH_TO_INPUT_VIDEO>_Trim_<TRIM_EOF_DURATION>.mp4)

. <PATH_TO_THIS_SCRIPT>/trim_video.sh <PATH_TO_INPUT_VIDEO> <OPTIONAL_TRIM_EOF_DURATION>

Example: Trim 3.0 seconds from EOF (Output: ~/Videos/input_video_Trim_3.0.mp4)

. ~/trim_video.sh ~/Videos/input_video.mp4 3.0
Share:
11,055
humble5050
Author by

humble5050

Updated on July 31, 2022

Comments

  • humble5050
    humble5050 almost 2 years

    I am trying to trim/cut off the last 3 secs of my videos with FFMPEG but this has really been an headache.

    The following code trims but only retains the last 3 seconds. I don't want to retain the 3 secs, i don't need that, i want to retain the deleted part.

    ffmpeg -sseof -3 -i input.mp4 output.mp4

    Can someone please help me with the right code?. I will also like to request a batch code that will auto trim all last 3 secs of videos in my folder. Thanks for the help.

  • humble5050
    humble5050 over 5 years
    Thank you for your response, but unfortunately, i am on a windows PC, can u tell me how to run that on windows PC?
  • L. Scott Johnson
    L. Scott Johnson over 5 years
    I'd think Windows batch also allows you to store the output of a command and perform arithmetic. Just replace the $(...) stuff with the appropriate syntax.
  • L. Scott Johnson
    L. Scott Johnson over 5 years
    (In general, StackOverflow is a question-answering platform, not a code-writing service.)
  • llogan
    llogan over 5 years
    This is the same as doing ffmpeg -i input.mp4 -ss 3 -c copy output.mp4. However, this removes the tail of the video, but humble5050 wants to keep that part only.
  • humble5050
    humble5050 about 5 years
    Thanks nico_lab,,,this is exactly what i am looking for...u r awesome
  • Phani Rithvij
    Phani Rithvij over 4 years
    Great answer but care to explain what it's doing?
  • Moss
    Moss over 4 years
    Windows batch sucks and the syntax for capturing the output of a command into a variable is extremely ugly and requires a for loop! stackoverflow.com/questions/6359820/…
  • nico_lab
    nico_lab over 4 years
    -ss 3 is input option of second input.mp4. -map 1:0 -map 0 stdout, but output -map 1:0 is shorten 3 seconds, then output -map 0 is shorten by the last 3 seconds by -shortest. -map -0:0 is deleted from -map 1:0. I wrote in Japnease, if you can read. ffmpeg で先頭と後ろを一度にカットする | ニコラボffmpeg で指定時間でカットするまとめ | ニコラボ
  • kohane15
    kohane15 about 2 years
    exactly what I need too! Been looking for this for months; previously I was just manually inputting the duration. Thanks!