Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.07k stars 1.51k forks source link

Vertical MOV video to MP4 #2199

Open Duizhant opened 1 week ago

Duizhant commented 1 week ago

To convert a vertical MOV video took by iPhone to MP4, first try the following code:

from moviepy.editor import VideoFileClip

def convert_mov_to_mp4(input_file, output_file): try:

Load the MOV file

         video_clip = VideoFileClip(input_file)

         # Write the video to the output MP4 file
         video_clip.write_videofile(output_file, codec='libx264', audio_codec='aac')

         print(f"Successfully converted {input_file} to {output_file}")
   except Exception as e:
        print(f"Error occurred: {e}")

but the original video is 1080(W)x1920(H) vertical video, the output video turns to be 1930(W)x1080(W), the video ratio is distorted.

Anyway to keep the output MP4 video in vertical with same ratio?

harroopsra commented 6 days ago

Based on your function, it seems like you're not trying to change anything about the video. Consider looking at using ffmpeg (which you have installed already) if all you want to do is just convert the container from MOV to MP4. I've also heard good things about handbrake. This doesn't change anything about the video and maintains the size and quality.

Links for help: https://askubuntu.com/questions/396883/how-to-simply-convert-video-files-i-e-mkv-to-mp4 https://askubuntu.com/questions/50433/how-to-convert-mkv-file-into-mp4-file-losslessly https://superuser.com/questions/1155186/convert-mov-video-to-mp4-with-ffmpeg.

zengqiang24 commented 16 hours ago

I also got this issue too. Finally I chose FFMPEG command to address it instead of MoviePy.

Command: ffmpeg -an -i IMG_6859.MOV -stream_loop -1 -i res/audio/audio.wav -c:v copy -t 60 -y out.mp4

Here is my code : import subprocess

ffmpeg_command = [ 'ffmpeg', '-an', # 禁用音频输入 '-i', 'IMG_6859.MOV', # 输入视频文件 '-stream_loop', '-1', # 无限循环音频输入 '-i', 'res/audio/music.mp3', # 输入音频文件 '-c:v', 'copy', # 复制视频流,不进行重新编码 '-t', '60', # 设置输出时长为60秒 '-y', # 如果输出文件已存在,则覆盖它 'out.mp4' # 输出文件 ]

result = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

if result.returncode != 0: print("Error executing command:") print(result.stderr) else: print("Command executed successfully.")

print(result.stdout)

byscut commented 1 hour ago

It seems that your video has the rotate param but moviepy doesn't support it very well, I guess...:)