Home > Back-end >  How to loop through my text file and call the name of picture
How to loop through my text file and call the name of picture

Time:08-25

I need to take all audio files from a directory, and export them along with one of multiple pictures listed in a text file.

It's working to take all the audio files one by one, but I cannot make it have a different picture for each video. I have same picture all the time.

  1. I don't know how to make the loop working for the pictures files.
  2. Will be nice if someone can tell me how I can group pictures in order to export as a slideshow.

Thank you.

In .txt file my pictures are like this :

1.jpg
2.jpg
3.jpg

And my code looks like this:

@echo off
set "sourcedir=E:\test slideshow imagini"  
set "outputdir=E:\test slideshow imagini\1" 
PUSHD "%sourcedir%"
for /F "tokens=*" %%A in (poze3.txt) do (
    echo %%A
    :: pictures names
     for %%F in (*.wav *.mp3) DO ffmpeg -loop 1 -i %%A -i "%%F" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest -vf scale=1920:1080 "%outputdir%\%%F.mp4" 
    POPD
)

CodePudding user response:

You need a trick to read the next line of a file with each run of the loop:

@echo off
setlocal enabledelayedexpansion
set "sourcedir=E:\test slideshow imagini"  
set "outputdir=E:\test slideshow imagini\1" 
PUSHD "%sourcedir%"
<"poze3.txt" (
    for %%F in (*.wav *.mp3) DO (
        set /p pic=
        ffmpeg -loop 1 -i !pic! -i "%%F" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest -vf scale=1920:1080 "%outputdir%\%%F.mp4" 
    )
)

(untested, but should work).

Btw: DO NOT use :: as comment. Use REM instead.

I know, :: is used often and behaves well outside of code blocks, but tends to hiccup when used within code blocks, leading to unexpected behaviour which is hard to troubleshoot.

I removed the popd in the loop. Repeatedly popd's without matching push's don't make sense. You do only one pushd before the loop, so the matching popd should be after the loop, where you can omit it, as the script ends anyway.

  • Related