def process_stream(stream):
    # stream = "dash/video.mp4/index.mpd" or "hls/video.mp4/index.m3u8"
    print("process stream: " + stream, flush=True)

    stream_name = stream.split("/")[1]

    if not isfile(ARCHIVE_ROOT + "/" + stream_name):
        print("process aborted for stream name: " + stream_name, flush=True)
        return

    zk = ZKState("/content_provider_transcoder/" + ARCHIVE_ROOT + "/" + stream)
    # "/content_provider_transcoder/" + ARCHIVE_ROOT + "/" + stream
    # => "/content_provider_transcoder//var/www/archive/dash/video.mp4/index.mpd"
    # or "/content_provider_transcoder//var/www/archive/hls/video.mp4/index.m3u8"
    if zk.processed():
        print("process already done", flush=True)
        zk.close()
        return

    if stream.endswith(".mpd"):
        print("it's a dash process", flush=True)
        try:
            mkdir(DASH_ROOT + "/" + stream_name)
        except:
            pass

        if zk.process_start():
            try:
                cmd = GetABRCommand(ARCHIVE_ROOT + "/" + stream_name,
                                    DASH_ROOT + "/" + stream_name, "dash")
                r = call(cmd)
                if r:
                    raise Exception("status code: " + str(r))
                zk.process_end()
            except:
                print(traceback.format_exc(), flush=True)
                zk.process_abort()

    if stream.endswith(".m3u8"):
        print("it's a hls process", flush=True)
        try:
            mkdir(HLS_ROOT + "/" + stream_name)
        except:
            pass

        if zk.process_start():
            try:
                cmd = GetABRCommand(ARCHIVE_ROOT + "/" + stream_name,
                                    HLS_ROOT + "/" + stream_name, "hls")
                r = call(cmd)
                if r:
                    raise Exception("status code: " + str(r))
                zk.process_end()
            except:
                print(traceback.format_exc(), flush=True)
                zk.process_abort()

    zk.close()
def process_stream(stream):
    stream_name = stream.split("/")[1]

    if not isfile(ARCHIVE_ROOT + "/" + stream_name):
        return

    zk = ZKState("/content_provider_transcoder/" + ARCHIVE_ROOT + "/" + stream)
    if zk.processed():
        zk.close()
        return

    if stream.endswith(".mpd"):
        try:
            mkdir(DASH_ROOT + "/" + stream_name)
        except:
            pass

        if zk.process_start():
            try:
                cmd = GetABRCommand(ARCHIVE_ROOT + "/" + stream_name,
                                    DASH_ROOT + "/" + stream_name, "dash")
                r = call(cmd)
                if r:
                    raise Exception("status code: " + str(r))
                zk.process_end()
            except:
                print(traceback.format_exc(), flush=True)
                zk.process_abort()

    if stream.endswith(".m3u8"):
        try:
            mkdir(HLS_ROOT + "/" + stream_name)
        except:
            pass

        if zk.process_start():
            try:
                cmd = GetABRCommand(ARCHIVE_ROOT + "/" + stream_name,
                                    HLS_ROOT + "/" + stream_name, "hls")
                r = call(cmd)
                if r:
                    raise Exception("status code: " + str(r))
                zk.process_end()
            except:
                print(traceback.format_exc(), flush=True)
                zk.process_abort()

    zk.close()
예제 #3
0
def ADTranscode(kafkamsg, db):
    zk = None

    msg = KafkaMsgParser(kafkamsg)
    # add zk state for each resolution file if we generate the ad clip each time for one solution
    zk = ZKState(msg.target_path, msg.target_name)

    if zk.processed():
        print("AD transcoding finish the clip :", msg.target, flush=True)
        zk.close()
        return

    if zk.process_start():
        try:
            print("mkdir -p " + msg.target_path, flush=True)
            makedirs(msg.target_path)
        except OSError as exc:  # Python >2.5 (except OSError, exc: for Python <2.5)
            if exc.errno == errno.EEXIST and isdir(msg.target_path):
                pass
            else:
                raise

        # copy static ADs to fill all resolutions
        CopyADStatic(msg)

        stream = ADClipDecision(msg, db)
        if not stream:
            print("Query AD clip failed and fall back to skipped ad clip!",
                  flush=True)
            # mark zk as incomplete (so that the valid one can be generated next time)
            zk.process_abort()
            zk.close()
            return

        # try to re-generate resolution specific AD
        SignalIncompletion(msg.target)

        try:
            # only generate one resolution for ad segment, if not generated, ad will fall back to skipped ad.
            cmd = GetABRCommand(stream,
                                msg.target_path,
                                msg.streaming_type,
                                msg.GetRedition(),
                                duration=msg.segment_duration,
                                fade_type="audio",
                                content_type="ad")
            process_id = subprocess.Popen(cmd, stdout=subprocess.PIPE)
            # the `multiprocessing.Process` process will wait until
            # the call to the `subprocess.Popen` object is completed
            process_id.wait()
            SignalCompletion(msg.target)
            zk.process_end()
        except Exception as e:
            print(str(e))
            CopyADStatic(msg)
            zk.process_abort()
    zk.close()
예제 #4
0
def process_stream(stream):
    stream_name = stream.split("/")[1]
    if not isfile(archive_root + "/" + stream_name): return

    zk = ZKState("/content_provider_transcoder/" + archive_root + "/" + stream)
    if zk.processed():
        zk.close()
        return

    if stream.endswith(".mpd"):
        try:
            mkdir(dash_root + "/" + stream_name)
        except Exception as e:
            print(str(e))

        if zk.process_start():
            try:
                cmd = GetABRCommand(archive_root + "/" + stream_name,
                                    dash_root + "/" + stream_name, "dash")
                r = call(cmd)
                if r: raise Exception("status code: " + str(r))
                zk.process_end()
            except Exception as e:
                print(str(e))
                zk.process_abort()
    if stream.endswith(".m3u8"):
        try:
            mkdir(hls_root + "/" + stream_name)
        except Exception as e:
            print(str(e))

        if zk.process_start():
            try:
                cmd = GetABRCommand(archive_root + "/" + stream_name,
                                    hls_root + "/" + stream_name, "hls")
                r = call(cmd)
                if r: raise Exception("status code: " + str(r))
                zk.process_end()
            except Exception as e:
                print(str(e))
                zk.process_abort()

    zk.close()
예제 #5
0
def ADTranscode(kafkamsg, db):
    msg=KafkaMsgParser(kafkamsg)
    # path: /var/www/adinsert/hls/Content_seq7u10.mp4/adstream/u10/4, name: 360p.m3u8
    zk_path="/ad-transcode/"+ ("/".join(msg.target_path.split("/")[-5:]))
    print("zk_path: "+zk_path+"/"+msg.target_name, flush=True)

    zks=ZKState(zk_path, msg.target_name)
    start_time=time.time()
    if zks.processed():
        print("AD transcoding finish the clip :",msg.target, flush=True)
        zks.close()
        return

    if zks.process_start():
        try:
            makedirs(msg.target_path)
        except:
            pass

        stream = ADClipDecision(msg,db, True)
        zkd_path="/".join(msg.target.replace(adinsert_archive_root+"/","").split("/")[:-1])
        if not stream:
            set_ad_path(zk_segment_prefix+"/"+zkd_path+"/link","/adstatic")
            zks.process_abort()
        else:
            try:
                stream_folder = msg.segment_path + "/" + stream.split("/")[-1]
                print("Checking pre-transcoded stream: "+stream_folder, flush=True)
                if isdir(stream_folder): # pre-transcoded AD exists
                    print("Prefetch the AD segment {} \n".format(stream_folder),flush=True)
                    CopyADSegment(msg,stream)
                else:
                    print("Transcoding the AD segment {} \n".format(stream),flush=True)
                    # only generate one resolution for ad segment, if not generated, ad will fall back to skipped ad.
                    cmd = GetABRCommand(stream, msg.target_path, msg.streaming_type, msg.GetRedition(), duration=msg.segment_duration, fade_type="audio", content_type="ad")
                    print("Command for creating segments: {} from url {} \n".format(cmd, stream),flush=True)
                    process_id = subprocess.Popen(cmd,stdout=subprocess.PIPE)
                    # the `multiprocessing.Process` process will wait until
                    # the call to the `subprocess.Popen` object is completed
                    process_id.wait()

                # signal that we are ready
                set_ad_path(zk_segment_prefix+"/"+zkd_path+"/link","/adinsert/"+zkd_path)
                zks.process_end()
                print("Status transcode: Timing {0} {1} {2} {3} {4}".format(msg.start_time, start_time, time.time()-start_time, msg.user_name, msg.target), flush=True)
            except Exception as e:
                print(traceback.format_exc(), flush=True)
                set_ad_path(zk_segment_prefix+"/"+zkd_path+"/link","/adstatic")
                zks.process_abort()
    zks.close()
예제 #6
0
def process_stream_vods(msg):
    stream_name = msg["name"]
    stream_type = msg["output"]["type"]
    stream_parameters = msg["parameters"]
    loop = msg["loop"]
    idx = msg["idx"] if "idx" in msg.keys() else int(random.random() * 10000)
    stream = stream_type + "/" + stream_name

    print("VOD transcode:", stream, flush=True)
    if not isfile(ARCHIVE_ROOT + "/" + stream_name):
        return

    zk = ZKState("/content_provider_transcoder/" + ARCHIVE_ROOT + "/vods/" +
                 stream)
    if zk.processed():
        zk.close()
        return

    target_root = VIDEO_ROOT + stream_type

    try:
        makedirs(target_root + "/" + stream_name)
    except:
        pass

    if zk.process_start():
        try:
            cmd = GetABRCommand(ARCHIVE_ROOT + "/" + stream_name,
                                target_root + "/" + stream_name,
                                stream_type,
                                params=stream_parameters,
                                loop=loop)
            print(cmd, flush=True)
            r = execute(idx, stream_name, cmd)
            if r:
                raise Exception("status code: " + str(r))
            zk.process_end()
        except:
            print(traceback.format_exc(), flush=True)
            zk.process_abort()

    zk.close()
예제 #7
0
def process_stream(stream):
    streams = []
    stream_name = stream.split("/")[1]
    config = configparser.ConfigParser()
    config.read('config.ini')
    src_mode = config.get('mode', 'srcMode')
    src_path = config.get('path', 'srcPath')
    src_protocol = ""
    src_api = ""
    if src_mode == "local":
        streams = listdir(src_path)
    elif src_mode == "live":
        html = urlopen("http://" + src_path)
        soup = BeautifulSoup(html, 'html.parser')
        src_protocol = "rtmp://"
        src_api = src_mode
        for item in soup.findAll('a')[1:]:
            streams.append(item.get('href'))
    else:
        return

    if stream_name not in streams:
        return

    if src_mode == "live":
        stream_name = stream_name[:stream_name.index('.')]

    zk = ZKState(src_protocol + src_path + "/" + src_api + "/" +
                 stream.replace("/", "_"))
    if zk.processed():
        zk.close()
        return

    if stream.endswith(".mpd"):
        try:
            mkdir(DASH_ROOT + "/" + stream_name)
        except Exception as e:
            print(str(e))

        if zk.process_start():
            try:
                cmd = GetABRCommand(
                    src_protocol + src_path + "/" + src_api + "/" +
                    stream_name, DASH_ROOT + "/" + stream_name, "dash")
                r = call(cmd)
                if r:
                    raise Exception("status code: " + str(r))
                zk.process_end()
            except Exception as e:
                print(str(e))
                zk.process_abort()
    if stream.endswith(".m3u8"):
        try:
            mkdir(HLS_ROOT + "/" + stream_name)
        except Exception as e:
            print(str(e))

        if zk.process_start():
            try:
                cmd = GetABRCommand(
                    src_protocol + src_path + "/" + src_api + "/" +
                    stream_name, HLS_ROOT + "/" + stream_name, "hls")
                r = call(cmd)
                if r:
                    raise Exception("status code: " + str(r))
                zk.process_end()
            except Exception as e:
                print(str(e))
                zk.process_abort()

    zk.close()