Mat's Playground

ogg.displayTags

Description

This script utilizes vorbiscomment to retrieve the ogg tags nicely formatted.

Sample output:

% ogg.displayTags 08_when_death_takes_its_dominion.ogg
ALBUM   = Enemy Of God
ARTIST  = Kreator
DATE    = 2005
GENRE   = Metal
TITLE   = When Death Takes It's Dominion
TRACKNR = 08

Calling the script without any parameters, will display usage information.

Required program

Download

Source code

Hide line numbers Expand lines
  1#!/bin/sh
  2# the next line restarts using tclsh \
  3exec tclsh "$0" "$@"
  4
  5
  6#
  7# required programs:
  8#   vorbiscomment
  9#
 10
 11
 12
 13## no parameters; display help and exit
 14if {[llength $argv] == 0} then {
 15
 16    puts "Usage: [file tail $argv0] \[OPTION\]... \[FILE\]"
 17    puts "Display tags of the given ogg vorbis file"
 18    puts ""
 19    puts "  --album\tDisplay the file's album"
 20    puts "  --artist\tDisplay the file's artist"
 21    puts "  --comment\tDisplay the file's comment"
 22    puts "  --date\tDisplay the file's date"
 23    puts "  --genre\tDisplay the file's genre"
 24    puts "  --title\tDisplay the file's title"
 25    puts "  --tracknr\tDisplay the file's track number"
 26    puts ""
 27    puts "  --nohead\tOmit the heading and display only the value"
 28    puts "\n  * if no parameters are given, all available information will be shown *\n"
 29
 30    exit 1
 31}
 32
 33## last parameter is the file, test whether it exists
 34set file [lindex $argv end]
 35
 36if {![file exists $file]} then {
 37    puts "file \"$file\" does not exist"
 38    exit 1
 39}
 40
 41## and whether it is readable
 42if {![file readable $file]} then {
 43    puts "file \"$file\" is not readable"
 44    exit 1
 45}
 46
 47## is it even an ogg file
 48if {[file extension $file] != ".ogg"} then {
 49    puts "file \"$file\" is not an ogg vorbis file"
 50    exit 1
 51}
 52
 53
 54## read all ogg tags into an array
 55set tmp [exec vorbiscomment -l $file] ;# is empty, if there are no tags
 56set values ""
 57
 58if {$tmp != ""} then {
 59
 60    foreach line [split $tmp \n] {
 61        set line [split $line =]
 62
 63        set tag [lindex $line 0]
 64        set val [join [lrange $line 1 end] =]
 65
 66        if {[string tolower $tag] == "tracknumber"} then {
 67            set tag "tracknr"
 68        }
 69
 70        lappend values $tag $val
 71        unset line tag val
 72    }
 73
 74}
 75array set tags $values
 76unset tmp values
 77
 78
 79## which infos should be displayed
 80set display {}
 81
 82foreach item {album artist comment date genre title tracknr} {
 83    set i [lsearch $argv "--$item"]
 84    if {$i > "-1"} then {
 85        lappend display $item
 86    }
 87    unset i item
 88}
 89
 90
 91
 92## no parameters given, display all infos
 93if {[llength $display] == 0} then {
 94    set display {album artist comment date genre title tracknr}
 95}
 96
 97
 98
 99## display the requested infos
100set nohead [lsearch $argv "--nohead"]
101
102foreach item $display {
103
104    catch {set val $tags($item)}
105
106    if {![info exists val]} then {
107        # try again with uppercase
108        catch {set val $tags([string toupper $item])}
109    }
110
111    if {[info exists val]} then {
112
113        if {$nohead == -1} then {
114            puts "[string toupper $item]\t= $val"
115        } else {
116            puts $val
117        }
118
119        unset val
120    }
121
122    unset item
123}
124
125unset display file
126array unset tags
127
128exit 0