6

I'm trying to batch encode 45 video files, and copy the encoded files into an existing subfolder called "encode"

I've used the suggested code from this question (https://stackoverflow.com/questions/5784661/how-do-you-convert-an-entire-directory-with-ffmpeg) But, I don't know how to capture the original file names (with spaces and punctuation) and use that as the new file name in the new directory.

Here's the code I'm using so far:

for i in *.mp4; do name=`echo "${i%.*}"`; echo $name; ffmpeg -i “$i” -vf "scale='if(gt(a*sar,16/9),640,-1)':'if(gt(a*sar,16/9),-1,360)',pad=640:360:(ow-iw)/2:(oh-ih)/2,setsar=1" -vcodec libx264 -b:v 1600k -bufsize:v 1600k -r 30000/1001 “encoded/$name.mp4” done It's giving me an "invalid argument" error message when I run it - probably because I haven't specified the names correctly(?)

The ffmpeg code that I'm using does work (I can encode individual file) but I haven't figured out how to batch encode the entire directory.

If anyone can let me know what I'm doing wrong, I would love to know!

Thank you!

1

4 Answers 4

6

The accepted answer in the question you're referring to uses additional unnecessary processes, legacy code, and doesn't prevent globbing or word splitting.

See the second answer which is shorter, simpler, and more efficient:

for f in *.mp4; do ffmpeg -i "$f" <additional options> "encoded/${f%.*}.mp4"; done

Secondly, don't use fancy quotes which will cause errors, so instead of “$varible” use "$variable".

5
  • Returns f was unexpected at this time. I feel like I'm missing something.
    – user3643
    Jan 21, 2017 at 17:20
  • I had to use for %f in (*.mp4); do ffmepg -i %f <additional options> %f.mp3. I got the batch to run correctly, but the extension is now duplicated. My files look like file.mp4.mp3. I'll figure it out soon, I'm sure.
    – user3643
    Jan 21, 2017 at 18:34
  • Acutally, I couldn't figure it out. Perhaps you can help: video.stackexchange.com/questions/20474/…
    – user3643
    Jan 21, 2017 at 20:15
  • @fredsbend Are you using Windows? My example is Bash shell (for Linux users).
    – llogan
    Jan 23, 2017 at 6:15
  • @LordNeckbeard Yes, I'm on Windows 10.
    – user3643
    Jan 23, 2017 at 7:57
2

I think you can achieve what you need with my ffmpeg Windows front-end, where you can easily select output destination folder.

https://sourceforge.net/projects/ffmpeg-batch

It adds to ffmpeg a window fashion interface, progress bars and time remaining info, features I always missed when using ffmpeg.

2
  • Please make sure to clearly indicate if an answer refers to a software or product that you are affiliated with. I have altered the answer to include the needed information.
    – AJ Henderson
    Apr 25, 2018 at 18:58
  • Yes indeed, thanks for the clarification, just trying to make people's life easier.
    – Eibel
    Apr 11, 2019 at 11:05
0

I'm trying to batch encode 45 video files, and copy the encoded files into an existing subfolder called "encode"

Try this: https://github.com/alessandro893/ffmpeg-batch-remux

1
  • Could you provide a little more detail on how to use the tool you linked to? Most people come here looking for more in depth answer than just a link. Aug 6, 2018 at 23:48
0

To convert a file within the same format without the extension saved into file name in batch mode just add ~n to the final input, like so:

for %f in (*.mkv) do ffmpeg -i %f <additional options> %~nf.mp4

If one needs to reformat the codec and wishes to keep the same title and save in the same extension it must be saved into another folder. Start the ffmpeg command from the location of the original files. Navigate to that different folder and copy the entire location from the address bar and paste it exactly as is at the beginning of the output name. For this there must be " " around the whole output address. Replace everything within () with your address, and then remove the ().

for %a in (*.mp4) do ffmpeg -i "%a" <additional options> "(F:\VIDEO\New)\%~na.mp4"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.