0

I have videos in a folder of .h264 format and i want to convert them to mp4. now by default i can use ffmpeg to do this: Here is the command:

ffmpeg -i youtube.flv -c:v libx264 filename.mp4

But, new h264 files are being added and I want to keep converting the videos so as to use it for my python script.

I know i can use this to initialize the variable

SET /A num=1

But how do i write a batch script to take all the videos from the directory one by one even though there new videos being added?

I wrote this but this isn't working:

@echo off
SET /A num=1

for file in E:\Records\1\*.h264
do
    ffmpeg -i "$file" -c:v libx264 E:\Recods\1\converted\%num%.mp4
    move E:\Records\1\"$file" E:\Records\1\done\
    set /A num=%num%+1
done
PAUSE

I am making a done folder and moving the videos that have been converted there and in converted folder i am putting converted videos.. Now i just have to run a task scheduler each hour so that if there is a new entry it should convert it and move it to appropriate folder.

sks
  • 197
  • 1
  • 3
  • 14
  • Would using the Task-Scheduler to run the script be an option? – geisterfurz007 Mar 30 '17 at 10:49
  • sorry I don't know what a task scheduler does. I am complete beginner with batch scripting.. – sks Mar 30 '17 at 10:51
  • The Task-Scheduler can be used to run scripts or other tasks under certain triggers. This includes for example each hour, day or week for example. So You could have a script getting executed every time in a certain space of time to refresh your file list. In addition to that I think there are questions dealing with the listening on folders-topic. – geisterfurz007 Mar 30 '17 at 10:55
  • yeah i did some research.. I think task scheduler would be good here.. I can move the already converted data to a different folder in same directory and run the task scheduler each 1 hour so if new file is present there then it would get converted.. – sks Mar 30 '17 at 11:08
  • Going forward try to read the help for the command you are trying to use. The syntax you are using for the `FOR` command is not even close to what the help file shows. Open up a cmd prompt and type: `for /?` – Squashman Mar 30 '17 at 12:43

1 Answers1

1

You can loop over all files (even with applying a filter) in a folder using the for loop in batch like this:

@echo off
setlocal EnableDelayedExpansion
set /a vid=0
cd /d "your_Folder\Goes here"
for %%f in (*.h264) do (
  ffmpeg -i "%%~f" -c:v libx264 "!vid!.mp4"
  set /a vid=!vid!+1
)

So what happens here?
The first line is used to have the program running in the correct directory.
After that all files ending with .h264 (might be a different ending in your case; just took it from the question) in that directory are processed using your command. For that the filepath is placed in the spot for the input file and the counter together with .mp4 is placed as the outputfile.
The counter is a bit more tricky because of how sets of parenthesis are evaluated in batch, which is as one whole block. As solution for this there are a lot of questions and answers on SO. Look for "Delayed Expansion batch" and you will find something like this answer.

Placing this as a Task-Scheduler-Task running on a regular basis should keep your folder updated. For actually monitoring the folder I found this impressive code that can be used in combination with the Task-Scheduler to run that on startup. If the monitoring triggers, you can execute the batch-file above and it should be run each time a file is added. You can adjust the powershell-file with file-filters as well to make it fit your needs.

Community
  • 1
  • 1
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • 1
    While the forward slash does work for some commands they do not work for 80% of the other Windows console commands. I would advise that you always use a back slash for your file paths. – Squashman Mar 30 '17 at 12:44
  • It works but i want to name the converted files like this: 1.mp4, 2.mp4 etc so that it is easier for me to process in python script.. Now how to increment a counter in for loop.. above code to increment is not incrementing.. thanks – sks Mar 30 '17 at 14:04
  • Oh I did not know that was a requirement to have them numbered. I editted my answer for that :) – geisterfurz007 Mar 30 '17 at 14:10
  • Glad I could help :) – geisterfurz007 Mar 30 '17 at 15:35