Batch process ffmpeg?

5,950

Solution 1

Python solution

from sh import ffmpeg
from os import listdir
from os.path import split
from sys import argv

[ffmpeg("-i '{0}' -acodec libmp3lame 'Converted\{0}'".format(_file))
 for _file in sum([listdir(split(arg)[0]) for arg in argv], [])]

Solution 2

Do this not simply work?

for file in *.avi;do ffmpeg -i "$file" -acodec libmp3lame "Converted\\$file";done

Edit:

There seem to by a backslash problem...

Try this:

for file in *.avi;do ffmpeg -i "$file" -acodec libmp3lame 'Converted\\'"$file";done

or this:

for file in *.avi;do ffmpeg -i "$file" -acodec libmp3lame 'Converted\'"$file";done

Solution 3

In bash the following will do what you want if your run it in the directory which has the .avi files:

#!/bin/bash
for file in *.avi; do
    ffmpeg -i "$file" -c:a libmp3lame "Converted\\$file";
done;
Share:
5,950

Related videos on Youtube

A T
Author by

A T

Updated on September 18, 2022

Comments

  • A T
    A T almost 2 years

    Given an AVI encapsulated video with WMA2 audio and MP4 video how do I pass-through the video, convert the audio to MP3; then re-encapsulate the entire file into an AVI?

    ffmpeg -i "foo bar.avi" -acodec libmp3lame "Converted\foo bar.avi"
    

    Now how do I do that same command for every .avi file in this directory?

    (need a solution using bash or Windows CLI [for?])

    • A T
      A T over 11 years
      Nope; I am not.
    • anishsane
      anishsane over 11 years
      So this command is successful & you see the output file generated as you want?
    • anishsane
      anishsane over 11 years
      By the way, if you want to pass through video "AS-IS", then add -vcodec copy in the ffmpeg command line. It will save video conversion time + retain the same quality in video.
  • Admin
    Admin over 11 years
    I was first ;-)
  • A T
    A T over 11 years
    Thanks, but neither worked. I am using Cygwin bash if that makes a difference.
  • Admin
    Admin over 11 years
    Was there any error message? How did it not work?
  • Admin
    Admin over 11 years
    @imp25 Maybe do you need to escape the backslash: ` \\ ` instead of ` \ `
  • A T
    A T over 11 years
    Nope, now getting same error (the final error I mentioned) on both // and `\\`
  • Admin
    Admin over 11 years
    Well, there is a conflict as Windows use ` \ ` for path and shell use ` \ ` for escape. Unfortunely ` \$var ` could be used to print effectively $var. So try the editer version, maybe.
  • slhck
    slhck over 11 years
    The first version should work in Bash.