Skip to content

Commit bd67f5f

Browse files
author
capitalg
committed
added live broadcast endpoints and example
1 parent b7c8d9c commit bd67f5f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

InstagramAPI/InstagramAPI.py

+31
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,37 @@ def generateUUID(self, type):
823823
def generateUploadId(self):
824824
return str(calendar.timegm(datetime.utcnow().utctimetuple()))
825825

826+
def createBroadcast(self, previewWidth=1080, previewHeight=1920, broadcastMessage=''):
827+
data = json.dumps({'_uuid': self.uuid,
828+
'_uid': self.username_id,
829+
'preview_height': previewHeight,
830+
'preview_width': previewWidth,
831+
'broadcast_message': broadcastMessage,
832+
'broadcast_type': 'RTMP',
833+
'internal_only': 0,
834+
'_csrftoken': self.token})
835+
return self.SendRequest('live/create/', self.generateSignature(data))
836+
837+
def startBroadcast(self, broadcastId, sendNotification=False):
838+
data = json.dumps({'_uuid': self.uuid,
839+
'_uid': self.username_id,
840+
'should_send_notifications': int(sendNotification),
841+
'_csrftoken': self.token})
842+
return self.SendRequest('live/' + str(broadcastId) + '/start', self.generateSignature(data))
843+
844+
def stopBroadcast(self, broadcastId):
845+
data = json.dumps({'_uuid': self.uuid,
846+
'_uid': self.username_id,
847+
'_csrftoken': self.token})
848+
return self.SendRequest('live/' + str(broadcastId) + '/end_broadcast/', self.generateSignature(data))
849+
850+
def addBroadcastToLive(self, broadcastId):
851+
# broadcast has to be ended first!
852+
data = json.dumps({'_uuid': self.uuid,
853+
'_uid': self.username_id,
854+
'_csrftoken': self.token})
855+
return self.SendRequest('live/' + str(broadcastId) + '/add_to_post_live/', self.generateSignature(data))
856+
826857
def buildBody(self, bodies, boundary):
827858
body = u''
828859
for b in bodies:

examples/live_broadcast.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Use text editor to edit the script and type in valid Instagram username/password
5+
6+
import subprocess
7+
8+
from InstagramAPI import InstagramAPI
9+
10+
USERNAME = ''
11+
PASSWORD = ''
12+
FILE_PATH = '/path/to/video/file'
13+
PUBLISH_TO_LIVE_FEED = False
14+
SEND_NOTIFICATIONS = False
15+
16+
api = InstagramAPI(USERNAME, PASSWORD, debug=False)
17+
assert api.login()
18+
19+
# first you have to create a broadcast - you will receive a broadcast id and an upload url here
20+
assert api.createBroadcast()
21+
broadcast_id = api.LastJson['broadcast_id']
22+
upload_url = api.LastJson['upload_url']
23+
24+
# we now start a boradcast - it will now appear in the live-feed of users
25+
assert api.startBroadcast(broadcast_id, sendNotification=SEND_NOTIFICATIONS)
26+
27+
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(
28+
file=FILE_PATH,
29+
stream_url=upload_url.replace(':443', ':80', ).replace('rtmps://', 'rtmp://'),
30+
)
31+
32+
print("Hit Ctrl+C to stop broadcast")
33+
try:
34+
subprocess.call(ffmpeg_cmd, shell=True)
35+
except KeyboardInterrupt:
36+
print('Stop Broadcasting')
37+
38+
assert api.stopBroadcast(broadcast_id)
39+
40+
print('Finished Broadcast')
41+
42+
if PUBLISH_TO_LIVE_FEED:
43+
api.addBroadcastToLive(broadcast_id)
44+
print('Added Broadcast to LiveFeed')

0 commit comments

Comments
 (0)