0

Would there be any specific reason why my CPU used will drop from 96%-100% to like 30% on average when running this script.

I want it to run @ 100% if possible to process the videos faster. Any help will do thanks in advance.

import moviepy.editor as mp
from moviepy.editor import VideoFileClip, concatenate
import os, glob
from random import randint
from shutil import copyfile

def clipexport():

    video1 = mp.VideoFileClip(clips)
    Duration = video1.duration

    if "sin" in clips:

        try:
            startp = filename[filename.find("sin")+3:filename.find("sin")+6].strip()
            print(startp)
            if "}" in startp:
                startp = startp[:startp.find('}')]
                print(startp)
                start = int(startp)

            else:
                print(startp)
                start = int(startp)
        except:
            start = 0.35
            print('using default start value')

    else:
        start = 0.35
    print(start)

    if "sout" in clips:
        try:
            endp = filename[filename.find("sout")+4:filename.find("sout")+7].strip()
            print(endp)
            if "}" in endp:
                endp = endp[:endp.find('}')]
                print(endp)
                end = int(endp)

            else:
                print(endp)
                end = int(endp)

        except:
            end = Duration - 0.35
            print('using default end value')        

    else:
        end = Duration - 0.35

    print(Duration)
    print(end)
    trimmedvideo = mp.VideoFileClip(clips).subclip(start, end).resize(width=1920)
    trimmedvideo.write_videofile(exportPath, bitrate="8000k", threads="8")

def videocreator():

    calculateClips = 0

    for filename in os.listdir(newpath):
        print(filename)
        if ".mp4" in filename:

            calculateClips = calculateClips + 1

    calculateClips = calculateClips -1

    videolist = []
    videoLength = 0
    totalClips = 0

    for filename in os.listdir(newpath):

        print(filename + "Check Videos")

        if ".mp4" in filename:

            videolist.append(VideoFileClip(newpath + filename).crossfadein(1))

    final = concatenate(videolist, padding=-1, method="compose")
    final.write_videofile('myvideo.mp4', bitrate ='8000k', threads="8")

path = 'C:/TBD/TBD/#Frikkie/'
for infile in glob.glob( os.path.join(path, '**') ):
    if "-" in infile:
        Refno = infile[infile.find('kie')+4:infile.find('-')].strip()

    else:
        Refno = infile[infile.find('kie')+4:].strip()

    print(Refno)
    clippath = infile + "/"
    length = len(clippath)
    #print(length)
    for clips in glob.glob( os.path.join(clippath, '**') ):
        #print(clips)
        filename = clips[clips.find(infile)+length:]
        filename = filename[:filename.find(".")]
        filename = filename + ".mp4"
        print(filename)
        exportPath = infile + "/" + "Export" + "/" + filename
        exportfolder = infile + "/" + "Export"
        print(exportfolder)
        if not os.path.exists(exportfolder):
            os.makedirs(exportfolder)

        try:

            if ".mp4" in clips:

                #clipexport()
                print("done mp4")
            elif ".m4v" in clips:
                #clipexport()
                print("done mp4")
            elif ".MOV" in clips:
                #clipexport()
                print("done mp4")
            else:
                continue


        except:

            print("Error")

    newpath = exportfolder + '/'
    newpath = newpath.replace('/', '\\')
    try:
        mp3number = str(randint(1,5))

        src = ("C:\music\{}.mp3".format(mp3number))
        dst = ("{}{}.mp3".format(newpath, mp3number))

        copyfile(src, dst)

    except:
        print("could not copy mp3")

    print("Mp3 Copied") 
    try:
        print("Video Path = " + newpath)
        videolist= []
        for filename in os.listdir(newpath):

            if "{" in filename:
                 newfilename = filename[:filename.find("{")].strip()
                 newfilename = newfilename + ".mp4"
                 oldfilename = newpath + filename
                 rename = newpath + newfilename
                 os.rename(oldfilename, rename)
                 videolist.append(newfilename)
            else:
                videolist.append(filename)

        for files in videolist:


            if files == "1.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "01.mp4"
                os.rename(oldfilename, rename)

            elif files == "2.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "02.mp4"
                os.rename(oldfilename, rename)

            elif files == "3.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "03.mp4"
                os.rename(oldfilename, rename)

            elif files == "4.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "04.mp4"
                os.rename(oldfilename, rename)

            elif files == "5.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "05.mp4"
                os.rename(oldfilename, rename)

            elif files == "6.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "06.mp4"
                os.rename(oldfilename, rename)

            elif files == "7.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "07.mp4"
                os.rename(oldfilename, rename)

            elif files == "8.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "08.mp4"
                os.rename(oldfilename, rename)

            elif files == "9.mp4":
                print(files)
                oldfilename = newpath + files
                rename = newpath + "09.mp4"
                os.rename(oldfilename, rename)

        print('done')

    except:
        print('not done')
    videocreator()     
Tom Burrows
  • 2,225
  • 2
  • 29
  • 46
  • 1
    `threads="8"` is probably too low. For example `x264` uses 1.5 * logical cores by default. Either try removing the option to use the defaults or set the appropriate values based on codec. – aergistal Jun 26 '17 at 12:22
  • Thanks aergistal even if i remove that option a 33 sec clip consisting of 3 clips only takes of 5 mins to export, My PC i am using has an i7 CPU 860 @ 2.8ghz with 12 gb memory and do have an SSD also. Anything Else i can try perhaps? – Frikkie Maritz Jun 26 '17 at 12:37
  • 1
    It would be useful to see what `ffmpeg` commands it execs. Any way to capture them? – aergistal Jun 26 '17 at 15:10
  • Sort of a short version would be something like this – Frikkie Maritz Jun 26 '17 at 15:45
  • ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -f lavfi -r "30" -i "color=black:s=1920X1080:r=30" -filter_complex "[0:v]format=pix_fmts=yuva420p,fade=t=out:st=10:d=1:alpha=1,setpts=PTS-STARTPTS[va0]; [1:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+10.18/TB[va1]; [2:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+27.009999999999998/TB[va2]; [3:v]scale=1920x1080,trim=duration=36.73[over]; [over][va0]overlay[over1]; [over1][va1]overlay[over2]; [over2][va2]overlay=format=yuv420[outv]" -vcodec libx264 -crf 19 -map [outv] T123456.mp4 – Frikkie Maritz Jun 26 '17 at 15:46
  • Show the complete console output from that command as well. It is recommended to [edit] your question in include more information instead of attempting to cram everything into limited space comments. – llogan Jun 26 '17 at 18:53
  • @FrikkieMaritz How did you get the complete FFmpeg command that MoviePY uses? – user319862 Nov 16 '19 at 15:56

0 Answers0