FFMPEG to trim off last 3 seconds of videos
Asked Answered
E

3

10

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.

Exoenzyme answered 25/3, 2019 at 9:40 Comment(1)
The usage for tag ffmpeg reads Questions about interactive use of the command line tool should be asked on Super User or Video Production.Spillage
G
13

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
Glossitis answered 26/3, 2019 at 14:6 Comment(5)
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.Puma
Thanks nico_lab,,,this is exactly what i am looking for...u r awesomeExoenzyme
Great answer but care to explain what it's doing?Misestimate
-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 で指定時間でカットするまとめ | ニコラボGlossitis
exactly what I need too! Been looking for this for months; previously I was just manually inputting the duration. Thanks!Danyelledanyette
O
8

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
Oriente answered 25/3, 2019 at 11:56 Comment(4)
Thank you for your response, but unfortunately, i am on a windows PC, can u tell me how to run that on windows PC?Exoenzyme
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.Oriente
(In general, StackOverflow is a question-answering platform, not a code-writing service.)Oriente
Windows batch sucks and the syntax for capturing the output of a command into a variable is extremely ugly and requires a for loop! #6360320Handedness
A
2

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
Amaryllidaceous answered 16/9, 2021 at 1:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.