Die Gesamtanzahl der Chunks ist ungültig, wenn Videos zur Veröffentlichung auf Tiktok mithilfe der Tiktok-API in Laravel
Posted: 20 Jan 2025, 16:11
Beim Veröffentlichen kleiner Videos mit 3 MB oder 2 MB funktioniert es einwandfrei, aber wenn ich zu größeren Videos wie 28 MB gehe, wird mir dieser Fehler angezeigt: Die Gesamtanzahl der Blöcke ist ungültig
So berechnen Sie die Blockgröße eines Videos, das ich in public/videos gespeichert habe
und das ist vollständiger Code
Ich habe versucht, die Variable $totalChunks so zu ändern, dass sie statisch auf 1 ist, aber es wird mir ein anderer Fehler angezeigt
Code: Select all
{
"success": false,
"error": "Failed to upload video: Client error: `POST https://open.tiktokapis.com/v2/post/publish/video/init/` resulted in a `400 Bad Request` response:\n{\"error\":{\"code\":\"invalid_params\",\"message\":\"The total chunk count is invalid\",\"log_id\":\"2025012015022992B6C5AD85BB021DC (truncated...)\n",
"publish_id": null
}
Code: Select all
// Get video from public folder
$videoPath = public_path('videos/xyz.mp4');
if (!file_exists($videoPath)) {
return response()->json([
'success' => false,
'error' => 'Video file not found in public/videos folder'
], 404);
}
// Calculate video parameters with TikTok specs
$videoSize = (int)filesize($videoPath);
$minChunkSize = 5 * 1024 * 1024; // 5MB minimum
$maxChunkSize = 64 * 1024 * 1024; // 64MB maximum
// Handle small videos ( 300]);
// Initialize video upload
$initResponse = $client->post('https://open.tiktokapis.com/v2/post/publish/video/init/', [
'headers' => [
'Authorization' => 'Bearer ' . $usertiktok->access_token,
'Content-Type' => 'application/json'
],
'json' => [
'source_info' => [
'source' => 'FILE_UPLOAD',
'video_size' => $videoSize,
'chunk_size' => $chunkSize,
'total_chunk_count' => $totalChunks
],
'post_info' => [
'title' => $request->title,
'description' => $request->description ?? '',
'privacy_level' => $request->privacy_level,
'disable_duet' => false,
'disable_comment' => false,
'disable_stitch' => false
]
]
]);
Code: Select all
public function uploadVideo(Request $request)
{
$account_id = auth()->user()->account_id;
$user_id = $request->user_id;
// Get video from public folder
$videoPath = public_path('videos/xyz.mp4');
if (!file_exists($videoPath)) {
return response()->json([
'success' => false,
'error' => 'Video file not found in public/videos folder'
], 404);
}
// Calculate video parameters with TikTok specs
$videoSize = (int)filesize($videoPath);
$minChunkSize = 5 * 1024 * 1024; // 5MB minimum
$maxChunkSize = 64 * 1024 * 1024; // 64MB maximum
// Handle small videos ( 4 * 1024 * 1024 * 1024) { // 4GB max
return response()->json([
'success' => false,
'error' => 'Video size must be less than 4GB'
], 400);
}
// Validate chunk count
if ($totalChunks > 1000) {
return response()->json([
'success' => false,
'error' => 'Invalid chunk count. Must be maximum 1000'
], 400);
}
// Validate request
$request->validate([
'title' => 'required|string|max:150',
'description' => 'nullable|string|max:2200',
'privacy_level' => 'required|in:PUBLIC_TO_EVERYONE,MUTUAL_FOLLOW_FRIENDS,SELF_ONLY',
]);
$usertiktok = User::where("id", $user_id)
->where("account_id", $account_id)
->where("social_type", "tiktok")
->first();
if (!$usertiktok) {
return response()->json(["message" => "TikTok account not found"], 400);
}
try {
$client = new Client(['timeout' => 300]);
// Initialize video upload
$initResponse = $client->post('https://open.tiktokapis.com/v2/post/publish/video/init/', [
'headers' => [
'Authorization' => 'Bearer ' . $usertiktok->access_token,
'Content-Type' => 'application/json'
],
'json' => [
'source_info' => [
'source' => 'FILE_UPLOAD',
'video_size' => $videoSize,
'chunk_size' => $chunkSize,
'total_chunk_count' => $totalChunks
],
'post_info' => [
'title' => $request->title,
'description' => $request->description ?? '',
'privacy_level' => $request->privacy_level,
'disable_duet' => false,
'disable_comment' => false,
'disable_stitch' => false
]
]
]);
$initData = json_decode($initResponse->getBody(), true);
if (!isset($initData['data']['upload_url'])) {
throw new \Exception('Failed to get upload URL: ' . json_encode($initData));
}
$uploadUrl = $initData['data']['upload_url'];
$publishId = $initData['data']['publish_id'];
// Upload video chunks
$videoFile = null;
try {
$videoFile = fopen($videoPath, 'r');
if (!$videoFile) {
throw new \Exception('Failed to open video file');
}
for ($chunk = 0; $chunk < $totalChunks; $chunk++) {
$offset = $chunk * $chunkSize;
$currentChunkSize = ($chunk == $totalChunks - 1)
? ($videoSize - $offset)
: $chunkSize;
fseek($videoFile, $offset);
$chunkData = fread($videoFile, $currentChunkSize);
$response = $client->put($uploadUrl, [
'headers' => [
'Authorization' => 'Bearer ' . $usertiktok->access_token,
'Content-Type' => 'video/mp4',
'Content-Length' => $currentChunkSize,
'Content-Range' => sprintf('bytes %d-%d/%d',
$offset,
$offset + $currentChunkSize - 1,
$videoSize
)
],
'body' => $chunkData
]);
// Check response codes (206 for partial, 201 for complete)
$statusCode = $response->getStatusCode();
if ($statusCode !== ($chunk == $totalChunks - 1 ? 201 : 206)) {
throw new \Exception("Unexpected response code: $statusCode");
}
}
} finally {
if (is_resource($videoFile)) {
fclose($videoFile);
}
}
// Check upload status
$maxAttempts = 30;
$attempts = 0;
$status = 'PROCESSING';
while ($status === 'PROCESSING' && $attempts < $maxAttempts) {
$statusResponse = $client->post('https://open.tiktokapis.com/v2/post/publish/status/fetch/', [
'headers' => [
'Authorization' => 'Bearer ' . $usertiktok->access_token,
'Content-Type' => 'application/json'
],
'json' => [
'publish_id' => $publishId
]
]);
$statusData = json_decode($statusResponse->getBody(), true);
$status = $statusData['data']['status'] ?? 'PROCESSING';
if ($status === 'PROCESSING') {
sleep(2);
$attempts++;
}
}
if ($status === 'FAILED') {
throw new \Exception('Upload failed during processing');
}
return response()->json([
'success' => true,
'message' => 'Video uploaded successfully',
'data' => [
'publish_id' => $publishId,
'status' => $status,
'video_size' => $videoSize,
'chunks' => $totalChunks
]
]);
} catch (\Exception $e) {
Log::error('TikTok Upload Error', [
'error' => $e->getMessage(),
'user_id' => $user_id,
'video_size' => $videoSize ?? 0,
'chunks' => $totalChunks ?? 0,
'publish_id' => $publishId ?? null
]);
return response()->json([
'success' => false,
'error' => 'Failed to upload video: ' . $e->getMessage(),
'publish_id' => $publishId ?? null
], 500);
}
}
Code: Select all
{
"success": false,
"error": "Failed to upload video: Client error: `POST https://open.tiktokapis.com/v2/post/publish/video/init/` resulted in a `400 Bad Request` response:\n{\"error\":{\"code\":\"invalid_params\",\"message\":\"The chunk size is invalid\",\"log_id\":\"20250120150836CF3EDDD9FB5BFC1C0A85\"}}\n",
"publish_id": null
}