Trimming a video to use a source background in a scene

Rah

New Member
Hi all. First off, I'm new to the forum. I apologize if this thread is in the wrong place.

What I am trying to do is use a video file as a background to my "Getting ready to start streaming" scene. It works great. The issue is that I only want to loop a section in the middle of the video.
For example: Instead of starting the loop at frame 1 and ending at the last frame, I want to start the loop at frame 200 and ending at frame 1400

I can edit the video in a video editor no problem, but I was just wandering if such a task was possible within OBS(with or without scripts or plugins). If I am able to accomplish this in OBS, it would save some extra steps.

I have not been able to find a solution on my own, so I am asking the community for their help. Any input will be greatly appreciated.
Thank you in advance
 

CodeYan

New Member
You could use ffmpeg. There are several options that you could use. They are documented here:

Usage:
ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Some of the options you might be interested in (taken from executing:
-c codec codec name
-ss time_off set the start time offset
-t duration record or transcode "duration" seconds of audio/video (will override -to)
-to time_stop record or transcode stop time (cannot be used with -t)
-frames number set the number of frames to output

Take note, however, that -ss, -t, and -to require either the 'hh:mm:ss.ms' or 'ss.ms' format. Thus, if you want to trim based on frames and not seconds, you'll need to convert the number of frames to a timestamp.
Assuming your file is 30 fps, getting the 200th frame will be 200/30 = 6.67 seconds. Then the duration will be (1400-200) / 30 = 40 s.
ffmpeg -ss 6.67 -t 40 -i input.mp4 out.mp4
It is recommended to put -ss before -i so that ffmpeg seeks faster to that point. Otherwise, it will decode the first frames (and discard them) until the specified point is reached, which results in a very seek. Read more about it in the documentation.

Note that not specifying the codec results in reencoding, which is what you want. You can not use -c:v copy or -c:a copy (copy video and audio instead of reencoding) because not most frames in a video is not a keyframe (full picture). You may specify the codecs you want, however.
ffmpeg -ss 6.67 -t 40 -i input.mp4 -c:v libx264 -c:a aac out.mp4

As for doing this in OBS Studio, you can make a script to do this. For the encoding, call the ffmpeg command in the shell (in Python you would do subprocess.Popen(), which automatically performs the command in the background for you).
 

Rah

New Member
Thank you for your response CodeYan. I have already trimmed the video in another program, however I think that I will experiment with your suggestions because they may very well become useful in the future.

Thank you again
 
Top