Exosylver
Exosylver

Reputation: 166

How to cut video properly with this ffmpeg python script

I am trying to write a python script that breaks a video into chunks of less than 64 mb each. This is my for loop that convert each chunk of broken up video:

for part in range(parts):
    print(start, end)
    subprocess.run(f"ffmpeg -i {filename} -vcodec copy -acodec copy -ss {start} -t {end} {newfile}\OutputPart{part}.mp4", shell=True)
    start = end
    end += partlength

I defined start to be 0 originally, and the end marker to be the length of each part.

For example, if the video file is 139 mb and 20 mins long, it takes the size divided by 64 [2.171875] turns it into an integer plus 1 in order to add a third part for any amount of video after the last 64 mb marker. Then, it takes that number of parts and divides the length of the video (in our case 20 mins = 1200 seconds) by the number of parts (the variable set previously as parts) which would get us (1200 seconds divided by 3 parts = ) 400 which is the length each part should be (partlength). Now, it runs a loop for the number of parts, to convert a video with the start point (denoted in the ffmpeg command as -ss) originally 0, and the end point (-t), originally the length of 1 part (in our case 400). after the first run through, to make sure the start and endpoints are correct, it prints the start and end points. All the runs say the correct start and endpoints (0-400;400-800;800-1200). The first and third files convert perfectly, while the second file of the three has from 400-1200 (it includes the third file).

Is there a reason why it won't copy the correct segment?

Upvotes: 1

Views: 1265

Answers (1)

Gyan
Gyan

Reputation: 93329

The first and third files convert perfectly, while the second file of the three has from 400-1200 (it includes the third file).

-t is duration, not final timestamp. So, -ss 400 -t 800 tells ffmpeg to copy 800 seconds, starting from t=400. Use -to instead of -t.

Upvotes: 1

Related Questions