Httpstatus - '400' & error - 'InvalidContent.' on chunked video upload

video

#1

I used a the gracious PHP package from the codebird github repo. I love it so far but I’ve run into a problem on the “FINALIZE” step of the chunked video upload. It’s a 400 httpstatus with an “InvalidContent.” error code. Here is all my code and I’ll output my debugging output afterwards.

`function uploadVideo_twitter($file_location) {
	$file       = $file_location;
	$size_bytes = filesize($file);
	$this->debug($size_bytes, "file size");
	$fp         = fopen($file, 'r');
	// INIT the upload
	$reply = $this->cb->media_upload([
		'command'     => 'INIT',
		'media_type'  => 'video/mp4',
		'total_bytes' => $size_bytes
		]);
	$this->debug($reply, "INIT");
	$media_id = $reply->media_id_string;
	$this->debug($media_id, "media_id");
	// APPEND data to the upload
	$segment_id = 0;
	while (! feof($fp)) {
		$chunk = fread($fp, 2000000); // 2MB per chunk for this sample
		$reply = $this->cb->media_upload([
			'command'       => 'APPEND',
			'media_id'      => $media_id,
			'segment_index' => $segment_id,
			'media'         => $chunk
			]);
		$this->debug($reply, "APPEND segment # $segment_id");
		$segment_id++;
	}
	fclose($fp);
	// FINALIZE the upload
	$reply = $this->cb->media_upload([
		'command'       => 'FINALIZE',
		'media_id'      => $media_id
		]);
	$this->debug($reply, "FINALIZE");
	if ($reply->httpstatus < 200 || $reply->httpstatus > 299) {
		die();
	}
	// Now use the media_id in a Tweet
	$reply = $this->cb->statuses_update([
		'status'    => 'Twitter now accepts video uploads.',
		'media_ids' => $media_id
		]);
}

`

The “INIT” and “APPEND” methods work and the filesize I’m uploading is right around 1mb and an mp4 video so theoretically all should be well. I downloaded the sample video I’m using here: http://www.sample-videos.com/video/mp4/360/big_buck_bunny_360p_1mb.mp4

Here is my debugging output

`    file size
1057551

INIT
stdClass Object
(
    [media_id] => 752947029504958464
    [media_id_string] => 752947029504958464
    [expires_after_secs] => 86400
    [httpstatus] => 202
    [rate] => stdClass Object
        (
            [limit] => 200
            [remaining] => 186
            [reset] => 1468352373
        )

)

media_id
752947029504958464

APPEND segment # 0
stdClass Object
(
    [httpstatus] => 204
    [rate] => stdClass Object
        (
            [limit] => 20000
            [remaining] => 19967
            [reset] => 1468352374
        )

)

FINALIZE
stdClass Object
(
    [request] => /1.1/media/upload.json
    [error] => InvalidContent.
    [httpstatus] => 400
    [rate] => stdClass Object
        (
            [limit] => 615
            [remaining] => 601
            [reset] => 1468352375
        )

)

`
and just to give you an idea of my debug function so that you can see how the output is formatted:

`  	private function debug($var, $msg = "") {
	if ($msg != "") {
		echo "<br />$msg<br />";
	}
	echo "<pre>";
	print_r($var);
	echo "</pre>";
}`

Any more information I can put forth to help solve this mystery, will be gladly provided.


#2

I’ve found the answer to my problem. The code wasn’t the problem but the format of the MP4 I was attempting to use as my sample video was the problem.

The information on that and the answer to that problem can be found here:


#3

I have tried uploading a video, init and append commands were working fine where as while trying to call ‘finalize’ i am getting “invalid content” error message.

{"error":"InvalidContent.","request":"/1.1/media/upload.json"}

I have read the doc and used media_category as ‘tweet_video’ which should solve the problem but i am still facing the issue. Kindly help me to come out of this.


#4

Hi,
It worked now as i tried another mp4 file but in twitter its showing like “The media could not be played” . Can’t we use mp4 format of video??

Thanks,
Krishna


#5

Yes, you can, but make sure that the specific details of your MP4 format conform to the details Twitter expects.