Search code examples
arraysbashshellraspbianmediainfo

Trouble storing the output of mediainfo video times into an array


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.


Solution

  • 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[@]}