cjrasmussen / BlueskyApi

Simple helper for interacting with the Bluesky API/AT protocol
MIT License
16 stars 3 forks source link

How to put aspect ratio in video posting? #12

Closed arthurbass closed 1 month ago

arthurbass commented 1 month ago

How to set aspect ratio uploading a video?

here are my args:

$args = [ 'collection' => 'app.bsky.feed.post', 'repo' => $bluesky->getAccountDid(), 'record' => [ 'text' => 'Hello World!', 'createdAt' => date('c'), '$type' => 'app.bsky.feed.post', 'embed' => [ '$type' => 'app.bsky.embed.video', 'video' => $video_blob, ], ], ];

Tried this: https://atproto.blue/en/latest/atproto/atproto_client.models.app.bsky.embed.video.html https://atproto.blue/en/latest/atproto/atproto_client.models.app.bsky.embed.defs.html#atproto_client.models.app.bsky.embed.defs.AspectRatio

I tried so many things but nothing works.... using 1.34:1 format or an array with width and height but didn't work.

cjrasmussen commented 1 month ago

I haven't looked much into posting with video but the way I'm reading it is that the embed object needs an aspectRatio property which is, itself, an object with a width and height property. So the above example would look like this:

$args = [
    'collection' => 'app.bsky.feed.post',
    'repo' => $bluesky->getAccountDid(),
    'record' => [
        'text' => 'Hello World!',
        'createdAt' => date('c'),
        '$type' => 'app.bsky.feed.post',
        'embed' => [
            '$type' => 'app.bsky.embed.video',
            'video' => $video_blob,
            'aspectRatio' => [
                'width' => 1920,
                'height' => 1080,
            ],
        ],
    ],
];
cjrasmussen commented 1 month ago

It looks like what I noted above is accurate. I v2.3.0 of BlueskyApi supports setting request-specific API hosts and tokens, which are necessary for uploading video. It looks like this...

$body = file_get_contents($video_path);

// GET TOKEN FOR HALF HOUR
$args = [
    'aud' => 'did:web:' . $blueskyApi->getSessionHost(),
    'lxm' => 'com.atproto.repo.uploadBlob',
    'exp' => strtotime('+30 minutes'),
];
$response = $blueskyApi->request('GET', 'com.atproto.server.getServiceAuth', $args);

$response = $blueskyApi->request('POST', ('app.bsky.video.uploadVideo?did=' . urlencode($blueskyApi->getAccountDid()) . '&name=' . $video_file_name), [], $body, 'video/mp4', 'video.bsky.app', $response->token);

$video = null;

if ($job_id = $response->jobId) {
    $args = [
        'jobId' => $job_id,
    ];

    do {
        sleep(3);
        $response = $blueskyApi->request('GET', 'app.bsky.video.getJobStatus', $args, null, null, 'video.bsky.app');
    } while ($response->jobStatus->state !== 'JOB_STATE_COMPLETED');

    $video = $response->jobStatus->blob;
}

if ($video) {
    $args = [
        'collection' => 'app.bsky.feed.post',
        'repo' => $blueskyApi->getAccountDid(),
        'record' => [
            'text' => '#TestingInProduction',
            'langs' => ['en'],
            'createdAt' => date('c'),
            '$type' => 'app.bsky.feed.post',
            'embed' => [
                '$type' => 'app.bsky.embed.video',
                '19' => [
                    'width' => 1920,
                    'height' => 1080,
                ],
                'video' => $video,
            ],
        ],
    ];

    $response = $blueskyApi->request('POST', 'com.atproto.repo.createRecord', $args);
}

Where $video_path is the path to the video file and $video_file_name is the base name of that file. I sleep for three seconds while waiting for the video file to be processed but, obviously, that could be adjusted. I've also got a bunch of places where the API response is assigned to a variable but never used. Not saying it's perfect, just that it works.

arthurbass commented 2 weeks ago

Is it working for you??

Before I just changed the image to video mp4 and worked but it isn't working anymore. Tried this v2.3.0 but got nothing again.

cjrasmussen commented 2 weeks ago

I just re-ran the code above to confirm that it was still working and I was able to successfully include a video in a post.