Advanced Scene Switcher

Advanced Scene Switcher 1.34.2

This might be a bit out there, but I figured I'd ask here.

I use Youtube Music to play music during my live streams, and I use Amuse to display the current song being played at that time. It also displays the total song length and how far along the current song is as well. YT Music doesn't support crossfade and so I thought I could sort of simulate the fading in/out of music by just increasing/decreasing the volume of the actual music Source in OBS itself.

After attempting various ideas I determined that I could use OCR to "read" the total length of the song, store it as a variable, and then subtract say 10 or 10 seconds to determine when i want the fade out to start and then store that as a separate variable. Since say "4:17" isn't something that can't be subtracted, I'd remove the colon and then get an actual integer. It seemingly worked until I realized that anything that's say 3 minutes long or 3:00 would become 300 and then 290, which doesn't really work when it comes to length of time. I also realized I had no idea how I could reinsert the colon without it being on the far left or right of said number.

I kept chipping at it trying to figure out if i could split the length of time into separate minutes and seconds variables specifically and then try some kind of math to convert the seconds and then somehow combine the numbers back together but it was late and something like this is just a bit out of my expertise.

I'm basically just asking am I over complicating things or is what I'm trying to do even possible? Thanks.
 
This might be a bit out there, but I figured I'd ask here.

I use Youtube Music to play music during my live streams, and I use Amuse to display the current song being played at that time. It also displays the total song length and how far along the current song is as well. YT Music doesn't support crossfade and so I thought I could sort of simulate the fading in/out of music by just increasing/decreasing the volume of the actual music Source in OBS itself.

After attempting various ideas I determined that I could use OCR to "read" the total length of the song, store it as a variable, and then subtract say 10 or 10 seconds to determine when i want the fade out to start and then store that as a separate variable. Since say "4:17" isn't something that can't be subtracted, I'd remove the colon and then get an actual integer. It seemingly worked until I realized that anything that's say 3 minutes long or 3:00 would become 300 and then 290, which doesn't really work when it comes to length of time. I also realized I had no idea how I could reinsert the colon without it being on the far left or right of said number.

I kept chipping at it trying to figure out if i could split the length of time into separate minutes and seconds variables specifically and then try some kind of math to convert the seconds and then somehow combine the numbers back together but it was late and something like this is just a bit out of my expertise.

I'm basically just asking am I over complicating things or is what I'm trying to do even possible? Thanks.
That is indeed rather complicated, but if you have figured out the OCR part to work reliably I think you have already done the most difficult part.
Assuming you have two variables named current time and total time I would suggest to use a condition of type "Script" to do the further string processing and time evaluation.

Something like this should do the trick:

1782931746350.png


Python:
import obspython as obs

def time_to_seconds(time_str: str) -> int:
    """Convert 'M:SS', 'MM:SS', or 'H:MM:SS' into total seconds."""
    parts = [int(p) for p in time_str.strip().split(':')]
    
    if len(parts) == 2:
        minutes, seconds = parts
        return minutes * 60 + seconds
    elif len(parts) == 3:
        hours, minutes, seconds = parts
        return hours * 3600 + minutes * 60 + seconds
    else:
        raise ValueError(f"Unexpected time format: {time_str}")


def seconds_to_time(total_seconds: int) -> str:
    """Convert total seconds back into 'M:SS' format (for display/debugging)."""
    minutes, seconds = divmod(total_seconds, 60)
    return f"{minutes}:{seconds:02d}"


def is_near_end(current_time_str: str, total_time_str: str, threshold: int = 10) -> bool:
    """
    Returns True if 'threshold' seconds or fewer remain in the song.
    current_time_str / total_time_str: e.g. '2:47', '4:17'
    threshold: seconds before the end to trigger (default 10)
    """
    current_seconds = time_to_seconds(current_time_str)
    total_seconds = time_to_seconds(total_time_str)
    remaining = total_seconds - current_seconds
    return remaining <= threshold

def run():
    try:
        return is_near_end("""${current time}""", """${total time}""", threshold=10)
    except Exception:
        return False

I hope that helps!
Let me know if you have questions!
 
Back
Top