0

Currently, I have a simple script that will detect video audio codec using ffmpeg and output a message specifing if the codec is mp4 or aac. Here is my code:

#!/bin/bash
for i in *.mp4; do
    OUTPUT=$(ffmpeg -i '$i' 2>&1 | grep -o 'Audio:.*' | cut -f2 -d' ' | awk '{print $0}' | tr -d ,)
    if [[ $OUTPUT == "mp3" ]]; then
        echo "${i} = audio codec mp3"
    fi
    if [[ $OUTPUT == "aac" ]]; then
        echo "${i} = audio codec aac"
    fi
done

But this script isn't working! There are no errors and I don't know what should I do to solve this. Also, ffmpeg -i <input.mp4> 2>&1 | grep -o 'Audio:.*' | cut -f2 -d' ' | awk '{print $0}' | tr -d , returns aac or mp3, this line is working perfeclty! I guess that the problem is related to if case, but as I've said, I'm unable to solve this alone, can you help me?

Thank you

Karlna
  • 67
  • 2
  • 11
  • 1
    Replace the single quotes surrounding `$i` with double quotes in the `ffmpeg` line. – tshiono Nov 07 '18 at 01:23
  • As an aside, if you are using Awk anyway, you [might as well get rid of `grep` and `cut` and `tr`.](http://www.iki.fi/era/unix/award.html#grep) Also, don't use uppercase for your private variables, and probably don't capture the result into a variable at all. `case $(ffmpeg -i '$i' 2>&1 | awk '/Audio:/ { a =$1; sub(/,/, "", a); print a }') in mp3) echo mp3;; ... esac` – tripleee Nov 07 '18 at 06:15

1 Answers1

0

if [[ $OUTPUT == "mp3" ]]; then
if [[ $OUTPUT == "aac" ]]; then


Replace this with

if [[ "$OUTPUT" = "mp3" ]]; then
if [[ "$OUTPUT" = "aac" ]]; then

Vinod Kumar
  • 562
  • 5
  • 12