Last active
February 15, 2019 11:06
-
-
Save grambas/0e1ea596ca4ea7f66178e9a9c610030e to your computer and use it in GitHub Desktop.
[ffmpeg] Recodring, streaming, ripping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! python | |
# Record stream with python from m3u8 file in 1h parts | |
import time | |
import subprocess as sp, sys | |
import urllib | |
def record(): | |
sourcePath ='https://url.m3u8' | |
name = '_video.ts' | |
destPath = time.strftime("%Y-%m-%d_%H-%M-%S", time.gmtime()) + name | |
command = [ 'ffmpeg', | |
'-y', # (optional) overwrite output file if it exists | |
'-hide_banner', | |
'-loglevel', 'panic', | |
'-i', | |
sourcePath, | |
'-vcodec', 'copy', | |
'-acodec', 'copy', | |
'-ss', '00:00:00.0', | |
'-t', '01:00:00.0', | |
# '-profile:v','high', | |
# '-preset','slow', | |
# '-threads', '2', | |
destPath] | |
returnExitCode = sp.call(command) | |
if returnExitCode != 0: | |
url_status = urllib.urlopen(sourcePath).getcode() | |
if url_status != 200: | |
print "Bad url response = " + str(url_status) + " Check source url" | |
else: | |
print "Error in ffmpeg" | |
print(returnExitCode) | |
print "Waiting 10 seconds" | |
time.sleep(10) | |
else: | |
print "ALL OK" | |
def main(argv): | |
print "Program started..." | |
while True: | |
record() | |
if __name__ == "__main__": | |
main(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Concert .ts files to .mp4 | |
# | |
for old_name in `find $PWD -type f -name '*.ts' | xargs ls -1`; | |
do | |
new_name=${old_name/%.ts/.mp4} | |
echo "#####################################################################################" | |
echo "Grabbing $old_name" | |
echo "Converting..." | |
ffmpeg -i "$old_name" -tune fastdecode -tune zerolatency -threads 0 -y -vcodec copy -acodec copy -bsf:a aac_adtstoasc -preset ultrafast "$new_name"; | |
#ffmpeg -i "$old_name" -vf scale=960:540 -c:v libx264 -preset:v ultrafast -maxrate 10M -bufsize 10M -f mpegts "$new_name"; | |
echo "Successfull converted!" | |
echo "New file: $new_name" | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ! python | |
import os, sys, urllib, time, signal, multiprocessing | |
from time import gmtime, strftime | |
from subprocess import Popen, PIPE | |
import httplib2 | |
import json | |
import requests | |
TWITCH_VERSION_HEADER = "application/vnd.twitchtv.v5+json" | |
CHUNKSIZE = 10 * 1024 * 1024 # 10MB chunk size for video parts | |
TOKEN = "XXXXXXXXXXXXXXXXXXXX" | |
TWITCH_CLIENT_ID = "XXXXXXXXXXXXXXXXXXXX" | |
USER_ID = "123456" | |
def get_channel_name(twitch_auth_token): | |
url = 'https://api.twitch.tv/kraken/user' | |
headers = {'Authorization': 'OAuth ' + twitch_auth_token, | |
'Accept': TWITCH_VERSION_HEADER, | |
'Client-ID': TWITCH_CLIENT_ID } | |
r = requests.get(url, headers=headers) | |
user = r.json() | |
#print user | |
return user['name'] | |
def create_twitch_video(title, description, tags, twitch_auth_token): | |
print ('Creating video on Twitch... with title' + str(title)) | |
url = 'https://api.twitch.tv/kraken/videos' | |
payload = {'channel_name': get_channel_name(twitch_auth_token), | |
'channel_id': USER_ID, | |
'title': title, | |
'description': description, | |
'tags': tags } | |
headers = {'Authorization': 'OAuth ' + twitch_auth_token, | |
'Accept': TWITCH_VERSION_HEADER, | |
'Client-ID': TWITCH_CLIENT_ID, | |
'channel_id': USER_ID, | |
'Content-Type': 'application/json' } | |
r = requests.post(url, json=payload, headers=headers) | |
video = r.json() | |
print (video) | |
return video["upload"]["url"], video["upload"]["token"] | |
def upload_to_twitch(filename, upload_url, upload_token): | |
print(filename) | |
print(upload_url) | |
print(upload_token) | |
print ("Uploading " + filename + " to Twitch via " + upload_url + ", " + str(CHUNKSIZE) + " bytes at a time..." ) | |
file = open(filename, 'rb') | |
index = 0 | |
while 1: | |
chunk = file.read(CHUNKSIZE) | |
if not chunk: break | |
index += 1 | |
headers = {'Accept': TWITCH_VERSION_HEADER, | |
'Client-ID': TWITCH_CLIENT_ID, | |
'Content-Length': str(len(chunk)) } | |
params = {'part': index, | |
'upload_token': upload_token } | |
r = requests.put(upload_url, params=params, data=chunk, headers=headers) | |
print ('Completed uploading part ' + str(index)) | |
file.close() | |
headers = {'Accept': TWITCH_VERSION_HEADER, | |
'Client-ID': TWITCH_CLIENT_ID } | |
params = {'upload_token': upload_token } | |
r = requests.post(upload_url + '/complete', params=params, headers=headers) | |
print ("DONE!!!!!!!!!") | |
return | |
def main(argv): | |
upload_url, upload_token = create_twitch_video(sys.argv[1], "XXXXXXXXX", "XXXXXXXXXXXXX", TOKEN) | |
upload_to_twitch(sys.argv[2], upload_url, upload_token) | |
#os.remove(sys.argv[2]) | |
if __name__ == "__main__": | |
main(sys.argv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment