3

I made a simple bash script to convert movies from .avi to .mp4 but now I want to do two more things.

  1. Have the output file have only *.mp4 rather than *.avi.mp4
  2. Delete the old file after the conversion is completed.

Here is my script.

#!/bin/bash avconv -i "$1" -c:v libx264 -c:a copy -sn -crf 24 "$1".mp4 

2 Answers 2

3
  1. You should do ${1%.avi}.mp4, where we tell bash to print the contents of $1 with the .avi suffix removed, and then append the .mp4 suffix.
  2. I would suggest running aconv ... && rm "$1", in order to only delete if conversion succeeds, because the && operator only executes the command that follows it if the previous command succeeds.

Final version:

#!/bin/bash avconv -i "$1" -c:v libx264 -c:a copy -sn -crf 24 "${1%.avi}.mp4" && rm "$1" 

Hope this helps =)

Sign up to request clarification or add additional context in comments.

Comments

2

How about:

avconv -i "$1" -c:v libx264 -c:a copy -sn -crf 24 "${1/.avi/.mp4}" && rm "$1" 

UPDATE

In response to the comment, the syntax for search and replace is:

${1/oldstring/newstring} 

So, you can use it any way you want.

1 Comment

Thank you very much. If I had a file that was a *.mkv and I wanted to subtract that, would I then do this? 'avconv -i "$1" -c:v libx264 -c:a copy -sn -crf 24 "${1/.avi/.mkv}.mp4" && rm "$1"'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.