Tuesday, February 23, 2010

Rotate video

No weird title for this one. No, this one was 100% purely itch-scratch oriented, and written in under under two hours.

Problem: I've been bowling and I made some short movies with my cell phone, but I recorded them horizontally instead of vertically. I basicaly forgot that, unlike with pictures, when you watch the movie you can't just press the rotation arrow in the movie player to turn it vertical or whatever.

Thus, a little zenity-enabled script around mencoder to convert these ineptly taken movies.

The code:
 
1 #!/bin/bash
2 #
3 # Rotate video
4 #
5 # A short and handy way to rotate videos, one file at a time (sorry).
6 #
7 # Usage:
8 # rotate_video [INPUT [OUTPUT [ROTATION]]]
9 #
10 # (If no arguments are given, the script will show appropriate dialogs)
11 #
12 # Requires:
13 # mencoder
14 # zenity
15 #
16 # Author:
17 # Konrad Siek <konrad.siek@gmail.com>
18 #
19 # License information:
20 #
21 # This program is free software: you can redistribute it and/or modify
22 # it under the terms of the GNU General Public License as published by
23 # the Free Software Foundation, either version 3 of the License, or
24 # (at your option) any later version.
25 #
26 # This program is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 # GNU General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #
34 # Copyright 2010 Konrad Siek
35
36 # Auxiliary shorthand for rotating:
37 # rotate INPUT OUTPUT ROTATION
38 function rotate() {
39 mencoder -vf rotate="$3" -o "$2" -oac copy -ovc lavc "$1"
40 }
41
42 CCW_90="90°"
43 CW_90="90°"
44 ROT_180="180°"
45
46 case "$#" in
47 0) # Interactive mode
48 file=$(zenity \
49 --file-selection \
50 --title="Select a video to rotate" \
51 --separator="\n" \
52 )
53 if [ -f "$file" ]
54 then
55 output=$(basename "$file")
56 extension="${output#*.}"
57 output="$(dirname "$file")/${output%%.*}"
58 rotation=$(zenity --list \
59 --title="Specify rotation angle" \
60 --column="Rotation angle" \
61 "$CW_90" "$CCW_90" \
62 )
63 case "$rotation" in
64 "$CW_90")
65 rotate "$file" "$output$CW_90.$extension" 1
66 exit 0
67 ;;
68 "$CCW_90")
69 rotate "$file" "$output$CCW_90.$extension" 2
70 exit 0
71 ;;
72 *)
73 zenity --error --text="No rotation specified."
74 exit 3
75 ;;
76 esac
77 else
78 zenity --error --text "Cannot open file '$file'."
79 exit 2
80 fi
81 ;;
82 1) # Just filename
83 output=$(basename "$1")
84 extension="${output#*.}"
85 output="$(dirname "$1")/${output%%.*}"
86 rotate "$1" "$output$CW_90.$extension" 1
87 ;;
88 2) # Two filenames
89 rotate "$1" "$2" 1
90 ;;
91 3) # Two filenames and rotation spec
92 case "$3" in
93 cw) rotate "$1" "$2" 1;;
94 ccw) rotate "$1" "$2" 2;;
95 *)
96 echo "Unknown option '$3'." >&2
97 echo "Try 'cw', 'ccw' or nothing instead." >&2
98 ;;
99 esac
100 ;;
101 *) # Anything else - show usage
102 echo "Usage:" >&2
103 echo -e "\t$0 [INPUT [OUTPUT [cw|ccw]]]" >&2
104 exit 1
105 ;;
106 esac


The code is also available at GitHub as bash/rotate_video.

No comments: