def createVideo(originalClipName, subtitlesFileName, outputFileName,
                alternateAudioFileName, useOriginalAudio):
    # This function is used to put all of the pieces together.
    # Note that if we need to use an alternate audio track, the last parm should = False

    print("\n==> createVideo ")

    # Load the original clip
    print("\t" + strftime("%H:%M:%S", gmtime()),
          "Reading video clip: " + originalClipName)
    clip = VideoFileClip(originalClipName)

    print("\t\t==> Original clip duration: " + str(clip.duration))

    if useOriginalAudio == False:
        print(strftime("\t" + "%H:%M:%S", gmtime()),
              "Reading alternate audio track: " + alternateAudioFileName)
        audio = AudioFileClip(alternateAudioFileName)
        audio = audio.subclip(0, clip.duration)
        audio.set_duration(clip.duration)
        print("\t\t==> Audio duration: " + str(audio.duration))
        clip = clip.set_audio(audio)
    else:
        print(strftime("\t" + "%H:%M:%S", gmtime()),
              "Using original audio track...")

    # Create a lambda function that will be used to generate the subtitles for each sequence in the SRT
    generator = lambda txt: TextClip(
        txt, font='Arial-Bold', fontsize=24, color='white')

    # read in the subtitles files
    print("\t" + strftime("%H:%M:%S", gmtime()),
          "Reading subtitle file: " + subtitlesFileName)
    subs = SubtitlesClip(subtitlesFileName, generator)
    print("\t\t==> Subtitles duration before: " + str(subs.duration))
    subs = subs.subclip(0, ((clip.duration) - 0.001))
    subs.set_duration(clip.duration - .001)
    print("\t\t==> Subtitles duration after: " + str(subs.duration))
    print("\t" + strftime("%H:%M:%S", gmtime()),
          "Reading subtitle file complete: " + subtitlesFileName)

    print("\t" + strftime("%H:%M:%S", gmtime()), "Creating Subtitles Track...")
    annotated_clips = [
        annotate(clip.subclip(from_t, to_t), txt)
        for (from_t, to_t), txt in subs
    ]

    print("\t" + strftime("%H:%M:%S", gmtime()),
          "Creating composited video: " + outputFileName)
    # Overlay the text clip on the first video clip
    final = concatenate_videoclips(annotated_clips)

    print("\t" + strftime("%H:%M:%S", gmtime()),
          "Writing video file: " + outputFileName)
    final.write_videofile(outputFileName)
    clip.reader.close()
    clip.audio.reader.close_proc()
    def __generateSubs( self, clip, subPath ):

        generator = lambda txt: TextClip(txt, font='Amiri-Bold', fontsize=40, color='white')

        subs = SubtitlesClip( subPath, generator )
        subs = subs.subclip( 0, clip.duration - .001 )
        subs.set_duration( clip.duration - .001 )

        return subs
def createVideo(originalClipName,
                subtitlesFileName,
                outputFileName,
                alternateAudioFileName,
                useOriginalAudio=True):
    # This function is used to put all of the pieces together.
    # Note that if we need to use an alternate audio track, the last parm should = False

    print("\n==> createVideo ")

    # Load the original clip
    print("\t" + strftime("%H:%M:%S", gmtime()),
          "Reading video clip: " + originalClipName)
    clip = VideoFileClip(originalClipName)
    print("\t\t==> Original clip duration: " + str(clip.duration))

    if useOriginalAudio == False:
        print(strftime("\t" + "%H:%M:%S", gmtime()),
              "Reading alternate audio track: " + alternateAudioFileName)
        audio = AudioFileClip(alternateAudioFileName)
        audio = audio.subclip(0, clip.duration)
        audio.set_duration(clip.duration)
        print("\t\t==> Audio duration: " + str(audio.duration))
        clip = clip.set_audio(audio)
    else:
        print(strftime("\t" + "%H:%M:%S", gmtime()),
              "Using original audio track...")

    # Create a lambda function that will be used to generate the subtitles for each sequence in the SRT
    generator = lambda txt: TextClip(
        txt, font='Arial-Bold', fontsize=24, color='white')

    # read in the subtitles files
    print("\t" + strftime("%H:%M:%S", gmtime()),
          "Reading subtitle file: " + subtitlesFileName)
    subs = SubtitlesClip(subtitlesFileName, generator)

    #print("\t\t==> Subtitles duration before: " + str(subs.duration))

    subs = subs.subclip(0, clip.duration - .001)
    subs.set_duration(clip.duration - .001)

    #try to see if chunks of approx 120 secs will work
    subsetTxts = []
    counter = 0
    subsetStart = 0
    subsetEnd = 0
    txts = []
    for txt in subs:
        counter = counter + 1
        txts.append(txt)
        if counter % 30 == 0:
            subsetTxts.append(txts[:])
            txts = []
    if len(txts) > 0:
        subsetTxts.append(txts[:])

    #subtract .001 from last time
    subsetTxts[-1][-1][0][1] -= 0.001

    test = 0
    clipFileNames = []
    for subset in subsetTxts:
        test = test + 1
        if test > 10:
            break
        fileName = 'clip_' + str(math.floor(subset[0][0][0])) + '.mp4'
        annotated_clips = [
            annotate(clip.subclip(from_t, to_t), txt)
            for (from_t, to_t), txt in subset
        ]
        clipFile = concatenate_videoclips(annotated_clips)
        clipFile.write_videofile(fileName)
        clipFileNames.append(fileName)
        gc.collect()

    finalClips = []
    for c in clipFileNames:
        finalClips.append(VideoFileClip(c))
    finalFile = concatenate_videoclips(finalClips)
    finalFile.write_videofile(outputFileName)