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.

Chong Onn Keat
  • 520
  • 2
  • 8
  • 19
humble5050
  • 111
  • 1
  • 1
  • 3
  • The usage for tag ffmpeg reads *Questions about interactive use of the command line tool should be asked on Super User or Video Production.* – greybeard Feb 17 '22 at 11:25

3 Answers3

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
nico_lab
  • 1,044
  • 1
  • 8
  • 10
  • 1
    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. – llogan Mar 26 '19 at 17:07
  • Thanks nico_lab,,,this is exactly what i am looking for...u r awesome – humble5050 Mar 27 '19 at 18:59
  • 2
    Great answer but care to explain what it's doing? – Phani Rithvij Jan 09 '20 at 21:21
  • 1
    -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 で先頭と後ろを一度にカットする | ニコラボ](https://nico-lab.net/cut_before_after_with_ffmpeg/)、[ffmpeg で指定時間でカットするまとめ | ニコラボ](https://nico-lab.net/cutting_ffmpeg/) – nico_lab Jan 11 '20 at 13:44
  • exactly what I need too! Been looking for this for months; previously I was just manually inputting the duration. Thanks! – kohane15 May 19 '22 at 07:33
7

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
L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
  • Thank you for your response, but unfortunately, i am on a windows PC, can u tell me how to run that on windows PC? – humble5050 Mar 25 '19 at 12:15
  • 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 Mar 25 '19 at 12:17
  • (In general, StackOverflow is a question-answering platform, not a code-writing service.) – L. Scott Johnson Mar 25 '19 at 12:19
  • 1
    Windows batch sucks and the syntax for capturing the output of a command into a variable is extremely ugly and requires a for loop! https://stackoverflow.com/questions/6359820/how-to-set-commands-output-as-a-variable-in-a-batch-file – Moss Jan 10 '20 at 20:13
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
sagunms
  • 8,030
  • 5
  • 41
  • 43