Hi. I am using Python’s Twitch library to download 60-second video clips from live twitch streams. My goal is to then save these clips as .mp4 files locally. This is my code:
client = TwitchClient(client_id='<client-id>')
streams_now = client.streams.get_live_streams(limit=100)
print(streams_now)
epoch = str(math.ceil(time.time()))
if not os.path.exists(current_dir + '/twitch_videos/'):
os.mkdir(current_dir + '/twitch_videos/')
counter = 0
for i in range(0, 100):
username = streams_now[i]['channel']['name']
id = streams_now[i]['id']
game = streams_now[i]['game']
game = game.translate(str.maketrans({':': '-', ' ': '-', "'": '', '!': '', '&': '_', '.': '', '+': '_'}))
streaming = streamlink.streams('http://twitch.tv/' + username)
stream = streams_now['Best'].url
twitch_stream = ffmpeg.input(stream)
twitch_stream = ffmpeg.filter(twitch_stream,
'fps',
fps=1,
round='up')
twitch_stream = ffmpeg.output(twitch_stream,
current_dir + '/twitch_videos/' + '%d_' + username + '_' + epoch + '.mp4',
sc_threshold='0',
g='60',
f='segment',
segment_time='600',
segment_format_options='movflags=+faststart',
reset_timestamps='1')
ffmpeg.run(twitch_stream)
But I wasn’t getting any downloads. This code did work before so I decided to look at what the API was giving me. This is what I saw from a single stream.
[{'id': 41521995070, 'game': 'Minecraft', 'broadcast_platform': 'live', 'community_id': '', 'community_ids': [], 'viewers': 65902, 'video_height': 1080, 'average_fps': 60, 'delay': 0, 'created_at': datetime.datetime(2021, 1, 30, 21, 44, 53), 'is_playlist': False, 'stream_type': 'live', 'preview': {'small': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_xqcow-80x45.jpg', 'medium': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_xqcow-320x180.jpg', 'large': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_xqcow-640x360.jpg', 'template': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_xqcow-{width}x{height}.jpg'}, 'channel': {'mature': False, 'status': 'GOING CRAZY AND DOING IT ALL - BE THERE', 'broadcaster_language': 'en', 'broadcaster_software': '', 'display_name': 'xQcOW', 'game': 'Minecraft', 'language': 'en', 'id': 71092938, 'name': 'xqcow', 'created_at': datetime.datetime(2014, 9, 12, 23, 50, 5, 989719), 'updated_at': datetime.datetime(2021, 1, 31, 1, 51, 35, 180819), 'partner': True, 'logo': 'https://static-cdn.jtvnw.net/jtv_user_pictures/xqcow-profile_image-9298dca608632101-300x300.jpeg', 'video_banner': 'https://static-cdn.jtvnw.net/jtv_user_pictures/a766d7ca-03cb-456e-9a6c-9d109eff47f1-channel_offline_image-1920x1080.png', 'profile_banner': 'https://static-cdn.jtvnw.net/jtv_user_pictures/83e86af1-9a6c-42b1-98e2-3f6238a744b5-profile_banner-480.png', 'profile_banner_background_color': '', 'url': 'https://www.twitch.tv/xqcow', 'views': 267341733, 'followers': 7529138, 'broadcaster_type': '', 'description': 'Overwatch Professional tank player and full time streamer. EZ Clap', 'private_video': False, 'privacy_options_enabled': False}},
The API is not returning a live stream URL (the one with the extension like m8…something). I realize that this is the reason why I was not able to download these 1-minute clips. Is there a reason why the API is doing this?