humble5050
humble5050

Reputation: 121

FFMPEG to trim off last 3 seconds of videos

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.

Upvotes: 11

Views: 20541

Answers (3)

sagunms
sagunms

Reputation: 8515

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

Upvotes: 2

nico_lab
nico_lab

Reputation: 1174

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

Upvotes: 14

L. Scott Johnson
L. Scott Johnson

Reputation: 4382

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

Upvotes: 8

Related Questions