I am converting an entire folder of videos to MP4. The script works except that the new videos have ".mp4" attached twice to them. For example. 'video.mp3' would be 'video.mp3.mp4' after conversion. Below is the shell script. TIA

#!/bin/bash
#Shell Script which converts all videos in a folder to MP4


for file in *.*; 
    do 
        if[ ${file: -4} != ".mp4"]   #don't want to convert mp4 files           
            ffmpeg -i "$file" "${file}".mp4 
done
share|improve this question
up vote 3 down vote accepted

This will strip the last file extension: ${file%.*}

So you'd want ${file%.*}.mp4

Here is a good reference for string manipulation in bash: http://tldp.org/LDP/abs/html/string-manipulation.html

share|improve this answer
    
This solution works. The reference is helpful for anyone learning shell scripting. – Avi Aug 25 '15 at 23:07

First of all, the way you have written it, a file named test.mp3 would be converted to an mp4 file named test.mp3.mp4. This is because your "file" variable contains the extension - as you would have to know, since you're checking for the extension .mp4 in the if statement.

You are using single brackets for your if statement, so it's possible that is failing ... I'm not entirely sure how you are managing to successfully convert any files with this script.

Try making a few modifications based off my feedback and see if you can get it to work.

I am choosing not to share the code I wrote, in the hopes that you may genuinely learn from tweaking what you have now to work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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