Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow urls with query values #1

Open
cavallium opened this issue Oct 1, 2020 · 1 comment
Open

Allow urls with query values #1

cavallium opened this issue Oct 1, 2020 · 1 comment

Comments

@cavallium
Copy link

Some kaltura videos have some obfuscated query values in the url, that are needed to download the segments.
It will be useful to make this software capable of splitting the url and appending the query to every segment.
Example:
https://cfvod.kaltura.com/scf/hls/p/2351962/sp/235196200/serveFlavor/entryId/1_28jtuk4w/v/1/ev/4/flavorId/1_zg05jfd7/name/a.mp4?Policy=eyJTdGXX0_&Signature=dS9g__&Key-Pair-Id=APKAJ

needs to be splitted in:
url="https://cfvod.kaltura.com/scf/hls/p/2351962/sp/235196200/serveFlavor/entryId/1_28jtuk4w/v/1/ev/4/flavorId/1_zg05jfd7/name/a.mp4"
query="?Policy=eyJTdGXX0_&Signature=dS9g__&Key-Pair-Id=APKAJ"

@oluvvafemi
Copy link

Here you go

import json
import os
import shutil
import argparse

import requests

temp_dir = "temp_dl"

def main(source, query_string, output_file):

    url = source + "/seg-71-v1-a1.ts?" + query_string

    response = requests.get(url)

    if response.status_code !=200:
        print("Query strings like Signature and Policy might have expired OR you don't have access to video")
        exit()

    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)

    count = 1
    fileList = []
    print("Downloading video segments")
    while response.status_code == 200:
        curr_seg = f"/seg-{count}-v1-a1.ts?"
        response = requests.get(source + curr_seg + query_string)
        with open(temp_dir + curr_seg, 'wb') as f:
            f.write(response.content)
        fileList.append(temp_dir + curr_seg)
        count += 1

    if os.path.getsize(fileList[-1]) < 1000:
        os.remove(fileList[-1])
        fileList = fileList[:-1]

    print("stiching video segments")
    with open(output_file, 'wb') as stitched:
        for filename in fileList:
            with open(os.path.join("", filename), 'rb') as part:
                shutil.copyfileobj(part, stitched)

    #cleanup downloaded files
    for filename in fileList:
        os.remove(filename)
    if len(os.listdir(temp_dir)) == 0:
        os.removedirs(temp_dir)

    print("Done - o ti pari")


if __name__ == "__main__":
    
     parser = argparse.ArgumentParser(description='Download and stitch private kaltura videos')
     parser.add_argument('-src', action='store', dest='src', default=None, help='URL ending in .../a.mp4')
     parser.add_argument('-qstr', action='store', dest='qstr', default=None, help="everything after the '.../seg-xx-v1-a1.ts?'" )
     parser.add_argument('-output', action='store', dest='output', default=None, help='Output file name')

     args = parser.parse_args()
     main(args.src, args.qstr, args.output)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants