I work for an influencer marketing and data company, Speakr, and we work with social media APIs all the time. This can be challenging, to put it lightly. One particularly difficult task has been posting videos to Twitter. If you search the internet, it seems we are all trying to figure this out and there is no clear answer. In my opinion, the best Twitter package for Node.js is Twit, but uploading videos using Twit is not explained well in the docs. Good news for you: I have gone through the pain and frustration so you do not have to.
Here is how I upload videos to Twitter using Node.js (version 6 or greater) and Twit (version 2.2.5). Below, I provide two solutions, both of which use Twit: uploading to Twitter when you have the video saved to disk already, and uploading a video when you need to first retrieve the video from remote storage, like an AWS S3 bucket.
Video format warning
Make sure the video is the correct format. Smartphone videos are often a safe bet, but they do not always work. The requirements are outlined in the infamously unhelpful Twitter media documentation.
Formatting a video for testing purposes
Here is a great trick I learned from my web searches:
- Open your video in QuickTime.
- From the File menu, select Export.
- Choose the iPad, iPhone, iPod Touch export option.
- Choose any iPhone format.
- Once it is converted, change the file type to .mp4 by renaming the file.
Twitter video upload: three steps
Uploading a video to Twitter involves three steps:
- POST the video.
- Using the data returned from step one, POST the returned metadata.
- POST the status, meaning the actual tweet.
I have not included my error handling, because it is specific to the app I am working on. Be sure to include error handling for all three steps, and also for retrieving and reading the file if you are fetching it from remote storage.
A practical sequence
- Install the necessary packages. To upload videos using Node.js, install the Node.js package manager, npm. Then install a file upload package such as Multer, and a streaming package such as m3u8.
- Create a server. Create a server using Node.js and Express. This will be the starting point of your application.
- Create a route for the file upload. This route should accept an uploaded file and save it to a specific location.
- Create a file upload form. Create an HTML form that allows users to select and upload a video file. This form should use the route you created in step three.
- Process the uploaded video file. Once the file has been uploaded to the server, process it so that it can be streamed. This can be done with the m3u8 package. Convert the file into an HLS stream, which is a standard streaming format.
- Stream. Serve the stream to the client and confirm playback before the tweet is posted.
Node.js is an open source server side platform for building web applications. It is a powerful and efficient language that is well suited to developing video uploading applications. To upload videos using Node.js, you will need to use a library such as Multer or Formidable. These libraries provide useful methods that allow you to upload files from a form or directly from a browser. Additionally, you can use streaming APIs such as Streams to upload videos directly to a remote server.
Once you have the library set up, you can start programming the upload process. You will need to create an endpoint in your server that will accept the video file and save it to a local directory or to a cloud service such as Amazon S3. After the video is uploaded, you can use an API to store its metadata such as title, description, and tags. Finally, you can use a library such as FFmpeg or Video.js to encode the video in a web ready format such as MP4 and store it on a remote server. By following these steps, you can easily create a video uploading application with Node.js.
References and further reading
Documentation for the tools named in this note.
- Twit for the Twitter client.
- Twitter API documentation for media upload rules.
- Node.js for the runtime.
- Multer for multipart file uploads.
- Formidable for an alternative upload parser.
- m3u8 for playlist generation.
- Express for the server layer.
- Node streams for handing off large files.
- HTTP Live Streaming for the HLS format.
- FFmpeg for encoding and conversion.
- Video.js for playback in the browser.