Mat's Playground

ogg.writeTags

Description

Obviously, this script does the exact opposite of ogg.displayTags. It writes ogg information into files.

Call this script without parameters to get 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# needed program: vorbiscomment
  8#
  9
 10
 11
 12## no parameters; display help and exit
 13if {[llength $argv] == 0} then {
 14    puts "Usage: [file tail $argv0] \[OPTION\]... \[FILE\]"
 15    puts "Modify tags of an ogg vorbis file"
 16    puts ""
 17    puts "  --album <album>\tSpecify the file's album tag"
 18    puts "  --artist <artist>\tSpecify the file's artist tag"
 19    puts "  --comment <comment>\tSpecify the file's comment tag"
 20    puts "  --date <date>\t\tSpecify the file's date tag"
 21    puts "  --genre <genre>\tSpecify the file's genre tag"
 22    puts "  --title <title>\tSpecify the file's title tag"
 23    puts "  --tracknr <tracknr>\tSpecify the file's tracknumber"
 24    puts "\n  * Warning: Already existing information will be deleted *\n"
 25
 26    exit 1
 27}
 28
 29
 30## last parameter is the file, test whether it exists
 31set file [file normalize [lindex $argv end]]
 32
 33if {![file exists $file]} then {
 34    puts "file \"$file\" does not exist"
 35    exit 1
 36}
 37
 38## and whether it is writable
 39if {![file writable $file]} then {
 40    puts "file \"$file\" is not writable"
 41    exit 1
 42}
 43
 44## is it even an ogg file
 45if {[file extension $file] != ".ogg"} then {
 46    puts "file \"$file\" is not an ogg vorbis file"
 47    exit 1
 48}
 49
 50
 51## create tag array
 52array set tag { "album" "" "artist" "" "comment" "" "date" "" "genre" "" "title" "" "tracknr" "" }
 53
 54
 55## look for specified tags
 56set nothingFound 1
 57foreach item {album artist comment date genre title tracknr} {
 58
 59    set i [lsearch $argv "--$item"]
 60    if {$i > "-1"} then {
 61        set val [lindex $argv [expr $i + 1]]
 62        if {[string length $val] > 0} then {
 63            array set tag [list $item $val]
 64            set nothingFound 0
 65        }
 66        unset val
 67    }
 68
 69    unset i item
 70}
 71
 72
 73
 74## nothing to do
 75if {$nothingFound} then {
 76    puts "No tags specified, nothing to do.."
 77    exit 1
 78}
 79
 80unset nothingFound
 81
 82
 83## write ogg tag
 84set cmd ""
 85
 86foreach item {album artist comment date genre title tracknr} {
 87    set $item [lindex [array get tag $item] 1]
 88    unset item
 89}
 90
 91set cmd "vorbiscomment -w \"$file\""
 92
 93if {$album   != ""} then {set cmd "$cmd -t \"ALBUM=$album\""}
 94if {$artist  != ""} then {set cmd "$cmd -t \"ARTIST=$artist\""}
 95if {$comment != ""} then {set cmd "$cmd -t \"COMMENT=$comment\""}
 96if {$date    != ""} then {set cmd "$cmd -t \"DATE=$date\""}
 97if {$genre   != ""} then {set cmd "$cmd -t \"GENRE=$genre\""}
 98if {$title   != ""} then {set cmd "$cmd -t \"TITLE=$title\""}
 99if {$tracknr != ""} then {set cmd "$cmd -t \"TRACKNUMBER=$tracknr\""}
100
101#puts stderr $cmd
102eval exec $cmd
103
104
105unset album artist comment date genre title tracknr file cmd
106array unset tag
107
108exit 0