Trouble with Command Substitution in Bash Script for FFmpeg

0
3
Asked By ScribbleBot3000 On

I'm new to bash scripting and I'm trying to write a script that uses FFmpeg to convert .ts files to .mp4. I want to assign the name of the new .mp4 file to a variable so I can run a Filebot CLI command on it later. The FFmpeg command finishes correctly and gives me the .mp4 file I expect, but when I try to use that variable in the Filebot command, it says there's no input file. Here's my current script:

```bash
#!/usr/bin/env zsh
if [[ "${1##*.}" == "ts" ]] ; then
converted_episode="$(ffmpeg -i "$1" "${1%.*}.mp4")"
else
fi
/Applications/FileBot.app/Contents/MacOS/filebot.sh -rename $converted_episode
```

What could be going wrong here? Thanks for any advice!

3 Answers

Answered By TechSavvyNerd On

It looks like you're accidentally capturing the output of the `ffmpeg` command instead of the filename. You should assign the new file name to `converted_episode` first, then use that variable in the `ffmpeg` command like this:

```bash
converted_episode="${1%.*}.mp4"
ffmpeg -i "$1" "$converted_episode"
```
This way, you're ensuring that `converted_episode` contains the name of the new file, not the command output.

Answered By CodeWhisperer42 On

Make sure you're aware that the output from `ffmpeg` could be going to standard error, which you may not be capturing. Run your command manually to see exactly what `ffmpeg` outputs. You can also do:

```bash
ffmpeg -i testfile.ts testfile.mp4 2> /dev/null
```
This can help confirm what’s being stored in `converted_episode`.

Answered By BikesAndBytes On

You might also want to check the quotes in your assignment. It's generally cleaner to separate the variable assignment from the command itself to avoid issues with nested quotes. Just move the assignment out of the command like I suggested above.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.