How to get video dimensions (width x height) using command line on Mac and Linux

Category : How to, Scripts

I’ve been searching about how to get video dimensions (width x height) in order to put my HD movies in one folder and non-hd movies in another folder. I didn’t want to do this manually, so the best solution would be to create a bash script that would do the job for me.

To do that, the first step is to detect the video dimensions. Basically you will need mplayer in your PATH and also use the grep and sed commands. So, to get a video’s width, the command is:

mplayer -vo null -ao null -frames 0 -identify mymovie.avi 2>/dev/null | grep –color=never ‘^ID_VIDEO_WIDTH=[.0-9]*’ | sed -e ‘s/ID_VIDEO_WIDTH=//g’

This command is divided into 3 parts. The first part (green), uses mplayer to get some information about the video. The second (red) part makes use of grep to get the line related video’s width. Finally, the third part (blue), removes the unecessary labels and gets only the number of pixels of movie’s width. At the end, you will receive only an integer value which represents the movie’s width.

To the the height, the process is the same, only replacing width by height.

mplayer -vo null -ao null -frames 0 -identify mymovie.avi 2>/dev/nullgrep –color=never ‘^ID_VIDEO_HEIGHT=[.0-9]*’sed -e ‘s/ID_VIDEO_HEIGHT=//g’

And that’s it. Now you have the width and height of a movie.

Popularity: 21% [?]

Simple bash script to create thumbnails from images

1

Category : Scripts

Hi, this is a script compatible with both mac and linux to generate thumbnails of images contained in a specific folder. It works with JPG and PNG files.

Here is the source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash

# Copyright 2010 Marcelo Carlos
#
# createThumbnails is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# createThumbnails is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# Check http://www.gnu.org/licenses/ for full GNU General Public License details

CONVERT="$(which convert)"

if [ $# -lt 3 ]; then
    echo
    echo "Usage: createThumbnails <source-folder> <max-width> <max-height>"  
    echo   
    exit 1
else
    SOURCE_FOLDER=$1
    MAX_W=$2
    MAX_H=$3   
    SEPARATOR="x"  
    CONVERTED=false
   
    if [ ! -x "$CONVERT" ]; then
        echo "ERROR: convert not installed" >&2
        exit 2
    fi
   
    for img in `ls *.jpg 2> /dev/null`
    do
        convert -sample $MAX_W$SEPARATOR$MAX_H $img thumb-$img
        CONVERTED=true
    done

    for img in `ls *.jpeg 2> /dev/null`
    do
        convert -sample $MAX_W$SEPARATOR$MAX_H $img thumb-$img
        CONVERTED=true
    done

    for img in `ls *.png 2> /dev/null`
    do
        convert -sample $MAX_W$SEPARATOR$MAX_H $img thumb-$img
        CONVERTED=true
    done
   
    if [ $CONVERTED == true ]; then
        echo "Done"
    else
        echo "Nothing to be done. No JPG or PNG file(s) found" 
    fi
fi

Alternatively, you can download the script clicking here

I hope you like it. If you have suggestions or questions, please comment!

Popularity: 11% [?]