Image source path changing based on input from a text file

Zzzes

New Member
I need OBS to read text from a txt file and then replace whatever file is in a certain source with a file of that name. Is there a tool for that?

Don't ask me why I need this, it is too long to explain.
 

RytoEX

Forum Admin
Forum Moderator
Developer
If you already have a mechanism for updating the text file, and the text file only has one line in it, you could just set the image source to target an image file and then overwrite that image file based on the text file's contents with an external script in just about any scripting language. Here are two quick examples.

Windows Batch
Code:
@ECHO OFF
REM copy images onto image.jpg based on text file contents

:BEGINLOOP
FOR /F %%I IN (image.txt) DO (
    ECHO %%I:
    ECHO  copying...
    COPY "%%I" image.jpg
    ECHO  copied!
    ECHO  sleeping...
    TIMEOUT 5
)

GOTO BEGINLOOP

Bash
Code:
#!/usr/bin/env bash

while true; do
    new_image_filename="$(cat image.txt)"
    echo "$new_image_filename:"
    echo " copying..."
    cp "$new_image_filename" "image.jpg"
    echo " copied!"
    echo " sleeping..."
    sleep 5
done
 

Zzzes

New Member
If you already have a mechanism for updating the text file, and the text file only has one line in it, you could just set the image source to target an image file and then overwrite that image file based on the text file's contents with an external script in just about any scripting language. Here are two quick examples.

Windows Batch
Code:
@ECHO OFF
REM copy images onto image.jpg based on text file contents

:BEGINLOOP
FOR /F %%I IN (image.txt) DO (
    ECHO %%I:
    ECHO  copying...
    COPY "%%I" image.jpg
    ECHO  copied!
    ECHO  sleeping...
    TIMEOUT 5
)

GOTO BEGINLOOP


[/CODE]

Oh, man this is actually exactly what I am looking for! One question though, Where in relation to the location of the batch file do I put image.jpg and all the other image files?

EDIT 1: apparently this batch thingymajig doesn't like unicode characters :/ is there any way to fix that? Because some of my images have unicode characters in their name
 
Last edited:

RytoEX

Forum Admin
Forum Moderator
Developer
In the context of the example script that I provided, you would put them all in the same directory. That behavior can be changed.

To support unicode characters is a bit tricky, but can be done. Use this script:
Code:
@ECHO OFF
@chcp 65001>nul
REM copy images onto image.jpg based on text file contents

:BEGINLOOP
FOR /F %%I IN (image.txt) DO (
    ECHO %%I:
    ECHO  copying...
    COPY "%%I" image.jpg
    ECHO  copied!
    ECHO  sleeping...
    TIMEOUT 5
)

GOTO BEGINLOOP

Ensure the file is saved as UTF-8 without BOM. When you create/save the "image.txt" file, also make sure to save that as UTF-8 without BOM. This can be done in Notepad++ or any other decent text editor. The unicode characters might not display correctly in the CMD/console window, but they should still process correctly behind the scenes (I've tested this).
 

Zzzes

New Member
Nope, didn't work. Whenever there are non-English characters the log looks something like this:

Code:
Waiting for 0 seconds, press a key to continue ...
The system cannot write to the specified device.
 copying...
The system cannot find the file specified.
 copied!
 sleeping...

And the image stays exactly as it was before
 

RytoEX

Forum Admin
Forum Moderator
Developer
It worked fine when I tested against files with Chinese/Japanese characters. What characters are in your filename, and what is your system language/locale set to?

This'd probably work fine with the Bash script. Batch is just ... touchy...
 

Zzzes

New Member
My filenames have Russim characters and some other symbols, system Lang is Russian, and, well, location is the Russian Federation.

But bash scripts are like a linux thing, running the om Win7 seems like such a hassle
 

RytoEX

Forum Admin
Forum Moderator
Developer
"The system cannot write to the specified device." could indicate a problem with user permissions or disk access/space. You could try running the script as an administrator.

"The system cannot find the file specified." sounds like it could still be having trouble changing the code page. Replace the line "@chcp 65001>nul" with "chcp 65001" so that it returns the result of that command to make sure it's working. If it works, it should show "Active code page: 65001".

I tested this against a filename with Cyrillic characters. Since I can't speak, read, or write Russian, I used random characters ("ДЖБЍШЪЫЭ.jpg"). The batch script still worked fine for me. Please double check that the batch script and the "image.txt" file are both encoded as "UTF-8" files without the Byte-Order Mark (BOM). If those are set correctly, please provide some sample filenames that are causing trouble for you. For context, I'm on Windows 10 with my system language/locale set in English.

There are ways to install Bash on Windows 7 (MSYS/MinGW/MSYS2, Gow, Git Bash, Cygwin, etc.). I already have Bash available because I write code, but I can understand that it might be a hassle for most users to install additional software.
 

Zzzes

New Member
Apparently bash scripts not as much of a hassle as I thought they were. Your script totally works!!! Thanks you sooooo much, man, I couldn't have figured this all out by myself, so you are getting a personal thank you note in the descriptions of my live streams. Have a great day, you are the best!
 

RytoEX

Forum Admin
Forum Moderator
Developer
Glad to help! I'd done image rotation scripts before, and I figured might be easier than trying to build a plugin or work with the image slideshow stuff. Good luck with your streams!
 
Top