How can I use ffmpeg to split videos into 3 minute chunks? - ffmpeg

i want to split a video that is 21 minutes long into 7 videos of 3 minutes long using ffmpeg in cmd windows. The command I'm using is this but it gives me an error.
for /L %i in (0,1,7) do ffmpeg -i test.mp4 -ss 00:$(%i*3):00 -t 00:03:00 -c copy outputs%i.mp4
If someone could tell me the error and how to fix it I would appreciate it.
that the video is trimmed consecutively

Related

FFMPEG Seeking with concat demuxer causes video & audio to be out of sync

I have a very simple use case that's driving me bananas.
My problem and question:
I'm using ffmpeg version 5.1.2 on a MacOS and i'm using ffmpeg seeking and concat demuxer to cut many 1 minute videos into 15 seconds chopped up over 12 clips where every clip is just 2 seconds from the same video (kind of like a mini teasers for the video). I would really like to not have to re-encode to make the video processing as fast as possible.
First, I take each 1 minute video and cut it up into 12 clips (I do all this programmatically in python fwiw)
ffmpeg -ss 0 -i input.mp4 -t 2 -c copy -y cut_1.mp4
ffmpeg -ss 4 -i input.mp4 -t 2 -c copy -y cut_2.mp4
ffmpeg -ss 8 -i input.mp4 -t 2 -c copy -y cut_3.mp4
...
...
I then write all the output file names to my concat_manifest.txt
file cut_1.mp4
file cut_2.mp4
...
...
Then I run my concat command:
ffmpeg -f concat -i concat_manifest.txt -c copy -y concat_video.mp4
This works really fast but the audio and video at the stitch point get out of sync and sometimes the video just chokes & lags. It's mostly not a smooth experience.
What I have tried:
using the concat protocol with intermediate profiles: ffmpeg.org/wiki/Concatenate#demuxer
Putting the -ss when I seek after the -i. This makes everything worse
Playing around with different -ss values. This has some noticeable affects but it's not obvious why yet.
I've also read from the ffmpeg resource regarding seeking and copying:
Which leads me to believe that maybe because ffmpeg is using timestamps instead of frames, seeking isn't accurate using -ss when using the concat demuxer
Is there a way to get concat demuxer cutting and concatenating the video where the audio is somewhat in sync with the video?
Thanks
EDIT: I found an answer and i'll be posting the solution in the coming few days.

ffmpeg: cutting 20 seconds of video starting from the middle time in a bash cycle

im in a bash script looping a dir full of .mp4 video files each one with a different lenght.
Now: how can i cut 20 seconds starting in the exact middle of any video?
For a single file i can read manually the duration of the video so i know at what second start cutting: e.g.
for a 120 sec. video i use
ffmpeg -i "tmpfile.mp4" -ss 60 -t 20 -c copy "outputfile.mp4" 2>/dev/null
but now the problem is that the "-ss" value should be a variable with the (total lenght of video/2)
some videos are long less than a minute, some are long many minutes, and some are more than one hour.
Thanks
Cant find a middle time value to start cutting
Use ffprobe
len=$(ffprobe -v error -show_entries format=duration -of compact=p=0:nk=1 input.mp4)
mid=$((${len%.*} / 2))
ffmpeg -v error -ss $mid -i input.mp4 -t 20 -c copy output.mp4

ffmpeg - loop MP4 for X Hours - sound missing

Hey can someone help me with this script:
I want to loop the out.mp4 file up to 1 hous.
It is working it loop the out.mp4 up to 1 hour and save it as finalVid.mp4 but it has no sound. I checked the out.mp4 here the sound is still fine but after executing this script the sound is gone and i just have a 1 hour video.
ffmpeg -y -f lavfi -i \"movie=filename=out.mp4:loop=0, setpts=N/(FRAME_RATE*TB)\" -t 3600 finalVid.mp4
what i need to add here to get sound or someone can tell me other way to loop it up to 1h with ffmpeg?

Downloading a Few Seconds of Audio from a Youtube Video with ffmpeg and youtube-dl Results in [youtube]: No such file or directory error

Below is the program that I wrote:
ffmpeg -ss 60 -t 10 -i $(youtube-dl -f 140 https://www.youtube.com/watch?v=kDCk3hLIVXo) output.mp3
This is supposed to get 10 seconds of audio starting at the one minute mark of the video and write it to output.mp3. If I run the youtube-dl command separately, and then the ffmpeg command with the entire video audio as input, it works. But, I do not want to download the entire video as well as create a new file with only a few seconds of audio.
In its current state, I am getting [youtube]: No such file or directory errors. Does anyone know how I can fix this and keep it in one line?
The issue is the output which is returned from youtube-dl is several lines of information, so ffmpeg doesn't know how to deal with it properly.
You'll want to return the actual name of the file without any other information included; a tool like awk or sed can be helpful for this. In addition there will need to be an encoding step added at the end so the audio stream gets copied to the output file (libmp3lame->mp3).
Example:
ffmpeg -ss 60 -t 10 $(youtube-dl -f 140 -g https://www.youtube.com/watch?v=kDCk3hLIVXo | \
sed "s/.*/-i &/") -c:v copy -codec:a libmp3lame output.mp3
This command should return an audio mp3 file 60 seconds in which is 10 seconds in duration.
Result:
output.mp3: Audio file with ID3 version 2.4.0, contains:MPEG ADTS, layer III, v1, 64 kbps, 44.1 kHz, Stereo

create thumbnails from big movies with FFmpeg takes too long

I'm using this shell command to make thumbnail from VIDEO_FILE from 123 second and save it to THUMBNAIL_FILE.
ffmpeg -i VIDEO_FILE -r 1 -ss 123 -f image2 THUMBNAIL_FILE
It works, but it is really slow for big movies. Is there any way to make it a little faster?
it has happened to me also, changing the argument order fixes this problem.
tested on a 1.4GB 90 minute mp4 video - took about 1-2 seconds. before that it took MINUTES...
try this:
ffmpeg -ss 123 -i "VIDEO_FILE" "THUMBNAIL_FILE" -r 1 -vframes 1 -an -vcodec mjpeg
Ffmpeg is not really good with creating thumbnails as I investigated. People recommend to use mplayer (by ffmpeg creators).
mplayer VIDEO_FILE -ss 00:10:11 -frames 1 -vo jpeg:outdir=THUMBNAILS_DIRECTORY
A small enhancement to Kirzilla's code: If you want to create PNG files (with compression), you can use the following code:
mplayer VIDEO_FILE -ss 00:10:11 -frames 1 -vo png:z=9:outdir=THUMBNAILS_DIRECTORY
This will probably create better thumbnails but of course with a larger size than JPEG.

Resources