Why does Moviepy stretch my output after cutting and putting back together a video?

Question:

I am using moviepy to cut up a video, put all the clips back together, and then export it. When I export it, the aspect ratio is changed, vertically stretching the image. I was using the ‘.mkv’ container, which I thought might have been the issue, but it’s happening with ‘.mp4’ files too. Is there an issue with my export options, or is it the file itself?

My code:

from moviepy.editor import *

input = "input.mp4"
timestamps = [[60,70],[80,100]]
output_destination = "test-output"
video_end = 120

video = VideoFileClip(input)

# MAKE CUTS
# all this does is create a list of pieces to cut out of the video, and then puts them in a list
initial_time = 0
clips = []
for a in timestamps:
    temp = video.subclip(initial_time, a[0])
    initial_time = a[1]
    clips.append(temp)
clips.append(video.subclip(initial_time, video_end))

# PUT TOGETHER
result = concatenate_videoclips(clips)

# EXPORT TO DESTINATION
result.write_videofile((output_destination+".mp4"),fps=29.97,codec='libx264',audio_bitrate='448k',preset='superfast',ffmpeg_params=['-crf','18'],write_logfile=True)

I wanted to briefly mention that I have tried resizing the video to 720×480 in the code, and I have also tried resizing it during import (as a parameter of VideoFileClip). I still get the streteched output. I have also tried just a pure re-encode to see if it’s the subclips that cause the problem, but I still get the stretched output.

My files:
https://drive.google.com/drive/folders/1dvFP6xPSqFMeC1VexIUIIkVJ-RApTbV6?usp=sharing

The two files called "input.###" are, well, the input files, and the two files labeled "test-output.###" are the outputs my program creates.

Asked By: limejellodragon

||

Answers:

I found that it’s possible to force a 16:9 aspect ratio by passing the aspect ratio inside ffmpeg_params:

ffmpeg_params=[..., '-aspect', '16:9']

The full call to write_videofile() then becomes this:

result.write_videofile((output_destination+".mp4"),fps=29.97,codec='libx264',audio_bitrate='448k',preset='superfast',ffmpeg_params=['-crf','18', '-aspect', '16:9'],write_logfile=True)

See also.

Answered By: Nick ODell
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.