Skip to content

Instantly share code, notes, and snippets.

@brndnblck
Last active September 14, 2017 15:58
Show Gist options
  • Save brndnblck/bb3feaff9f0a2cad2d74 to your computer and use it in GitHub Desktop.
Save brndnblck/bb3feaff9f0a2cad2d74 to your computer and use it in GitHub Desktop.
Script for Resumable Media Uploads to Twitter
function video-upload() {
if [ $# -lt 1 ]; then
echo "[ERROR] Missing required file name."
else
FILESIZE=$(wc -c "$1" | awk '{print $1}')
printf "[START] Uploading $FILESIZE bytes.\n"
MEDIAID=$(twurl /1.1/media/upload.json -H upload.twitter.com -d "command=INIT&media_category=amplify_video&media_type=video/mp4&total_bytes=$FILESIZE" | jq .media_id_string | sed 's/\"//g')
INDEX=0
split -b 5m $1 twitter-video-
for FILE in twitter-video-*; do
echo "[INFO] Uploading segment $INDEX ($FILE)..."
twurl "/1.1/media/upload.json" -H upload.twitter.com -d "command=APPEND&segment_index=$INDEX&media_id=$MEDIAID" --file-field "media" --file "$FILE"
INDEX=$((INDEX + 1))
done
rm twitter-video-*
twurl "/1.1/media/upload.json" -H upload.twitter.com -d "command=FINALIZE&media_id=$MEDIAID" && printf "\n"
printf "[DONE] $MEDIAID"
fi
}
@brndnblck
Copy link
Author

Example Usage:

~ ❯ video-upload /path/to/video.mp4
[START] Uploading 7015328 bytes.
[INFO] Uploading segment 0 (twitter-video-aa)...
[INFO] Uploading segment 1 (twitter-video-ab)...
[INFO] Uploading segment 2 (twitter-video-ac)...
{"media_id":643937461538652162,"media_id_string":"643937461538652162","size":7015328,"expires_after_secs":86400,"video":{"video_type":"video\/mp4"}}
[DONE] 643937461538652162

@VitaliyKulikov
Copy link

@brandonblack awesome. thanks )

@brndnblck
Copy link
Author

Moved to wc -c instead of stat. Thanks @TonyNa!

@andypiper
Copy link

Note that media_category=amplify_video is only valid for promoted video with an Ads account. media_category=tweet_video should be used in the majority of media upload use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment