CodeGym /Courses /Python SELF EN /Final Export for Various Platforms

Final Export for Various Platforms

Python SELF EN
Level 48 , Lesson 4
Available

1. Video Export

Getting Ready for the Final Export

Welcome to the final lecture of our course dedicated to video editing with MoviePy. Congrats, you've made it through 99 steps on your journey to becoming a video automation master! Today, we'll learn one of the most important things—how to export your masterpieces so they can shine on all platforms, from web pages to social media.

It's time to bring all your ideas and work to life. Before exporting a video in the desired formats, let's figure out how to properly configure export settings so that your result looks professional and perfectly fits the chosen platform.

Setting Export Parameters

In MoviePy, it's pretty straightforward to set output parameters for a video. The main parameters to consider are:

  • File format: for example, MP4, GIF.
  • Resolution: This is how sharp your video will look. For instance, 1920x1080 for Full HD.
  • Bitrate: Affects video quality and size. Higher bitrate means better quality but larger size.
  • Frame rate: Often 24, 30, or 60 frames per second. More frames make for smoother video.

Let's take a look at how to set up these options in code.


from moviepy.editor import VideoFileClip

# Loading your final video
final_clip = VideoFileClip("your_video.mp4")

# Export the video with the specified parameters
final_clip.write_videofile(
    "exported_video.mp4",
    codec="libx264",  # Setting the codec
    bitrate="2000k",  # Setting the bitrate
    fps=24,           # Setting the frame rate
    preset="medium"   # Setting the encoding speed
)

The libx264 codec is widely used and supported on many platforms, including social media. Take note that codec and other parameters can be adjusted based on your needs.

2. Video Formats

Choosing a Format for Export

MoviePy supports various video formats including MP4, AVI, WEBM, and GIF. The MP4 format (H.264 codec) is one of the most popular for web and social media due to its high quality and relatively small file size.

The most popular formats are:

  • MP4: Widely used for web and social media due to its great balance between quality and size.
  • AVI: An older format, good for storing high-quality videos.
  • WEBM: Popular for online videos, optimized for web platforms.

Export in MP4 format


video.write_videofile("final_video.mp4", codec="libx264")

Here:

  • codec="libx264" tells MoviePy to use the H.264 codec, which is widely supported across platforms and offers good compression quality.

Export in WebM format for web platforms

WebM is a format optimized for the web environment, offering high compression and quality.


video.write_videofile("final_video.webm", codec="libvpx")

3. Adjusting Resolution and Frame Rate

Different platforms may require different resolutions and frame rates. For example, for YouTube and Instagram, it is recommended to use a resolution of at least 720p (1280x720), while for websites 480p (854x480) may suffice.

Changing Video Resolution


# Changing resolution to 720p
video_resized = video.resize((1280, 720))
video_resized.write_videofile("final_video_720p.mp4", codec="libx264")

Here:

  • resize((1280, 720)) changes the resolution of the video to 1280x720 pixels.

Setting Frame Rate

Frame rate (fps) is also important for the final video quality. Standard frame rates for web and social media are 24 or 30 fps.


video.write_videofile("final_video.mp4", fps=24)

4. Optimizing Video File Size

To reduce the video file size without significantly compromising quality, you can reduce the bitrate or use various compression methods.

Setting Bitrate to Reduce Size

Bitrate directly affects the size and quality of the video. Lowering the bitrate reduces video quality but also decreases file size.


video.write_videofile("final_video_low_bitrate.mp4", bitrate="500k")

Here:

  • bitrate="500k" sets the bitrate to 500 kbps. Bitrate values can be varied depending on quality and file size requirements.

Export with the preset Parameter for Compression

The H.264 codec offers a preset parameter that lets you choose the balance between quality and compression speed.


video.write_videofile("final_video_fast.mp4", codec="libx264", preset="fast")

Here:

  • preset="fast" means that compression will be quicker, but quality might slightly decrease. Available presets include ultrafast, fast, medium, slow, and veryslow.

5. Exporting Video for Social Media

Different social media platforms have specific requirements for videos, including resolution, format, and duration. Let's look at the optimal export settings for popular platforms.

YouTube

YouTube supports resolutions from 720p to 4K, and it's recommended to use the H.264 codec in MP4 format. Frame rates may vary, but the standard is 24 or 30 fps.


video.write_videofile("youtube_video.mp4", codec="libx264", fps=30, bitrate="5000k")

Instagram

For Instagram, it's best to use square or vertical videos with a resolution of 1080x1080 or 1080x1350 for the feed. Stories require 1080x1920.


# Export video for Instagram Stories
video_stories = video.resize((1080, 1920))
video_stories.write_videofile("instagram_stories.mp4", codec="libx264", fps=30)

Facebook

Facebook supports a resolution of 720p and higher, with a typical frame rate of 30 fps and MP4 format.


video.write_videofile("facebook_video.mp4", codec="libx264", fps=30, bitrate="2500k")

Tips for Optimizing Videos for Web and Social Media

  • Lowering resolution: If video quality isn’t critical, reducing resolution is one of the most effective ways to reduce file size.
  • Adjusting bitrate: Find the right bitrate to achieve an optimal balance of size and quality.
  • Choosing the right format: MP4 with the H.264 codec is the most universal and supported by almost all platforms.
  • Reducing frame rate: A frame rate of 24 fps works for most videos and can significantly reduce file size.
  • Using GIFs for short animations: GIFs work for short, looping animations but require optimization.

6. Exporting Video as GIF

MoviePy allows exporting videos as GIFs, which is especially useful for web platforms. However, GIF files can be very large, especially at high resolutions and durations. It's recommended to reduce size and frame rate for GIF optimization.

Export Video as GIF


video.write_gif("final_animation.gif", fps=10, optimize=True)

Here:

  • fps=10 reduces the frame rate, making the GIF less smooth but smaller in size.
  • optimize=True optimizes the GIF for minimal file size.

7. Exporting Large Projects

MoviePy allows you to display the export progress, which is useful for large projects.


video.write_videofile("final_video.mp4", progress_bar=True)

8. Recommendations for Optimization

In addition to all the settings mentioned above, don't forget to:

  • Lower the resolution: If the video is meant for mobile devices only.
  • Trim unnecessary scenes: To shorten the video duration.
  • Keep file backups: At each stage of editing, to avoid quality loss from re-encoding.

And there you have it! You've reached the finish line of our marathon. Now you're equipped with the knowledge to export and optimize videos for any platform, making your projects as accessible and impressive as possible. May your video masterpieces conquer the world (or at least get a few likes on social media)!

1
Task
Python SELF EN, level 48, lesson 4
Locked
Basic Video Export
Basic Video Export
2
Task
Python SELF EN, level 48, lesson 4
Locked
Export video with resolution change
Export video with resolution change
3
Task
Python SELF EN, level 48, lesson 4
Locked
Export video in WebM format
Export video in WebM format
4
Task
Python SELF EN, level 48, lesson 4
Locked
Export and Optimize GIF
Export and Optimize GIF
1
Survey/quiz
Creating Video Effects, level 48, lesson 4
Unavailable
Creating Video Effects
Creating Video Effects
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION