Uploading Videos with php2Bluesky

Ever since I launched php2Bluesky a couple of years ago, I have wanted to add the ability to upload video as well as images. Initially, that wasn’t possible as Bluesky didn’t support videos, and then they did, but it wasn’t in the API. Now, however, it is all systems go, and so I have updated the library to support videos.

How to Upload a Video

First things first. In order to start uploading videos all you need to do is update the library. The way you call the library remains exactly the same, so you would do the following to upload a single video:

// connect to Bluesky API
$connection = $php2Bluesky->bluesky_connect($handle, $password);

// send to Bluesky
$response = $php2Bluesky->post_to_bluesky($connection, "This is the text", $media = "/location/of/video.mp4");

Requirements

There are a few limitations that are in place, by Bluesky rather than myself, regarding the uploading of media, both video and images. For example, only the following file types are supported:

  • image/jpeg
  • image/png
  • image/gif
  • image/jpg
  • video/mp4
  • video/mpeg
  • video/webm
  • video/mov

And the following will apply and, where appropriate, be upheld by php2Bluesky:

  1. you can only upload one video per post and you cannot mix images and videos (if you try the videos will be silently dropped)
  2. you can upload gif files but only the first frame will be displayed
  3. a video can only be up to 60 seconds in length
  4. only local videos can be uploaded.

The file BlueskyConsts.php has been updated to include references to these limits so that they can be easily changed in future if necessary.

Aspect Ratio

Like with images, it is possible to send to Bluesky along with any video the aspect ratio of such a file. However, unlike images where there is a built-in command, getimagesize, that returns the width and height of the image, there is not an equivalent for videos.

Why is this important? Well take a look at the side-by-side screenshots below. The one on the left has been sent without an aspect ratio and Bluesky has dealt with this by reducing the video size and adding white bars to either side. However, on the right the video has been uploaded with an aspect ratio and fills the whole frame.

A Partial Solution

If you use the code as delivered it will work and any videos uploaded will be sent to Bluesky without any aspect ratio and you will get it displayed as above left. If you want to avoid this then you need to make use of a small piece of code called ffprobe which is part of FFmpeg.

As this is not part of php and is an executable file it does require the correct permissions that allow escapeshellcmd to run an executable. I will leave you to work out what that might be for your system!

Download the correct version of ffprobe for your system and drop it in the same location as php2Bluesky.php. Now when you upload a video ffprobe will be called and this returns the necessary information which is passed on to Bluesky.

Error codes

While I was making the change to the code to add video support, I also tweaked the error handling slightly so that now not only is an error description returned with an error thrown, but also an error code. The table below shows what codes go with which error. I hope that having these consistent codes might help with trapping error.s

MessageCode
BLANK specified for fallback image but blank.png is missing. 1001
Could not determine mime type of file. 1002
File type not supported: . $mime 1003
Could not get the size of the image. 1004
Provided text greater than . BlueskyConsts::MAX_POST_SIZE 1005
Error loading url . $url 1006
No suitable image found for link card. 1007
ffprobe is not available. Please install. 1008
Video duration exceeds maximum allowed duration of . BlueskyConsts::MAX_VIDEO_DURATION . ” seconds.” 1009

Download the latest version, with video support, from Github.

Leave a Reply

Your email address will not be published. Required fields are marked *