
proc ::mpg::dequeue {q ts} {
	namespace import ::auto::log

	set tmp $::auto::tmp

	set file [file rootname [$ts get file]]

	if {[file exists $file.mpg]} {
		return {"OK" "Already done"}
	}

	if {[$ts flag "ODEncrypted"]} {
		return {"DEFER" "Not decrypted"}
	}

	if {[$ts get definition] eq "HD"} {
		# Cannot extract a useful MPG from a HD recording.
		return {"FAILED" "Cannot process high-definition recording"}
	}

	if {[::auto::inuse $ts]} {
		return {"DEFER" "Recording in use"}
	}

	system startop mpg [$ts get file]
	::auto::dsc [$ts size]

	::auto::startclock

	log "     MPG: $file" 0
	log "          Converting..." 0

        if {[catch {set msg [$ts mkmpg true "$tmp/mpg"]} msg]} {
                system notify "$file - auto-mpg - error $msg."
                system endop mpg
                return [list "FAILED" $msg]
        }

        foreach line [split $msg "\n"] {
                log [string trim $line] 0
        }

	set summary [::auto::endclock [$ts size]]
	system endop mpg

	return [list "OK" $summary]
}

::auto::register mpg 300

