4

I wanted to just comment on an answer to a question very similar to this but I don't have enough rep. I'm looking for a way to change this line of code:

for i in *.mkv; do ffmpeg -i "$i" "${i%.*}.mp4"; done

So that it includes .avi files and so that it can search through nested folders. I want to be able to target /Videos and have the script automatically traverse through the folder tree into /Videos/2016/January, convert all of the video files contained within that folder, then do the same for /Videos/2016/February and so on.

Thanks in advance.

(credit for above line of code goes to LordNeckBeard, from this post.)

Community
  • 1
  • 1
E.Owen
  • 753
  • 1
  • 10
  • 25

1 Answers1

4

Using LordNeckBeard's reference to find, I came up with the following solution:

find ./ -iname '*.avi' -o -iname '*.mkv' -exec bash -c 'ffmpeg -i "{}" -c:v libx265 -preset medium -crf 28 -c:a aac "{}".mp4' \;

Tested and worked exactly how I expected, so it is currently running through the entire library.

If you want to give your converted files a different name to the original, see Parameter Expansion.

If you wish to destructively convert all files, be extremely careful with this command:

find ./ -iname '*.avi' -o -iname '*.mkv' -exec bash -c 'ffmpeg -i "{}" -c:v libx265 -preset medium -crf 28 -c:a aac "{}".mp4 && rm "{}"' \;

NOTE: The command above isn't bulletproof and was removing some files BEFORE the conversion process had finished, meaning I have now lost those files (thank God for backups). I tested with disposable files and have made sure I have a fully functional back up of my data before starting this procedure.

MysteryPancake
  • 1,365
  • 1
  • 18
  • 47
E.Owen
  • 753
  • 1
  • 10
  • 25
  • This command deleted a significant amount of files from my computer. I am now spending the rest of my day attempting to recover these files. I suggest removing rm "{}" completely from this code, in case anyone hastily copies it like I did. – MysteryPancake Sep 27 '18 at 06:35
  • Also, at least on OSX, `find ~/` targets all files in the user's folder, rather than just searching the current directory. It might be worth removing this just in case someone makes the same mistake that I did – MysteryPancake Sep 27 '18 at 10:32
  • I noticed you mentioned you have lost some files as well, as you stated "thank god for backups". I must say that this answer may not be particularly helpful for people such as myself without backups. – MysteryPancake Sep 27 '18 at 10:33
  • 1
    @MysteryPancake on two separate occasions in the above post, I warned readers about the extreme risks of automating use of the `rm` command. I then go on to post a version of the answer which doesn’t delete any files. It is not my responsibility to ensure that you read the entirety of an answer before you “hastily copy” any code from it. The code supplied answers the question which was asked and for this reason it will stay the accepted answer. I hope you’ve learned a very valuable lesson. – E.Owen Sep 27 '18 at 20:53
  • Regardless of which version is used, the user folder `~/` is searched rather than the current directory. I think it would be at least useful to alter this to search the correct directory, as the OP never mentions wanting to search their entire user folder rather than just the folder `/Videos`. – MysteryPancake Sep 28 '18 at 01:39
  • For someone such as myself who has never used the `find` command before, I expected both answers to only search the current directory, which contained nothing but disposable files. It is never mentioned anywhere that `~/` searches the user folder, which I only found out after deletion. I can't think of a single reason for anyone wanting to permanently wipe all the files from their account. – MysteryPancake Sep 28 '18 at 02:13
  • @MysteryPancake None of this changes the fact that you could have avoided all of this by reading the answer in the first place. To search in the current directory, you can use `find ./` – E.Owen Sep 28 '18 at 08:33
  • This is true, I know I could have avoided a big mess by reading the answer properly, but I'm trying to emphasise that the answer doesn't mention that `~/` searches the user folder. Even if I used the second command, there would still be lots of converted files outside the intended folder. The OP doesn't mention wanting to search the user folder, so I think it might be a good idea to change `~/` to `./` in case anyone else uses this command. – MysteryPancake Sep 28 '18 at 10:18
  • I have made a small edit to this answer, just to move the non-destructive option to the top, and change the search operator to `./`. I feel that this will help anyone testing bits of code as I was, and if they read the post properly as I should have, they have the option to permanently remove the files if this behaviour is wanted. It doesn't make much of a difference, but I think that the destructive option should always come second, especially as it has deleted some of your files as well. Sorry about all this annoying nonsense, I'm just trying to help. – MysteryPancake Sep 28 '18 at 10:39