I have my film and series collection in one directory, which obviously has a lot of sub directories and a lot of different formats. It's constantly being added to by all of us here. What I'd like to do is make a script that I can run occasionally when one or all of us has added in new stuff that will search for files in that directory/sub directories and convert them to a format that kodi/xbmc can stream to my trusty old xbox 360. I'm running Gnome 16.04, I'm not sure if that's relevant or not.

So far I've found the code to convert the actual files;

for i in *mkv; do ffmpeg -i $i -vcodec copy -acodec copy "$i.mp4"; done

and the files are all located in;

/media/dave/Seagate Expansion Drive/Media

So, these are the problems I'm left with.

  • Is mp4 the best way to go? Googling this hasn't really cleared it up for me, there are a million different opinions! So far it'll play some MP4's but not all.

  • How to adapt that little line of code to search for more than just mkv - there are mpeg, avi, wmv, mov, everything you can think of! Would I need to just repeat that line of code for each different file type?

  • How to make it search the subdirectories

  • How to make it delete the original file only if the conversion if successful

  • How can I set kodi/xbmc to load on start up already minimised?

Any help, suggestions or pointers to get this sorted would be REALLY appreciated :)

1 Answer 1


How to make it search the subdirectories

Use find.

How to adapt that little line of code to search for more than just mkv

Use -iregex option with find.

How to make it delete the original file only if the conversion if successful

You need an if switch.

Putting it all together:

while IFS= read -r -d '' movie ; do
    echo "Found: $movie"

    convert_command="ffmpeg -i '$movie' -vcodec copy -acodec copy '$movie.mp4'"

    if bash -c "$convert_command" ; then
        echo "Converted to mp4"
        rm "$movie"
        echo "Convertation failed"
done < <(find . -type f -iregex '.*\(mkv\|mpeg\|avi\|wmv\|mov\)' -print0)

I used this answer to create loop based in find output.

Advantages and disadvantages of mp4 and kodi/xbmc minimizing on startup are independent questions, I suggest you to create separate posts for each of them.

  • That's fantastic, thank you @teksisto - it kicks up a fairly lengthy error however which seems to center around a library config mismatch. It won't let me copy the whole thing in because it's massive - it looks like it's asking for config parameters. It ends it all with 'No such file or directory Convertation failed' - I'm honestly not sure how much help that is to you!
    – Dave
    Jul 23, 2016 at 19:54
  • Sorry, I forgot that file names can include spaces. See updated version.
    – olmstad
    Jul 24, 2016 at 19:52
  • Answering this question, I realized that my usual approach with for/find loop is wrong and works only because my files usually have no spaces in names.
    – olmstad
    Jul 24, 2016 at 19:58
  • Thank you @teksisto - it's still crashing out on me though. I think the problem might lie in the ffmpeg bit itself as that doesn't seem to work any-which-way. Would it be possible to swap that maybe for the av version do you think? I'll put another question up to try and figure out what it's deal is, but if I can find a fix that works with something else (even handbrake-cli?) then I'm still ahead of the game!
    – Dave
    Jul 26, 2016 at 15:20
  • I assumed that your ffmeg command is valid, and tested script with cp $movie $movie.converted.
    – olmstad
    Jul 27, 2016 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .