I tried to cut the video using the start and end time of the video by using the following command
ffmpeg -ss 00:00:03 -t 00:00:08 -i movie.mp4 -acodec copy -vcodec copy -async 1 cut.mp4
By using the above command i want to cut the video from 00:00:03
to 00:00:08
. But it is not cutting the video between those times instead of that it is cutting the video with first 11 seconds. can anyone help me how resolve this?
Edit 1:
I have tried to cut by using the following command which is suggested by mark4o
ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 cut.mp4
But it was shown the following error.
the encoder 'aac' is experimental but experimental codecs are not enabled
so i added the -strict -2
into the command i.e.,
ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 -strict -2 cut.mp4
Now it is working fine.
This question is related to
linux
video
ffmpeg
video-editing
ffmpeg -i movie.mp4 -vf trim=3:8 cut.mp4
Drop everything except from second 3 to second 8.
feel free to use this tool https://github.com/rooty0/ffmpeg_video_cutter I wrote awhile ago Pretty much that's cli front-end for ffmpeg... you just need to create a yaml what you want to cut out... something like this
cut_method: delete # we're going to delete following video fragments from a video
timeframe:
- from: start # waiting for people to join the conference
to: 4m
- from: 10m11s # awkward silence
to: 15m50s
- from: 30m5s # Off-Topic Discussion
to: end
and then just run a tool to get result
Use -to instead of -t: -to specifies the end time, -t specifies the duration
Even though I'm 6 years late, but I think all the answers above didn't properly address the question @kalai is asking. The bash script below will process a text file in the following format:
URL | start_time | end_time | filename
for example
https://www.youtube.com/watch?v=iUDURCrvrMI|00:02:02|00:03:41|1
and loop through the file, downloads the file that youtube-dl
supports, calculating duration between start_time
and end_time
and passing it to ffmpeg
, since -t
is actually the duration, not the real end_time
Hit me up if you have any question.
for i in $(<video.txt);
do
URL=`echo $i | cut -d "|" -f 1`;
START=`echo $i | cut -d "|" -f 2`;
END=`echo $i | cut -d "|" -f 3`;
FILE=`echo $i | cut -d "|" -f 4`;
SEC1=`echo $START | sed 's/^/((/; s/:/)*60+/g' | bc`
SEC2=`echo $END | sed 's/^/((/; s/:/)*60+/g' | bc`
DIFFSEC=`expr ${SEC2} - ${SEC1}`
ffmpeg $(youtube-dl -g $URL | sed "s/.*/-ss $START -i &/") -t $DIFFSEC -c copy $FILE".mkv";
ffmpeg -i $FILE".mkv" -f mp3 -ab 192000 -vn $FILE".mp3";
rm $FILE".mkv";
done;
Try using this. It is the fastest and best ffmpeg-way I have figure it out:
ffmpeg -ss 00:01:00 -i input.mp4 -to 00:02:00 -c copy output.mp4
This command trims your video in seconds!
Explanation of the command:
-i: This specifies the input file. In that case, it is (input.mp4).
-ss: Used with -i, this seeks in the input file (input.mp4) to position.
00:01:00: This is the time your trimmed video will start with.
-to: This specifies duration from start (00:01:40) to end (00:02:12).
00:02:00: This is the time your trimmed video will end with.
-c copy: This is an option to trim via stream copy. (NB: Very fast)
The timing format is: hh:mm:ss
Please note that the current highly upvoted answer is outdated and the trim would be extremely slow. For more information, look at this official ffmpeg article.
ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 -c copy cut.mp4
Use -c copy for make in instantly. In that case ffmpeg will not re-encode video, just will cut to according size.
Here's what I use and will only take a few seconds to run:
ffmpeg -i input.mp4 -ss 01:19:27 -to 02:18:51 -c:v copy -c:a copy output.mp4
Reference: https://www.arj.no/2018/05/18/trimvideo
Generated mp4
files could also be used in iMovie
. More info related to get the full duration using get_duration(input_video) modele.
If you want to concatenate multiple cut scenes you can use following Python script:
#!/usr/bin/env python3
import subprocess
def get_duration(input_video):
cmd = ["ffprobe", "-i", input_video, "-show_entries", "format=duration",
"-v", "quiet", "-sexagesimal", "-of", "csv=p=0"]
return subprocess.check_output(cmd).decode("utf-8").strip()
if __name__ == "__main__":
name = "input.mkv"
times = []
times.append(["00:00:00", "00:00:10"])
times.append(["00:06:00", "00:07:00"])
# times = [["00:00:00", get_duration(name)]]
if len(times) == 1:
time = times[0]
cmd = ["ffmpeg", "-i", name, "-ss", time[0], "-to", time[1], "-c:v", "copy", "-c:a", "copy", "output.mp4"]
subprocess.check_output(cmd)
else:
open('concatenate.txt', 'w').close()
for idx, time in enumerate(times):
output_filename = f"output{idx}.mp4"
cmd = ["ffmpeg", "-i", name, "-ss", time[0], "-to", time[1], "-c:v", "copy", "-c:a", "copy", output_filename]
subprocess.check_output(cmd)
with open("concatenate.txt", "a") as myfile:
myfile.write(f"file {output_filename}\n")
cmd = ["ffmpeg", "-f", "concat", "-i", "concatenate.txt", "-c", "copy", "output.mp4"]
output = subprocess.check_output(cmd).decode("utf-8").strip()
Example script will cut and merge scenes in between 00:00:00 - 00:00:10
and 00:06:00 - 00:07:00
.
If you want to cut the complete video (in case if you want to convert mkv
format into mp4
) just uncomment the following line:
# times = [["00:00:00", get_duration(name)]]
You can make bash do the math for you, and it works with milliseconds.
toSeconds() {
awk -F: 'NF==3 { print ($1 * 3600) + ($2 * 60) + $3 } NF==2 { print ($1 * 60) + $2 } NF==1 { print 0 + $1 }' <<< $1
}
StartSeconds=$(toSeconds "45.5")
EndSeconds=$(toSeconds "1:00.5")
Duration=$(bc <<< "(${EndSeconds} + 0.01) - ${StartSeconds}" | awk '{ printf "%.4f", $0 }')
ffmpeg -ss $StartSeconds -i input.mpg -t $Duration output.mpg
This, like the old answer, will produce a 15 second clip. This method is ideal even when clipping from deep within a large file because seeking isn't disabled, unlike the old answer. And yes, I've verified it's frame perfect.
NOTE: The start-time is INCLUSIVE and the end-time is normally EXCLUSIVE, hence the +0.01
, to make it inclusive.
If you use mpv
you can enable millisecond timecodes in the OSD with --osd-fractions
To cut based on start and end time from the source video and avoid having to do math, specify the end time as the input option and the start time as the output option.
ffmpeg -t 1:00 -i input.mpg -ss 45 output.mpg
This will produce a 15 second cut from 0:45 to 1:00.
This is because when -ss
is given as an output option, the discarded time is still included in the total time read from the input, which -t
uses to know when to stop. Whereas if -ss
is given as an input option, the start time is seeked and not counted, which is where the confusion comes from.
It's slower than seeking since the omitted segment is still processed before being discarded, but this is the only way to do it as far as I know. If you're clipping from deep within a large file, it's more prudent to just do the math and use -ss
for the input.
Source: Stackoverflow.com