1

I have a folder with 100 videos, I need to split all videos into 2, the split needs to happen 2 seconds before the end of every video.

I want to keep both parts of the video after the split.

So far I have the code below, this will trim 2 seconds from the end of every video that is in de same folder as the .bat file. It will put the new videos inside of a folder called 'new files'. The problem is that the length of the video is not trimmed. Only audio and video. So the last 2 seconds will just be the last frame and no audio.

for %%a in ("*.mp4") do ffmpeg -i "%%a" -filter_complex "[0]trim=2,setpts=PTS-STARTPTS[b];[b][0]overlay=shortest=1" -shortest -c:a copy "newfiles\%%~na.mp4" pause 
Leendert
  • 13
  • 1
  • 8
  • Can you share your attempts? – Enlico Dec 07 '19 at 21:32
  • I was able to cut a single video with .\ffmpeg -ss 0 -i 1.mp4 -c copy -map 0 -t 2 input-trimmed.mp4 And I tried https://stackoverflow.com/questions/49964271/cut-end-of-multiple-videos – Leendert Dec 08 '19 at 11:44
  • I've found that with: for %%a in ("*.mp4") do ffmpeg -i "%%a" -ss 2 -acodec copy -vcodec copy "newfiles\%%~na.mp4" pause I can handle all files in a certain folder and output them in new folder. Only thing now is that FFMPEG line has to be adjusted to just cut the last 2 seconds. – Leendert Dec 08 '19 at 12:37
  • Have you checked [this](https://stackoverflow.com/questions/5651654/ffmpeg-how-to-split-video-efficiently)? – Enlico Dec 08 '19 at 17:15
  • I checked it and this works when you know the length of your video. I want to run the script on a folder with multiple videos that have different lenghts. So the split of the clip should happen 2 seconds before the end, which is not always the same time depending on the lenght of the clip. – Leendert Dec 08 '19 at 21:15

1 Answers1

0

I've read again the answer you linked, and I think it actually is a better answer than mine to your question. The only difference is that you need to execute the command ffmpeg twice in the code that's written there.

This answer on SuperUser shows you how to use the -t and -ss options of ffmpeg, and gives some useful info about it.

I've tried it myself, but the second part of the video is not good, as I hear the sound with a steady photogram. As I'm not expert of video encoding/deconding, I really can't help you further.

I've update my original answer below to include the two commands I referred to earlier, plus some comments.

Maybe there's an easier way to get it done, but you can write a bash script to do what you want.

You can start with something like this:

#!/bin/bash
file=$1
filename=${1%.*}
extension=${1##*.}
splitTime=$(ffmpeg -i $file 2>&1 \
  | sed -n 's/^ *Duration: *\([^,]*\).*$/\1/p' \
  | awk -F: '{ print $1*3600 + $2*60 + $3 - 2.24 }')
ffmpeg -t  $splitTime -i $file -c copy $filename.part1.$extension
ffmpeg -ss $splitTime -i $file -c copy $filename.part2.$extension

which stores the duration of the video, reduced by 2.24 seconds, in the variable splitTime, and then uses it for two ffmpeg commands.

Note that ffmpeg accepts durations in 2 formats for both the -t and -ss options (look at man ffmpeg, which references man ffmpeg-utils).

They script the way I've set it up is meant to be used with a single file, whose name is in the format basename.extension. You can clearly call it from something like this:

for f in *.mp4; do
    that_script $f
done

or you can wrap the original into the for loop and adapt it to your needs.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • Probably due to the lack of knowledge in bash scripts I cannot get it to work. I've tried a different approach and I came up with the follow: for %%a in ("*.mp4") do ffmpeg -i "%%a" -filter_complex "[0]trim=2,setpts=PTS-STARTPTS[b];[b][0]overlay=shortest=1" -shortest -c:a copy "newfiles\%%~na.mp4" pause This is trimming the last 2 seconds from the clip. However the clip length is staying the same. So it does trim the video and audio but I want it to also delete those 2 seconds from the clip length. Any idea how to do that? – Leendert Dec 14 '19 at 14:42
  • Please, edit the question, rather than adding details through comments. As you can see yourself, code in comments is really unreadable (even more if you don't format it). Besides, where have you learned this use of the `%`? `%%a` is not a valid identifier, and `for %%a in ...` should fail immediately. – Enlico Dec 14 '19 at 15:58
  • Ok I've edit my question. the " for %%a " I've gotten from a YouTube video I've found, I assume this is what is taking every video in the folder instead of only 1 file name. Ty for the help so far btw! – Leendert Dec 15 '19 at 15:40
  • 1
    @EnricoMariaDeAngelis `ffmpeg` output is not meant to be machine parsed which can result in issues. [Use `ffprobe` instead](https://superuser.com/a/945604/110524) as that is what it is made for, and doing so will allow you to eliminate `sed` and `awk` as well. – llogan Dec 15 '19 at 20:47