Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't ${1:r} strip file extension in my zsh script?

Tags:

zsh

I'm writing a zsh script that automates some video encoding. The file to be converted will be the first parameter to the script, which contains this line:

ffmpeg -i $1 -ac 0 -vcodec libx264 -b:v 500k -crf 20 ${1:r}.mp4

I expected ${1:r}.mp4 to create a file with the same name as my .mov input, but with the .mp4 file extension replacing .mov. But instead I get input.mov.mp4. Why isn't the :r modifier stripping the file extension?

like image 345
tommaisey Avatar asked Apr 04 '16 14:04

tommaisey


People also ask

How do I remove extension from filename in Shell?

%. * will only remove the last extension; if you want to remove all the extensions, use %%. * .

How do I get filenames without an extension in Unix?

`basename` command is used to read the file name without extension from a directory or file path. Here, NAME can contain the filename or filename with full path. SUFFIX is optional and it contains the file extension part that the user wants to remove. `basename` command has some options which are described below.


1 Answers

I experienced something similar and it seems like zsh doesn't accept :r on the positional arguments. Instead you could do something like this:

file=${1}
ffmpeg -i $1 -ac 0 -vcodec libx264 -b:v 500k -crf 20 ${file:r}.mp4
like image 158
Peter Hoeg Avatar answered Sep 21 '22 12:09

Peter Hoeg