Bash Script: Append .720p or .1080p to all videos in a directory

Status
Not open for further replies.

Peter South

Active Member
607
2013
360
1,690
This script will run through all video files in a directory and rename the ones with a resolution height larger than 700 pixels.

Example:
Lets pretend I have two files in ~/My Videos/, one is an HD 1080p mp4 file and the other is a SD avi file:
Code:
root@Ubuntu:~/My Videos/# ls
My.Video.1.mp4
My.Video.1.avi
root@Ubuntu:~/My Videos/#

After running this script the same files will be renamed as such:
Code:
root@Ubuntu:~/My Videos/# ls
My.Video.1.1080p.mp4
My.Video.1.avi
root@Ubuntu:~/My Videos/#

Please note: This simple bash script requires the mediainfo package on Ubuntu 12.04LTS+. To install mediainfo simply type: apt-get install mediainfo -y;.

Code:
#! /bin/bash
for file in ./*; do
basename=`basename $file | rev | cut -c4- | rev`;
ext=`ls $file | rev | cut -d'.' -f1 | rev`;
height=`mediainfo $file | grep Height | awk '{print $3}'`;
if [[ $height -gt "700" && $height -lt "1000" ]]; then
  echo "Renaming "$file" to "$basename720p.$ext;
  mv $file $basename720p.$ext;
fi;
if [[ $height == "1" ]]; then
  echo "Renaming "$file" to "$basename1080p.$ext;
  mv $file $basename1080p.$ext;
fi;
done;

For more of my scripts see: Index of various Bash Script Threads contributed by Eddie Johnson.
 
Status
Not open for further replies.
Back
Top