0

For the life of me, I cannot figure out why I can't store the output of the mediainfo --Inform command into an array. I've done for loops in Bash before without issue, perhaps I'm missing something really obvious here. Or, perhaps I'm going about it the completely wrong way.

#!/bin/bash

for file in /mnt/sda1/*.mp4

  do vidtime=($(mediainfo --Inform="Video;%Duration%" $file))

done

echo ${vidtime[@]}

The output is always the time of the last file processed in the loop and the rest of the elements of the array are null.

I'm working on a script to endlessly play videos on a Raspberry Pi, but I'm finding that omxplayer isn't always exiting at the end of a video, it's really hard to reproduce so I've given up on troubleshooting the root cause. I'm trying to build some logic to kill off any omxplayer processes that are running longer than they should be.

ErichL
  • 3
  • 2

2 Answers2

0

Give this a shot. Note the += operator. You might also want to add quotes around $file if your filenames contain spaces:

#!/bin/bash

for file in /mnt/sda1/*.mp4

  do vidtime+=($(mediainfo --Inform="Video;%Duration%" "$file"))

done

echo ${vidtime[@]}
brianlq
  • 14
  • That fixed it, thanks! Why is the += operator required in this instance? I don't see it used on any of the examples that I've referenced in the past. – ErichL Jul 01 '14 at 14:25
  • Since you're iterating through the mediainfo commands on the set of files one at a time, your vidtime array is being constantly reset, which would explain why you're only able to see the last file in your array. – brianlq Jul 02 '14 at 21:41
0

It's more efficient to do it this way:

read -ra vidtime < <(exec mediainfo --Inform='Video;%Duration% ' -- /mnt/sda1/*.mp4)

No need to use a for loop and repeatingly call mediainfo.

konsolebox
  • 72,135
  • 12
  • 99
  • 105