LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Batch convert video in entire folder using ffmpeg (https://www.linuxquestions.org/questions/linux-general-1/batch-convert-video-in-entire-folder-using-ffmpeg-639309/)

Steven Hyde 05-02-2008 05:32 AM

Batch convert video in entire folder using ffmpeg
 
I am trying to schedule batch conversion of all mpeg in a single base folder to swf in another output folder, backup original mpeg, and delete original mpeg from base folder.

I would like to run when mpeg exist (appear) in base folder or on a schedule.

I was planning on scheduling bat files containing ffmpeg commands to run and use xcopy/del for backup but other posts seem to handle similar tasks with bash shell scripts (which I am not too familiar) I am open to whichever method works best.

When I try to run the following...I get the error "cannot open file C:\OUTPUT\*.swf"
Code:

c:\ffmpeg\ffmpeg.exe -i "C:\BASE\*.mpg" -ar 44100 -ab 192 "C:\OUTPUT\*.swf"
If I don't use wildcards and specify individual file names instead it works.


??????
Code:

for x in *.mpg; do ffmpeg -i "$x" -ar 44100 -ab 192  "`basename "$x" .mpg`.swf"; done

I am not sure how to use bash scripts similar to below to schedule conversions of an entire folder.

bash example: mpg2swf
Code:

#!/bin/bash

mkdir output
for x in *.mpg ; do
echo processing $x
ffmpeg -i $x -ar 44100 -ab 192
output/$x
done

chmod a+x mpg2swf


Thanks.

unSpawn 05-02-2008 06:01 AM

There are a gazillion applications and scripts already that can do what you want, but since your commandline works, lets just expand. I hope commentary is kind of self-explanatory else you might want to read some Bash scripting guides like http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html and http://www.tldp.org/LDP/Bash-Beginne...tml/index.html.

Code:

#!/bin/sh --
# The directory name that holds your movies.
DIR="/tmp/movies"
# Creating the subdirectory names that hold the result and the backup.
for NEW in swf backup; do
 mkdir "${DIR}/${NEW}" || { echo "Could not create ${DIR}/${NEW}, exiting."; exit 1; }
done
# Start looping over files, tell what we do and log any output to screen and log.
for MPEG in ${DIR}/*.mpg; do echo "Starting ${MPEG}"
 ffmpeg -i "${MPEG}" -ar 44100 -ab 192  "${DIR}/swf/${MPEG%.mpg}.swf" 2>&1 | tee -a "${DIR}/conversion.log"
 # If ffmpeg exited OK, then say it and move the original to the backup directory.
 if [ $? -eq 0 ]; then
  echo "Completed ${MPEG%.mpg}.swf"; mv "${MPEG}" ${DIR}/backup || { echo "Could not move ${MPEG}, exiting."; exit 1; }
 else
  # If ffmpeg failed, then say so and exit loop.
  echo "FAILED converting ${MPEG}, exiting."; exit 1;
 fi
done # End of file loop
# Exit.
exit 0

...or in a less functional, terse format:
Code:

#!/bin/sh --
DIR="/tmp/movies"; for NEW in swf backup; do mkdir "${DIR}/${NEW}"; done
for MPEG in ${DIR}/*.mpg; do ffmpeg -i "${MPEG}" -ar 44100 -ab 192  "${DIR}/swf/${MPEG%.mpg}.swf" \
>/dev/null 2>&1; [ $? -eq 0 ] && mv "${MPEG}" ${DIR}/backup; done; exit 0


seraphim172 05-02-2008 06:58 AM

pathname syntax
 
The "cannot open file C:\OUTPUT\*.swf" error actually indicates that you are using a Windows-style pathname. On the other hand you are using a bash script, that would require UNIX-style pathnames. I suppose this is even true if you run CygWin on Windows, though I'm not sure of this.

Linux Archive

Steven Hyde 05-02-2008 06:42 PM

I'm sorry, it's still not working for me.
I am using cygwin in a WinXP environment.
I tested a simple bash script and it works using cygwin through bash.exe.
Code:

echo 123
read

However, using bash.exe to run the below script has no effect and closes without error.
I can't understand what I may be doing wrong.

Thanks for sticking with me on this...


seraphim172 - yes. The code that generated that error was run from win cmd line. It didn't work most likely because of my attempt to use wildcards to perform conversion on multiple files in a folder. Most likely this is incorrect ffmpeg syntax.

I am also looking into trying to schedule bash scripts and unSpawn is trying to help me figure this out.

If I knew the proper syntax to run ffmpeg on a folder from cmd I would use that. If I could work out a bash/shell script to do the same I would try to use that.

Applications and utilities I have found require manual selection of individual files in a folder to do batch conversion which is not flexible enough.

Thanks again.

Steven Hyde 05-03-2008 05:28 PM

Now I am able to get the error:
Quote:

line 20: syntax error: unexpected end of file
Any suggestions?


All times are GMT -5. The time now is 01:11 AM.