EroScripts is a community for scripts that sync up interactive sex toys with porn. Learn more.

To discover, download, and help choose what gets scripted, create an account.

EroScripts

Python script for automatic scripting of Cock Hero with visual beat meters

Intro
I have created a python script, that can script cock hero videos, if they have a nice visual beat meter. I have used the script to create
https://discuss.eroscripts.com/t/cock-hero-seductive-reasoning/34245
https://discuss.eroscripts.com/t/cock-hero-freedom/34246
It takes about 5 minutes of actual work, to script a video.

Code

# Setup
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from tqdm import tqdm
%matplotlib inline

class BeatScripter:
    def __init__(self, filename):
        self.filename = filename
        self.file_path = f'D:\\ax156\Launch\Scripts\CH todo\{self.filename}.mp4'
        self.file_path_out = f'D:\\ax156\Launch\Scripts\CH todo\{self.filename}.funscript'
        self.box_top_corner = (0,0)
        self.box_size = (50,50)
        self.frames_list = []
        self.beats_trained_list = []
        self.model = KNeighborsClassifier(n_neighbors=5)
        self.predicted_beats = []
        
    def load_frames_list(self, start_times, number_of_frames = 100):
        for start_time in start_times:
            self.frames_list.append(self.load_frames(start_time * 1000, number_of_frames))
            print(f"{number_of_frames} frames loaded from {start_time} seconds" )
        print(f"dimension (height, length, colors) {self.frames_list[0][0][1].shape}")
        
    def load_frames(self, start_time_in_msec, number_of_frames):
        cap = cv2.VideoCapture(self.file_path, cv2.CAP_PROP_POS_MSEC)
        frames = []
        i = 0
        while True:
            read, frame = cap.read()
            if not read:
                break
            time = cap.get(cv2.CAP_PROP_POS_MSEC)
            if time >= start_time_in_msec:
                frames.append((i, frame, time))
                if len(frames) >= number_of_frames:
                    break
            i += 1
        cap.release()
        return frames 
    
    def show_frame(self, frame_set, frame_number, y_sub=None):
        if y_sub is None:
            self.do_show_frame(self.frames_list[frame_set][frame_number][1], x = None, y = None)
        else:
            self.do_show_frame(self.frames_list[frame_set][frame_number][1][y_sub:, :, :], x = None, y = None)
        
    def show_frame_with_box(self, frame_set, frame_number, y_sub=None):
        if y_sub is None:
            self.do_show_frame(self.frames_list[frame_set][frame_number][1], 
                               x = (self.box_top_corner[0], self.box_top_corner[0] + self.box_size[0]),
                               y = (self.box_top_corner[1], self.box_top_corner[1] + self.box_size[1]))
        else:
            self.do_show_frame(self.frames_list[frame_set][frame_number][1][y_sub:, :, :], 
                               x = (self.box_top_corner[0], self.box_top_corner[0] + self.box_size[0]),
                               y = (self.box_top_corner[1]-y_sub, self.box_top_corner[1] + self.box_size[1] - y_sub))
    
    def anotate(self, num_frames = 100, y_sub=None):
        print("Space: not beat")
        print("b: beat")
        for frames in self.frames_list:
            beats_trained = self.beat_marker(frames[0:num_frames], y_sub=y_sub)
            self.beats_trained_list.append(beats_trained)
    
    def get_sub_frame(self, frame):
        return frame[self.box_top_corner[1]:self.box_top_corner[1] + self.box_size[1],
                     self.box_top_corner[0]:self.box_top_corner[0] + self.box_size[0], :]
    
    def train(self):
        beats_train = []
        frames_train = []
        for b in self.beats_trained_list:
            beats_train = beats_train + b
        for f in self.frames_list:
            frames_train = frames_train + f
        
        X = []
        y = []
        for i, beat in beats_train:
            for j, frame, time in frames_train:
                if i == j:
                    X.append(self.get_sub_frame(frame).flatten())
            y.append(int(beat))
        self.model.fit(X, y) 
    
    def do_show_frame(self, frame, x = None, y = None):
        red = (0, 0, 255)
        frame_cp = frame.copy()
        if x is not None:
            cv2.rectangle(frame_cp, (x[0], y[0]), (x[1], y[1]), red, -1)
        cv2.imshow("frame", frame_cp)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    def beat_marker(self, frames, y_sub):
        beats = []
        for i, frame, time in frames:
            if y_sub is None:
                cv2.imshow("frame", frame)
            else:
                cv2.imshow("frame", frame[y_sub:, :, :])
            while True:
                k = cv2.waitKey(0)
                if k == 32:         # wait for ESC key to exit
                    beats.append((i, False))
                    cv2.destroyAllWindows()
                    break
                elif k == ord('b'): # wait for 's' key to save and exit
                    beats.append((i, True))
                    cv2.destroyAllWindows()
                    break
        return beats

    def predict_beats(self, number):
        cap = cv2.VideoCapture(self.file_path, cv2.CAP_PROP_POS_MSEC)
        beats = []
        for i in tqdm(range(number)):   
            _, frame = cap.read()
            try:
                X_pred = [self.get_sub_frame(frame).flatten()]
                beats.append((self.model.predict(X_pred)[0] == 1,
                          i,
                          cap.get(cv2.CAP_PROP_POS_MSEC)))
            except:
                break
        cap.release()
        self.predicted_beats = beats

    def export_funscript(self):
        initial = '{"version": "1.0","inverted": false,"range": 90,"actions": ['
        end = ']}'
        middle = ''
        pos = 0
        last_i = 0

        for beat, i, time in self.predicted_beats:
            if beat and (i-last_i) > 4:
                next_step = '{"pos": ' + str(pos) + ', "at": ' + str(round(time)) + '}'
                middle = ','.join([middle, next_step])
                pos = 100 - pos
                last_i = i
        middle = middle[1:]

        text_file = open(self.file_path_out, "w")
        text_file.write(initial + middle + end)
        text_file.close()

#Script
beat_scripter = BeatScripter("chhappyface")
beat_scripter.load_frames_list([0, 1.5*60, 5*60, 9*60])

# beat_scripter.show_frame(1, 0, y_sub=500)
beat_scripter.show_frame(1, 0)

beat_scripter.box_top_corner = (625, 669)
beat_scripter.box_size=(32, 50)
# beat_scripter.show_frame_with_box(1 ,0, y_sub=500)
beat_scripter.show_frame_with_box(1 ,0)

# beat_scripter.anotate(y_sub=500)
beat_scripter.anotate()

beat_scripter.train()

beat_scripter.predict_beats(15000)
# beat_scripter.predict_beats(150000)

beat_scripter.export_funscript()

Tutorial

  1. Install and open jupyter https://jupyter.org/
    Maybe you need to install the following libraries
    scikit-learn Installing scikit-learn — scikit-learn 0.24.2 documentation
    cv2 opencv-python · PyPI
    matplotlib matplotlib.org/stable/users/installing.html
    pandas Installation — pandas 1.3.3 documentation
    tqdm tqdm · PyPI
  2. Create a new notebook and insert the code. The setup shold be in its own cell. And when you come to #Script an empty line, means the code should be divided in cells.
  • To enter a cell: Enter
  • To exit a cell: Escape
  • To create a new cell below, when outside: b
  • To create a new cell above, when outside: a
  • To run a cell: ctrl + Enter
  1. Change the paths self.file_path and self.file_path_out to the directory of the video. (keep \{self.filename}.mp4)
  2. Run the setup cell
  3. beat_scripter = BeatScripter("CH Freedom")
    Write the name of the file insted of “CH Freedom”
  4. beat_scripter.load_frames_list([0, 1.5*60, 5*60, 9*60])
    loads 100 frames from the video at the beginning, and at the 1.5, 5 and 9 minute marks
  5. beat_scripter.show_frame(1, 0)
    Opens a new window showing the first frame at the 1.5 minute mark. You can close the window by hitting enter. This frame should show the beat meter. If it does not, change the numbers. The first number corresponds to the above timestamps (starting at 0) 0 is 0 minutes, 1 is 1.5 minutes, 2 is 5 minutes and 3 is 9 minutes. The second number is the number of the frame in the series, also staring at 0.
  6. If the the resolution is so high, that you have problems having it on your screen, you can add for example “, y_sub=500” to your calls
beat_scripter.box_top_corner = (625, 669)
beat_scripter.box_size=(32, 50)
# beat_scripter.show_frame_with_box(1 ,0, y_sub=500)
beat_scripter.show_frame_with_box(1 ,0)

Now we need to tell the script where the beat meter is located. This code shows a frame with a red box. The box needs to cover the small area, where the beats “activate”. This is where the script will look for beats.
To move the box change the values in beat_scripter.box_top_corner = (625, 669) (x pixels from the top left corner, y pixels from the top left corner)
To change the size of the box change the numbers in beat_scripter.box_size=(32, 50)
Change the numbers and run the cell, until you are satisfied.
10. beat_scripter.anotate()
Now you need to tell the script what a beat and what a not-beat looks like.
The code will open a new window showing the first frame of the video. If the frame is a beat press “b” if not press “space”. Then it will show the next frame, until alle the frames we loaded in step 6 have been shown. It is okay to press b two-three times in a row, when it is almost a beat, and when it is a beat, and when it just was a beat. It will help the script.
11. beat_scripter.train()
This will let the script analyze what you have just told it.
12. beat_scripter.predict_beats(15000)
This will analyze the video and find the beats. The number is the number of frames, to analyze. This takes some time, so it is best just to do part of the video, until it finds the beats as it should.
If the script is ok, then increase the number, just type a number too large, it will stop when it reaches the end of the file. (fx 150000)
13. beat_scripter.export_funscript()
Exports the funscript in the video directory

What to do if the funscript is not ok?
You can fine tune the placement of the red box.
If there is a part of the video, where the script is bad, then load som frames from that part (add the minute mark to beat_scripter.load_frames_list([0, 1.5*60, 5*60, 9*60]))
If this doesn’t work, it is probably better to look for another video, some videos are just dificult.

Final notes
I hope you can use the script, and share the funscripts you create. The script is quite basic, so you are very welcome to improve it, and upload your version of it.
It would for example be nice to create the red box with the mouse instead of changing the numbers. The algorithm used for detecting beats (k-nearest neighbours) could also be improved.

19 Likes

First off, this is great and appreciated. I’m trying it out but ran into a couple hiccups:

  1. You should include a list of the libraries used for this project. I know its implied with the “import” stuff but a user may not have scikit-learn in their environment by default, and it takes some googling to figure out that I need to install scikit-learn in order to import sklearn. I have some prior knowledge using Python so it wasn’t that hard for me to figure out, but it might be good to include instructions on how to add these specific libraries to a python environment.

  2. I’m getting the following error and not sure where the index mismatch is. Any ideas?

When I run this:
beat_scripter.load_frames_list([0, 1.5*60, 5*60, 15*60])

I get this output:

100 frames loaded from 0 seconds
100 frames loaded from 90.0 seconds
100 frames loaded from 300 seconds
100 frames loaded from 900 seconds

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21456/2029814210.py in <module>
----> 1 beat_scripter.load_frames_list([0, 1.5*60, 5*60, 15*60])

~\AppData\Local\Temp/ipykernel_21456/201628164.py in load_frames_list(self, start_times, number_of_frames)
     26             self.frames_list.append(self.load_frames(start_time * 1000, number_of_frames))
     27             print(f"{number_of_frames} frames loaded from {start_time} seconds" )
---> 28         print(f"dimension (height, length, colors) {self.frames_list[0][0][1].shape}")
     29 
     30     def load_frames(self, start_time_in_msec, number_of_frames):

IndexError: list index out of range
1 Like

Are you planning to package it?
I want to use it easily

Good point I will add that.

I am unsure which of the needed libraries are part of the standard python installation. Did you have to install other packages than scikit-learn?

I am unsure why you get the error. But the line is not important, it just gives the resolution of the video, to make it easier to place the red box. You can just comment out the line with a “#”

Maybe, if I figure out how to do it

1 Like

Had to install all of these

import cv2
import matplotlib.pyplot as plt
import pandas as pd
from tqdm import tqdm

1 Like

I am having the same issue but after commenting out the line I get this

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_26520/3174591542.py in <module>
      4 
      5 # beat_scripter.show_frame(1, 0, y_sub=500)
----> 6 beat_scripter.show_frame(1, 0)
      7 
      8 beat_scripter.box_top_corner = (625, 669)

~\AppData\Local\Temp/ipykernel_26520/1070883719.py in show_frame(self, frame_set, frame_number, y_sub)
     47     def show_frame(self, frame_set, frame_number, y_sub=None):
     48         if y_sub is None:
---> 49             self.do_show_frame(self.frames_list[frame_set][frame_number][1], x = None, y = None)
     50         else:
     51             self.do_show_frame(self.frames_list[frame_set][frame_number][1][y_sub:, :, :], x = None, y = None)

IndexError: list index out of range

It looks like your videos are structured differently from the ones I have tried. Try posting the result of

print(beat_scripter.frames_list[0][0])

after loading the frames

I found the problem. but now I am running in to new problems

That looks like the same problem again. What is the output of the following?

beat_scripter = BeatScripter("CHSloots")
beat_scripter.load_frames_list([0])
print(beat_scripter.frames_list[0][0])
> beat_scripter = BeatScripter("Main-1")
> beat_scripter.load_frames_list([0])
> print(beat_scripter.frames_list[0][0])

returns

> 
> 100 frames loaded from 0 seconds
> 
> ---------------------------------------------------------------------------
> IndexError                                Traceback (most recent call last)
> ~\AppData\Local\Temp/ipykernel_15256/843545175.py in <module>
>       1 #Script
>       2 beat_scripter = BeatScripter("Main-1")
> ----> 3 beat_scripter.load_frames_list([0])
>       4 print(beat_scripter.frames_list[0][0])
> 
> ~\AppData\Local\Temp/ipykernel_15256/201628164.py in load_frames_list(self, start_times, number_of_frames)
>      26             self.frames_list.append(self.load_frames(start_time * 1000, number_of_frames))
>      27             print(f"{number_of_frames} frames loaded from {start_time} seconds" )
> ---> 28         print(f"dimension (height, length, colors) {self.frames_list[0][0][1].shape}")
>      29 
>      30     def load_frames(self, start_time_in_msec, number_of_frames):
> 
> IndexError: list index out of range

Sorry. Try to outcomment the line

print(f"dimension (height, length, colors) {self.frames_list[0][0][1].shape}")

in load_frames_list like this

#print(f"dimension (height, length, colors) {self.frames_list[0][0][1].shape}")

And then try

beat_scripter = BeatScripter("Main-1")
beat_scripter.load_frames_list([0])
print(beat_scripter.frames_list[0][0])
100 frames loaded from 0 seconds

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15256/843545175.py in <module>
      2 beat_scripter = BeatScripter("Main-1")
      3 beat_scripter.load_frames_list([0])
----> 4 print(beat_scripter.frames_list[0][0])

IndexError: list index out of range


Used this last night and had great results. My only gripe is that by default the progress bar will go to 15000 rather than being capped to the amount of frames in the video, leading to the progress bar being incorrect on videos with fewer than 15000 frames. The funscript was generated perfectly, I just thought it had frozen since the progress bar was stuck at 41%.

Does it literally just create 1 to 100 without any regard for speed or creativity?

Yes, it creates a single movement per beat script from 0 - 100. You can then run it through FunExpander to convert it into a hard mode script.

Personally I’d make some changes before sharing the script, but it saves time creating a very simple outline which you can build upon.

1 Like