The final rendered gif is the ready to be hosted or transferred to your application:Īccessing your rendered videos using the dashboard If something goes wrong the status will show as failed. Re-run the status.py script every couple of seconds until the status is done and a URL is returned. Print ( f"Unable to resolve API call: with the ID returned from the mp4-to-gif.py script. The Shotstack SDK takes care of converting our objects to JSON, adding our key to the request header, and POSTing everything to the API. ) Sending the edit for rendering via the APIįinally, we send the edit for processing and rendering using the API. These setting help us optimize the file size and rendering time of the gif. We also use a very low frames per second (fps) value and low resolution. By setting repeat to True the gif will loop continuously. We set the output format to gif, which will convert the file from the mp4 format to an animated gif. We configure the output format and add the timeline and output to create an edit. Tracks on the timeline allow us to layer clips on top of each other.īelow we add our video_clip to the Track clips array, and then add our track to the array of tracks of the Timeline: track = Track (clips = ) Let's create a timeline, which contains a track, and the track will contain our video clip. The Shotstack API follows many of the principles of desktop editing software such as a timeline and tracks. For our clip we use the video_asset we just created, a start of 0 seconds and a length of 3 seconds: video_clip = Clip ( We can configure different properties like length (duration to play for) and start time (when on the timeline the clip will play from). A clip is container for different types of assets, including the VideoAsset. You can replace it with your own video url from any online source.Īdd the following code to create a VideoAsset using the video URL: video_asset = VideoAsset ( The video needs to be hosted online and accessible via a public or signed URL. We will convert the following Christian Bale American Psycho meme video hosted on Imgflip at the URL. EditApi (api_client ) Setting up the video clip ApiClient (configuration ) as api_client :Īpi_instance = edit_api. Configuration (host = host )Ĭonfiguration. If you want, you can hard code the API key here but we recommend environment variables. Next, add the following, which sets up the API client with the API URL and key, this should use the API key added to your environment variables. video_asset import VideoAsset Configuring the API client timeline import Timelineįrom shotstack_sdk. We need to import modules from the Shotstack SDK to edit and render our video plus a couple of built in modules: import shotstack_sdk as shotstackįrom shotstack_sdk. Let's import the required modules for the project. You can call it whatever you like, but for this tutorial we created a file called mp4-to-gif.py. Create a Python script to convert MP4 to GIFĬreate a file for the script in your favorite IDE or text editor. Replace your_key_here with your provided sandbox API key which is free for testing and development. Or, if using Windows (make sure to add the SHOTSTACK_KEY to the path): set SHOTSTACK_KEY =your_key_here Then, set your API key as an environment variable (Linux/Mac): export SHOTSTACK_KEY =your_key_here You may need to use pip3 depending on how your environment is configured. Otherwise follow the steps below to install dependencies and set up your API key.įirst of all, install the the Shotstack Python SDK from the command line: pip install shotstack_sdk If you want to skip ahead you can find the source code for this guide in our GitHub repository. You can sign up for a free developer account to get your Shotstack API key. We will also be using the Shotstack video editing Python SDK for this tutorial. Shotstack's infrastructure makes it possible to concurrently render multiple media files like video, audio, and GIFs in minutes. ![]() Rendering videos is resource intensive and it can take hours to edit and generate videos at scale. Shotstack provides a cloud based video editing API. Plus it's more fun and a good learning experience doing it programmatically. In this guide we'll show you how to convert an MP4 to GIF using Python so you can do all the awesome things mentioned above. But what if you want to convert MP4 files to GIF in bulk or build your own meme generator? How about automating the conversion process or integrating a GIF feature in your next application or workflow? Often used for memes and humour, GIF's have become a popular method of communication and cultural expression.Ĭonverting an MP4 to GIF is fairly easy with free tools and meme generators available online. Animated GIF's are a fun way to convert videos into light-weight, short visual media.
0 Comments
Leave a Reply. |