Split screen is a method of displaying multiple videos side-by-side by dividing the screen into two sections, either vertically or horizontally. To create a splitscreen video in Python, you'll need a video editing API capable of processing video files. Here is how to accomplish that in Python code with just a few lines:
import requests
json = {
'source': {
'output_format': 'mp4',
'width': 1920,
'height': 1080,
'duration': 6,
'elements': [
{
'type': 'video',
'source': 'https://cdn.creatomate.com/demo/river.mp4',
'x': '25%',
'width': '50%'
},
{
'type': 'video',
'source': 'https://cdn.creatomate.com/demo/bridge.mp4',
'x': '75%',
'width': '50%'
}
]
}
}
response = requests.post(
'https://api.creatomate.com/v1/renders',
headers={
# Find your API key under 'Project Settings' in your account:
# https://creatomate.com/docs/api/rest-api/authentication
'Authorization': 'Bearer Your-API-Key',
'Content-Type': 'application/json',
},
json=json
)
# Wait a minute, then visit the URL provided in the response:
print(response.json())
How it works: The REST API can be accessed through Python's standard "requests" package, without the need to install any additional software. The output video is based on a composition we define in our Python code. Here's how it works.
First off, decide on the resolution you want for your final video. For our example, let's go with a landscape layout of 1080p (1920 by 1080) using two input videos.
To display both videos side-by-side, we'll make use of the "x" and "width" properties. These positions are based on the center of each video element. To position them on the left and right sides, set the positions to "25%" and "75%" respectively. To ensure equal horizontal distribution, set the width of each element to "50%".
We're only changing the horizontal properties ("x" and "width") and leaving the vertical properties ("y" and "height") as they are. By default, the "y" position and height of an element are set to "50%" and "100%" respectively, filling up the entire vertical space. Therefore, we do not need to specify these values for this example. However, if we wanted to offset the videos vertically, we could do so as well.
If you want to play around with different values, you can try using a video editor that allows building a video composition by dragging and dropping. Once you're satisfied with your video template, the editor will automatically generate the necessary Python code.