Skip to content

Commit

Permalink
added live broadcast endpoints and example
Browse files Browse the repository at this point in the history
  • Loading branch information
capitalg committed Mar 18, 2018
1 parent b7c8d9c commit bd67f5f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
31 changes: 31 additions & 0 deletions InstagramAPI/InstagramAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,37 @@ def generateUUID(self, type):
def generateUploadId(self):
return str(calendar.timegm(datetime.utcnow().utctimetuple()))

def createBroadcast(self, previewWidth=1080, previewHeight=1920, broadcastMessage=''):
data = json.dumps({'_uuid': self.uuid,
'_uid': self.username_id,
'preview_height': previewHeight,
'preview_width': previewWidth,
'broadcast_message': broadcastMessage,
'broadcast_type': 'RTMP',
'internal_only': 0,
'_csrftoken': self.token})
return self.SendRequest('live/create/', self.generateSignature(data))

def startBroadcast(self, broadcastId, sendNotification=False):
data = json.dumps({'_uuid': self.uuid,
'_uid': self.username_id,
'should_send_notifications': int(sendNotification),
'_csrftoken': self.token})
return self.SendRequest('live/' + str(broadcastId) + '/start', self.generateSignature(data))

def stopBroadcast(self, broadcastId):
data = json.dumps({'_uuid': self.uuid,
'_uid': self.username_id,
'_csrftoken': self.token})
return self.SendRequest('live/' + str(broadcastId) + '/end_broadcast/', self.generateSignature(data))

def addBroadcastToLive(self, broadcastId):
# broadcast has to be ended first!
data = json.dumps({'_uuid': self.uuid,
'_uid': self.username_id,
'_csrftoken': self.token})
return self.SendRequest('live/' + str(broadcastId) + '/add_to_post_live/', self.generateSignature(data))

def buildBody(self, bodies, boundary):
body = u''
for b in bodies:
Expand Down
44 changes: 44 additions & 0 deletions examples/live_broadcast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Use text editor to edit the script and type in valid Instagram username/password

import subprocess

from InstagramAPI import InstagramAPI

USERNAME = ''
PASSWORD = ''
FILE_PATH = '/path/to/video/file'
PUBLISH_TO_LIVE_FEED = False
SEND_NOTIFICATIONS = False

api = InstagramAPI(USERNAME, PASSWORD, debug=False)
assert api.login()

# first you have to create a broadcast - you will receive a broadcast id and an upload url here
assert api.createBroadcast()
broadcast_id = api.LastJson['broadcast_id']
upload_url = api.LastJson['upload_url']

# we now start a boradcast - it will now appear in the live-feed of users
assert api.startBroadcast(broadcast_id, sendNotification=SEND_NOTIFICATIONS)

ffmpeg_cmd = "ffmpeg -rtbufsize 256M -re -i '{file}' -acodec libmp3lame -ar 44100 -b:a 128k -pix_fmt yuv420p -profile:v baseline -s 720x1280 -bufsize 6000k -vb 400k -maxrate 1500k -deinterlace -vcodec libx264 -preset veryfast -g 30 -r 30 -f flv '{stream_url}'".format(
file=FILE_PATH,
stream_url=upload_url.replace(':443', ':80', ).replace('rtmps://', 'rtmp://'),
)

print("Hit Ctrl+C to stop broadcast")
try:
subprocess.call(ffmpeg_cmd, shell=True)
except KeyboardInterrupt:
print('Stop Broadcasting')

assert api.stopBroadcast(broadcast_id)

print('Finished Broadcast')

if PUBLISH_TO_LIVE_FEED:
api.addBroadcastToLive(broadcast_id)
print('Added Broadcast to LiveFeed')

0 comments on commit bd67f5f

Please sign in to comment.