FORUMS: list search recent posts

Batch trim video files

COW Forums : FFmpeg

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
Andy Seru
Batch trim video files
on Oct 6, 2015 at 3:30:55 pm

Hi all,

I'm trying to find a way to batch trim multiple video files. I'm not sure if ffmpeg is the best way to do this, but after reading some posts and articles, I think it's the best solution.
So I have around 500 video files with different durations, and what I wanna do is to cut off the first and the last frame of every clip (I also need to convert the files to ProRes, but that could happen in another step). Sure one could do it manually for every clip, but there has to be an easier way with a script and a batch process.
Can anyone help me on this?

Cheers,
Andy


Return to posts index

Chad Gilmour
Re: Batch trim video files
on Oct 6, 2015 at 5:11:45 pm

There's basically 2 steps for this, but you can run them both at once.
1st step will be setting up a loop, but that will vary depending on your operating system and whether or not every file is the same length.

2nd step is using the appropriate flags.

"-ss" will cut off seconds of video at the beginning
"-t" will encode for a given amount of seconds starting at the very beginning of the file even if you are using the "-ss" flag
"-vcodec copy" will copy the input file's video codec
"-acodec copy" will copy the input file's audio codec

example: ffmpeg -ss 1 -t 10 inputfile.mov -vcodec copy -acodec copy inputfile_new.mov

The above command will result in a 9 second file with the same exact video and audio codecs, except the first second from the input file will be missing.

You will have to do a little math and some testing to make sure it works, if you do "-ss .0334" it should shave off the first frame if you file is 29.97 frames per second.

If all you're files are the same length then setting up a loop to apply the same "-t" flag command will be fairly easy, if they are all different lengths it will still be possible, but I'm not smart enough to figure it out.

Hope that helps.

What OS are you using?


Return to posts index

Andy Seru
Re: Batch trim video files
on Oct 7, 2015 at 7:45:00 am

Hi Chad,

thanks for your reply!
I've got Win and Mac workstations but Mac would be preferred.
I've read about the "-ss" but never found something that does the same at the end of the clip. The problem is, that the clips have different durations, so I probably need something that calculates the clip length and then cuts off the last frame. You think that could work somehow?

Thank you!
Andy


Return to posts index


Chad Gilmour
Re: Batch trim video files
on Oct 7, 2015 at 2:27:45 pm

yeah, there are ways to calculate the length of the file and then automatically/dynamically change the integer for the "-t" flag, but that's over my head. You could try something using python maybe, or apple script could help. Here's the command (assuming all the files are in 1 folder) to trim the first frame, sorry I can't help with the last part.

for "$i" do; ffmpeg -ss .033 -i /Volumes/folder/"$1" -vcodec copy -acodec copy /Volumes/folder/new_media/"$i"

I think that will go through an entire folder, trim the first frame, copy the video/audio and then put the new file in whatever folder path you specify at the end there all while retaining the original name.

As far as creating a variable that's automatically replaced based on the length, you might be able to employ some help from a coder at fiverr.com I've been using a guy over there to help write some simple python scripts to automate ffmpeg on a mac in our office. You will have to figure out how to get a frame accurate total run time on a file though, I'm not sure how to do that.


Return to posts index

Gyan Doshi
Re: Batch trim video files
on Oct 11, 2015 at 8:27:30 am

"-t" will encode for a given amount of seconds starting at the very beginning of the file even if you are using the "-ss" flag

No. 't' represents the duration to encode. So '-ss 15 -t 10 -i file' will work on 10 seconds of the input, starting from the 15th.


Return to posts index

Chad Gilmour
Re: Batch trim video files
on Oct 12, 2015 at 6:59:33 pm

Sorry, I don't use the "-t" flag much, I didn't realize it took the other flags into consideration.


Return to posts index


Andy Seru
Re: Batch trim video files
on Oct 13, 2015 at 8:12:11 am

Hi guys,
thanks for your comments. I found someone who wrote me a script and it does delete the first and the last frame, but unfortunately it also deletes some other frames (always the same numbers 54, 79, 104, ...). I guess that's got something to do with the encoding to ProRes, but we didn't find a solution yet. I'll keep you updated.


Return to posts index

Chad Gilmour
Re: Batch trim video files
on Oct 13, 2015 at 2:01:52 pm

I'm just speculating now, but I'm guessing it might be a frame rate issue. Try specifying the input frame rate and the output frame rate.

For example: ffmpeg -ss 1 -t 10 -r 29.97 -i inputfile.mov -r 29.97 -vocdec copy -acodec copy outputfile.mov

I could be wrong on where I put the -r flags, so you might want to experiment, or maybe someone else can offer a suggestion. I think that -r before the -i flag tells ffmpeg that the incoming file will be treated as 29.97fps and the -r flag after the -i flag tells ffmpeg that the new file will be encoded at 29.97fps. I also know there's some sort of "force frame rate" option as well, but I've never used it, so I don't know if it would work for you here.

What frame rate are you using and are all of your video files the same frame rate?


Return to posts index

Andy Seru
Re: Batch trim video files
on Oct 13, 2015 at 3:57:08 pm

Yeah it was a framerate issue and even with the -r flags we didn't have any succes. Then we changed the encoding line and now it's all working!
Thanks for your help again!


Return to posts index


Chad Gilmour
Re: Batch trim video files
on Oct 13, 2015 at 5:02:44 pm

Where did you get help to write your script? I'm always on the look out for good sources for that sort of work and help.


Return to posts index

Mark Burton
Re: Batch trim video files
on Dec 5, 2015 at 2:34:59 pm

Would really love to see how to did this, we're currently trying to find the best method to trim handles from shots. Looking into using the select filter, but not found the solution yet. Be great to see what you ended up using.

Thanks



Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]