0

I want to make a "batch" file so to say for some bash commands (convert.sh). I think it would be best to describe the situation. i have a $#!^ ton of mp4 videos that i want converted into mp3's. it would take me an unreasonable amount of time to convert them using ffmpeg -i /root/name\ of\ video.mp4 /name\ of\ video.mp3 for every single video. not to mention the fact that all the file names are long and complicated so typos are a possibility. so i want to know how to make a shell script (for bash) that will take every file with the extension .mp4 and convert it to a .mp3 with the same name one by one. as in it converts one then when it done it moves on to the next one. im using a lightweight version of linux so any 3rd part soft probably wont work so i need to use ffmpeg...

many thanks in advance for any assistance you can provide

PS: i cant seem to get the formatting sytax on the website to work right so if somone can format this for me and maybe post a link to a manual on how it works that would be much appreciated =)

PPS: i understand that questions about using the ffmpeg command should be asked on superuser however since i dont so much have any questions about the specific command and this relates more to scripting a bash file i figure this is the right place

  • There are formatting instructions in the sidebar on the edit/post page. You can also get help text by hovering over the tools. – Carl Norum Feb 03 '13 at 17:58
  • 1
    Seems like you haven't taken a basic intro to shell scripting. Try googling "bash script tutorial" – Bjørne Malmanger Feb 03 '13 at 18:01
  • @BjørneMalmanger see iv tried that kinda thing but all i can ever find is stuff on making directories and moving files..... – Konner Rasmussen Feb 03 '13 at 18:16
  • 1
    Read http://www.tldp.org/LDP/abs/html/ (it does have imperfections, but it will teach you a lot); also learn about `at`, `batch`, `crontab`, `nohup`, `screen` .... commands – Basile Starynkevitch Feb 03 '13 at 18:25
  • Also see the [FFmpeg MP3 Encoding Guide](https://ffmpeg.org/trac/ffmpeg/wiki/Encoding%20VBR%20%28Variable%20Bit%20Rate%29%20mp3%20audio). – llogan Feb 04 '13 at 19:04

2 Answers2

1

A bash for loop should do it for you in no time:

SRC_DIR=/root
DST_DIR=/somewhereelse
for FILE in ${SRC_DIR}/*.mp4
do
    ffmpeg -i "${FILE}" "${DST_DIR}/$(basename \"${FILE}\" .mp4).mp3"
done

Sorry - I don't know the ffmpeg command line options, so I just copied exactly what's in your post.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

1) use find:

find . -name \*.mp4 | xargs ./my_recode_script.sh

2) my_recode_script.sh - see this question so you can easily change the extension for output file name

the rest is trivial scripting job:

ffmpeg -i $name $new_name # in my_recode_script.sh after changing extension

this is enough for one-time script, if you want something reusable, wrap it with yet another script which receive path to dir, extensions from which to which to recode and call other parts :)

Community
  • 1
  • 1
VB9-UANIC
  • 330
  • 1
  • 5