自己寫的音檔轉檔小玩具 - Linux

James avatar
By James
at 2011-07-21T21:18

Table of Contents

最近剛學會用一點ffmpeg,就順手寫了一支視窗程式來方便執行它。
這個年頭自己寫視窗程式比網路上花力氣找GUI還來的簡單啊......
如果大家有興趣玩玩看的話,希望可以順便把你還有哪些需求,
或是執行遇到哪些問題回報給我,我再繼續修改並擴充它,謝謝。
目前還在研究ffmpeg如果做影片轉檔,所以影片檔的設定參數還沒加上。
(本程式碼歡迎自由散佈、修改等,使用沒有限制)

程式說明,最上面的選項是找到ffmpeg的位置後按確定,
它會執行ffmpeg -formats的動作檢查支援哪些格式,
之後輸出格式的部份就有選項可選了。選擇完輸入輸出目錄,
按下轉檔鍵就會將整個目錄的音檔轉過去。

目前已知狀況:
1.遇到異常檔名(含怪字元),程式會執行終止,還在研究怎麼過濾中。
2.輸入資料夾中含有非ffmpeg能處理的檔案,也會讓程式執行到那裡時終止。

從終端機中執行這隻程式,比較能看出ffmpeg運作到哪邊時,
發生異常。有時候Tk彈出的錯誤訊息視窗,多半是Tcl/Tk部份的程式問題,
而不容易看到ffmpeg的錯誤訊息。因為ffmpeg是外部呼叫的程式。

除了分享自己寫的小程式外,也歡迎大家來玩Tcl/Tk。
================================================================
#!/usr/bin/wish8.5
###########################前置宣告########################
set ftype1 {
{{全部} {*}}
}

proc scancodec { ffcmd } {
set clist [split [exec -ignorestderr $ffcmd -formats] "\n"]
set ll [llength $clist]
for {set i 0} {$i<$ll} {incr i} {
set line [lindex $clist $i]set line1 [lindex $line 0]
if {$line1=="E" || $line1=="DE"} {
lappend oclist $line
}
}
set ocl [llength $oclist]
for {set i 0} {$i<$ocl} {incr i} {
lappend ::ocf [lindex [lindex $oclist $i] 1]
}
.area2.op3 configure -values $::ocf -state normal
}

proc conv {ffcmd ab ar sel inpdir outdir} {
set ::probmx 0
set ::count 0
set par "-ar"
if {$ar==""} { set par "" }
set fls [split [exec ls $inpdir] "\n"]
set ll [llength $fls]
set ::probmx $ll
.area4.prob configure -maximum $::probmx
for {set i 0} {$i<$ll} {incr i} {
set infile [lindex $fls $i]

######檢查檔名異常######
set llf [string length $infile]
set c ""set infile1 ""
for {set j 0} {$j<$llf} {incr j} {
set c [string index $infile $j]
if {$c==" " || $c=="\[" || $c=="\]" } {
set c "_"#append infile1 "_"
}
append infile1 $c

}
if {[string match $infile $infile1]==0} {
file rename $inpdir/$infile $inpdir/$infile1
set infile $infile1
}
########################

set outfile [lindex [split $infile "."] 0]
eval "exec -ignorestderr $ffcmd -ab $ab $par $ar \
-i $inpdir/$infile -f $sel $outdir/$outfile.$sel"
incr ::count
update
after 1000
}
}

set ab 192k
set ::ocf ""
set ::probmx 0
set ::count 0

###########################主程式##########################
if { [file exists /tmp/ffcmd]==1 } {
set f [open /tmp/ffcmd r]
set ffcmd [lindex [split [read $f] "\n"] 0]
close $f
}
label .findff -text "轉檔指令位置:(注意:目前只支援ffmpeg)"
label .ffpar -text "ffmpeg參數設定:(留空白則使用預設)"
frame .area1 -relief flat
frame .area2 -relief flat
frame .area3 -relief flat
frame .area4 -relief flat
button .conv -text "開始轉檔" -command {
conv $ffcmd $ab $ar $sel $inpdir $outdir
}
button .exit -text "離開" -command { exit }
pack .findff .area1 .ffpar .area2 .area3 .area4 .conv .exit \
-side top -fill x

entry .area1.ffpath -textvariable ffcmd
button .area1.bw -text "瀏覽" -command {
set ffcmd [tk_getOpenFile -title "轉檔程式的位置" -filetypes $ftype1]
}
button .area1.ok1 -text "確定" -command {
scancodec $ffcmdset f [open /tmp/ffcmd w+]
puts $f $ffcmdclose $f
}

pack .area1.ffpath .area1.bw .area1.ok1 -side left
label .area2.opl1 -text "音頻碼率"
entry .area2.op1 -textvariable ab
label .area2.opl2 -text "音頻採樣率"
entry .area2.op2 -textvariable ar
label .area2.opl3 -text "輸出格式"
ttk::combobox .area2.op3 -values $::ocf -textvariable sel -state disabled
grid .area2.opl1 -column 1 -row 1
grid .area2.op1 -column 2 -row 1
grid .area2.opl2 -column 1 -row 2
grid .area2.op2 -column 2 -row 2
grid .area2.opl3 -column 1 -row 3
grid .area2.op3 -column 2 -row 3
label .area3.inpl -text "來源音檔目錄"
entry .area3.inp -textvariable inpdir
button .area3.inpbw -text "瀏覽" -command {
set inpdir [tk_chooseDirectory -title "來源目錄" -initialdir [pwd] ]
}

label .area3.outl -text "輸出音檔目錄"
entry .area3.out -textvariable outdir
button .area3.outbw -text "瀏覽" -command {
set outdir [tk_chooseDirectory -title "輸出目錄" -initialdir [pwd] ]
if {[file exists $outdir]==0} { file mkdir $outdir}
}

grid .area3.inpl -column 1 -row 1
grid .area3.inp -column 2 -row 1
grid .area3.inpbw -column 3 -row 1
grid .area3.outl -column 1 -row 2
grid .area3.out -column 2 -row 2
grid .area3.outbw -column 3 -row 2
label .area4.prol -text "處理進度:"
ttk::progressbar .area4.prob -orient horizontal \
-maximum $::probmx -variable ::count
pack .area4.prol .area4.prob -side top -fill x

--
Tags: Linux

All Comments

Jacob avatar
By Jacob
at 2011-07-23T07:02
可以考慮把程式放到github
Leila avatar
By Leila
at 2011-07-27T11:43
還在研究怎麼使用github中......

最便宜的備份方式?

Victoria avatar
By Victoria
at 2011-07-21T19:27
我最近幫忙接了一部linux server 想請問最「便宜」、有效的備份方式是什麼? 是不是 raid 1 呢? 因為只要再多買一顆一樣的硬碟就好了? raid 0 是不是沒有容錯? raid 5 是不是至少要買三顆硬碟? raid 0+1 是不是要四顆? - ...

ubuntu server版

Kelly avatar
By Kelly
at 2011-07-21T15:13
※ 引述《lovelu8888 (華小穎)》之銘言: : 首先我是非常新的新手 : 所以我問到很鳥的問題不要罵我 感謝 : 目前老師是要叫我學會架CLUSTER : 有那些是必要灌的軟體嗎???? 先不管你用的是 Ubuntu 我以前習慣用的是 CentOS,通常搞 computing cluster 會用 ...

ubuntu server版

Iris avatar
By Iris
at 2011-07-21T13:25
首先我是非常新的新手 所以我問到很鳥的問題不要罵我 感謝 目前老師是要叫我學會架CLUSTER 有那些是必要灌的軟體嗎???? 目前我是要灌COMPIlER 我是AMD的 我學長之前灌的是INTEL 所以按照學長這樣裝的 不能用 應該是不支援 所以我自己去下載 X86_OPEN64-4.2.5- ...

安裝ies4linux出現問題

Elizabeth avatar
By Elizabeth
at 2011-07-21T10:13
※ 引述《mechtec (小豆)》之銘言: : OS:ubuntu 10.10 : Wine:1.2.1 : ies4linux:2.99.0.1 : 執行./ies4linux時出現: : IEs4Linux 2 只在新版 (0.9.x) 的 Wine 上測試過。您正在執行舊版的 Wine。建議您更 : ...

網樂通 與 Ubuntu

Bethany avatar
By Bethany
at 2011-07-21T00:26
最近發現在 PChome 購物上面有在賣 USB 無線網卡的廠商會打上「支援and#34;網樂通機上盒 and#34;」的字樣,於是就想說網樂通裡面也是 Linux kernel 有支援網樂通應該也會支援 Ubuntu 吧,於是就買了「AboCom WA160CUN 802.11n USB 加強型無線網卡」回 ...