Showing posts with label zenity. Show all posts
Showing posts with label zenity. Show all posts

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.

Friday, September 18, 2009

Zentube

Another variation on the theme of zenity. I honestly like the way you can make simple front-ends. In addition, I'm doing something with youtube again, or more precisely, I'm doing stuff with youtube-dl.

So, the problem with youtube is that if you don't have Internet access, you obviously can't really use it, and there are certain instances where it'd come in handy. One such instance is when you're doing language teaching in an Internet-bereft classroom.

So there's youtube-dl to get some videos downloaded, but a person is not always in the mood for fiddling with the shell when preparing their lesson material.

Hence, this script provides the simples of interfaces to download videos via youtube-dl. That's pretty much it. Anyway, I think it's simple and does its job.

Oh, yeah, I played around with the idea of automatically installing a package if it is not available at the time of execution. It's a sort of experiment, to see if it can be done at all. I'm not sure how effective this is though. And it depends on apt and gksudo.

The code:
 
1 #!/bin/bash
2 #
3 # Zentube
4 #
5 # A simple GUI front-end to youtube-dl. All you need to do is run it,
6 # and put in the address of the video, and the back-end tries to
7 # download the video.
8 #
9 # Parameters:
10 # None
11 #
12 # Requires:
13 # youtube-dl
14 # zenity
15 # gksudo & apt (if you want youtube-dl installed automatically)
16 #
17 # Author:
18 # Konrad Siek <konrad.siek@gmail.com>
19 #
20 # License:
21 #
22 # Copyright 2008 Konrad Siek.
23 #
24 # This program is free software: you can redistribute it and/
25 # or modify it under the terms of the GNU General Public
26 # License as published by the Free Software Foundation, either
27 # version 3 of the License, or (at your option) any later
28 # version.
29 #
30 # This program is distributed in the hope that it will be
31 # useful, but WITHOUT ANY WARRANTY; without even the implied
32 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
33 # PURPOSE. See the GNU General Public License for more
34 # details.
35 #
36 # You should have received a copy of the GNU General Public
37 # License along with this program. If not, see
38 # <http://www.gnu.org/licenses/>.
39
40 # The downloader backend.
41 PACKAGE=youtube-dl
42
43 # Output information.
44 OUTPUT_DIR=~/Videos/
45 EXTENSION=.flv
46 TEMP_FILE=/tmp/$(basename $0).XXXXXXXXXX
47
48 # The quality of the output file can be adjusted here, or you can comment
49 # out this setting altogether, to get the default.
50 QUALITY=--best-quality
51
52 # Exit code constants.
53 SUCCESS=0
54 INSTALLATION_ABORTED=1
55 INSTALLATION_FAILED=2
56 INVALID_VIDEO_ADDRESS=4
57 INVALID_OUTPUT_DIRECTORY=8
58 BACKEND_ERROR=16
59
60 # This is a convenience installer for apt-using distros, e.g. Ubuntu.
61 if [ -z "$(which $PACKAGE)" ]
62 then
63 # Ask whether to attempt automatic install of the missing package.
64 # If the answer is no, then quit with an error.
65 zenity --question \
66 --title="Automatic installation" \
67 --text="Can't find <b>$PACKAGE</b>. Should I try installing it?" \
68 || exit $INSTALLATION_ABORTED
69
70 # Try installing the missing package, or quit with an error if the
71 # attempt is failed.
72 gksudo "apt-get install $PACKAGE" || exit $INSTALLATION_FAILED
73 fi
74
75 # Ask user for the URL of the video.
76 url=$(\
77 zenity --entry \
78 --title="Video address" \
79 --text="What is the address of the video?" \
80 )
81 # If no URL is given, then quit.
82 [ -z "$url" ] && exit $INVALID_VIDEO_ADDRESS
83
84 # Move to the output directory, create it i necessary.
85 mkdir -p "$OUTPUT_DIR" || exit $INVALID_OUTPUT_DIRECTORY
86 cd "$OUTPUT_DIR"
87
88 # Make a temporary file to collect error messages from the downloader.
89 temp_file=$(mktemp $TEMP_FILE)
90
91 # Run the downloader.
92 $PACKAGE $QUALITY --title "$url" 2>"$temp_file" | \
93 zenity --progress --pulsate --auto-kill --auto-close --text="Downloading..."
94
95 # Check for errors, and display a success of error dialog at the end.
96 errors=$(cat $temp_file)
97
98 if [ -z "$errors" ]
99 then
100 # Display successful info.
101 zenity --info --text="Download successful!"
102
103 # Remove temporary file.
104 unlink "$temp_file"
105
106 # Exit successfully.
107 exit $SUCCESS
108 else
109 # Display error dialog.
110 zenity --error --text="$errors"
111
112 # Remove temporary file.
113 unlink "$temp_file"
114
115 # Exit with an error code.
116 exit $BACKEND_ERROR
117 fi


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

Wednesday, September 16, 2009

Zenspeak

You can give this one to children. It makes them noisier.

This one's just a simple interface to either espeak or festival: it asks you what to say via zenity and then says it. It doesn't take any arguments, so you start it up with a simple:

./zenspeak

In summary, it's not exactly dragon magic.

The code:
 
1 #!/bin/bash
2 #
3 # Zenspeak
4 #
5 # Provides a simple graphical (Gtk) interface to a speech production system:
6 # either espeak or festival. It's really simple too: you put in some text,
7 # the text is spoken. When you put in zero text, the program ends.
8 #
9 # Parameters:
10 # None
11 #
12 # Depends:
13 # espeak (apt:espeak)
14 # festival (apt:festival)
15 # zenity (apt:zenity)
16 #
17 # Author:
18 # Konrad Siek <konrad.siek@gmail.com>
19 #
20 # License information:
21 #
22 # This program is free software: you can redistribute it and/or modify
23 # it under the terms of the GNU General Public License as published by
24 # the Free Software Foundation, either version 3 of the License, or
25 # (at your option) any later version.
26 #
27 # This program is distributed in the hope that it will be useful,
28 # but WITHOUT ANY WARRANTY; without even the implied warranty of
29 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 # GNU General Public License for more details.
31 #
32 # You should have received a copy of the GNU General Public License
33 # along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #
35 # Copyright 2009 Konrad Siek
36
37 # System for production of sound is selected by the parameter,
38 # or the defaut is used if none were specified.
39 SYSTEM_DEFAULT=espeak
40 SYSTEM=`(( $# == 0 )) && echo "$SYSTEM_DEFAULT" || echo "$1"`
41 echo $SYSTEM
42
43 # System dependent settings for espeak:
44 espeak_speed=120 # default: 160
45 espeak_pitch=60 # 0-99, default: 50
46 espeak_amplitude=20 # 0-20, default: 10
47 espeak_voide=english # list of voices: `espeak --voices`
48 espeak_variant=f2 # m{1,6}, f{1,4}
49
50 # I'm completely pants when it comes to setting up festival, so I won't
51 # even attempt it here.
52
53 while true
54 do
55 # Show dialog and get user input.
56 something=`zenity --entry --title="Say something..." --text="Say:"`
57
58 # If no user input or cancel: bugger off (and indicate correct result).
59 if [ -z "$something" ]
60 then
61 exit 0
62 fi
63
64 # Put the input through either espeak or festival.
65 if [ "$SYSTEM" == "espeak" ]
66 then
67 # Note: the sound is padded within pulse, so that it can be
68 # played simultaneously with other sources.
69 padsp espeak \
70 -a $espeak_amplitude \
71 -p $espeak_pitch \
72 -s $espeak_speed \
73 -v $espeak_voice+$espeak_variant \
74 "$something"
75 elif [ "$SYSTEM" == "festival" ]
76 then
77 # Incidentally, that's all I know about festival.
78 echo "$something" | festival --tts
79 fi
80 done


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

Monday, April 13, 2009

Convert file encodings

Here's to a new bit of code!

I wrote this program once, called Tester, which my dad is using in his day-to-day language teaching.

I wrote the thing quite a long time ago, so it's a bit buggy here and there, and one of the problems is that it doesn't handle encodings well. Since dad is thinking about using Ubuntu, the mishandled encodings are a problem: he used to have them encoded as WINDOWS-1250, but the current settings allow him to use only UTF-8 on Ubuntu.

Hence, this program, to convert between the two. Since the files come in bulk, I figured doing entire directories at once is a good idea.

Also, I wrote it in about an hour and a bit, so it might be a bit buggy here and there...

Here's the code:
 
1 #!/bin/bash
2
3 # Convert directory
4 #
5 # Convert text files in the directory from one encoding to another,
6 # all in a simple GUI. Converts between UTF-8 and WINDOWS-1250.
7 #
8 # Requires:
9 # zenity
10 # iconv
11 # Author:
12 # Konrad Siek
13
14
15 # Get directory to convert files in
16 directory=$(\
17 zenity \
18 --file-selection \
19 --directory \
20 --title="Select a directory to convert" \
21 )
22
23 # If none selected, quit.
24 if [ $? != 0 ]
25 then
26 exit 1
27 fi
28
29 # Select source encoding from a list.
30 source_encoding=$(\
31 zenity --list \
32 --column="Encoding" \
33 --title="Source encoding" \
34 --text="The files are currently encoded as... " \
35 WINDOWS-1250 UTF-8 \
36 )
37
38 # If none selected, quit.
39 if [ $? != 0 ]
40 then
41 exit 1
42 fi
43
44 # Select destination encoding from a list.
45 destination_encoding=$(\
46 zenity --list \
47 --column="Encoding" \
48 --title="Destination encoding" \
49 --text="And you want these files encoded as... " \
50 UTF-8 WINDOWS-1250 \
51 )
52
53 # If none selected, quit.
54 if [ $? != 0 ]
55 then
56 exit 1
57 fi
58
59 # For all files in the selected directory...
60 find "$directory" -type f | while read f
61 do
62 # Get information about the file.
63 extension=${f#*.}
64 basename=$(basename "$f" ".$extension")
65 addition=$(echo "$destination_encoding" | tr -d - | tr [A-Z] [a-z])
66 output="$directory/$basename.$addition.$extension"
67
68 # Convert encoding.
69 iconv \
70 --from-code="$source_encoding" \
71 --to-code="$destination_encoding" \
72 --output="$output" \
73 "$f"
74
75 echo "Created $directory/$basename.$addition.$extension"
76 done
77
78 # Notify on finish
79 zenity --info --text="Operation complete." --title="Complete"
80


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