Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Friday, 6 October 2017

Querying video file metadata with mediainfo

I am working on a script that will query media files (mp4/mkv videos) to obtain metadata that can be subsequently used to rename the file to enforce a naming convention. I use the excellent mediainfo tool (available in the standard repositories) to do this.

mediainfo has a metric tonne of options and functions that you can use for various purposes. In my case I want to know the aspect ratio, vertical height and video codec for the file. This can be done in a single command;

mediainfo --Inform="Video;%DisplayAspectRatio%,%Height%,%Format%"

This works fine and returns something like this;

1.85,720p,AVC

When I say it works fine I mean it works fine in 99% of cases. The other 1% are made up of files that contain more than one video stream. Sometimes people package a JPEG image inside the container which is designated internally as "Video#2". In such cases the above command will also return values relating to the JPEG image producing something like this;

1.85,720p,AVC1.85,720p,JPEG

When this happens my script breaks. The workaround for that is to pipe the results through some unix tools to massage the output;

mediainfo --Inform="Video;%DisplayAspectRatio%,%Height%,%Format%\n" "${_TARGET}" | xargs | awk '{print $1;}'

Things to note in the revised command. There is a carriage return ("\n") at the end of the --Inform parameters which will put the unwanted data on a new line like this;

1.85,720p,AVC
1.85,720p,JPEG

xargs will remove that line feed and replace it with a space;

1.85,720p,AVC 1.85,720p,JPEG

And finally awk will produce only the first "word" (space delimited) from the result, which produces the desired output.

1.85,720p,AVC

Now obviously this method assumes that the first video stream in the container is the one we are interested in. I'm struggling to imagine a scenario where this would not be the case so at this point I am OK with that. If I find a file that doesn't work I might have to revise my script, but for now I will stick with this solution.

Saturday, 9 April 2011

Adding to system PATH for all users

I have a few scripts that I use to do certain things on my machines. Rather than copy them to /usr/sbin or similar I like to keep them segregated in a "scripts" directory. In this way I can copy them to new machines or keep them up to date using rsync without messing up the standard scripts that ship with Debian/Ubuntu.

To make the scripts executable for users without typing in the full path to the scripts directory you can edit the users bash profile file ".bashrc" and add the path there. However, this gets messy real fast when dealing with lots of users, so we want a way to update the path for every user that logs in. We do this by putting a script in the /etc/profiles.d directory
Note: Scripts in /etc/profile.d are executed whenever a user logs in to a bash session.

Create a file in /etc/profile.d.
sudo vi /etc/profile.d/addpath.sh

Add this line;

PATH=$PATH:/usr/scripts

Log out and log in again and check to see if your path has been updated;

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/scripts
Enjoy!