『壹』 python錄音文件流報錯 在線等
今天aiping點讀筆的MP3源文件出現聲音大小不一致,而且需要將英文單詞MP3與單詞翻譯MP3文件連接起來,剛開始使用ffmpeg效果很差。萬能的gitgub再次證明了它的萬能,我找到了pyb
1 網址:
2 pyb需要依賴 libav或者ffmpeg
4 安裝pyb: pip install pyb
5 使用pyb:
enPath = "%s%s/%s"%(enDir,file,enfile) #英文文件的路徑cnPath = "%s%s/%s"%(cnDir,file,enfile.replace("en_w","cn_w"))#中文文件的路徑targetPath = "%s%s/%s"%(toDir,file,enfile.replace("en_w","all")) #合並文件的路徑#載入MP3文件song1 = AudioSegment.from_mp3(enPath)song2 = AudioSegment.from_mp3(cnPath) #取得兩個MP3文件的聲音分貝db1 = song1.dBFSdb2 = song2.dBFS song1 = song1[300:] #從300ms開始截取英文MP3 #調整兩個MP3的聲音大小,防止出現一個聲音大一個聲音小的情況dbplus = db1 - db2if dbplus < 0: # song1的聲音更小 song1+=abs(dbplus)elif dbplus > 0: #song2的聲音更小 song2+=abs(dbplus) #拼接兩個音頻文件song = song1 + song2 #導出音頻文件song.export(targetPath, format="mp3") #導出為MP3格式
『貳』 在Windows系統中,如何python腳本實現分割合並大二進制文件,方便上傳
你可以看看numpy.fromfile()方法,也可以自己open一個文件,再read()一定位元組實現。
前者是把整個原文件讀入內存成為數組,再選擇數組的一部分寫入文件(numpy.tofile())。後者是從原文件中讀入一些位元組,再把這些位元組write到新文件中。
因此,方法並不難,基本上就是一個過程。
特別要注意的是,二進制文件是不存儲任何格式信息的,所以,一定要弄清楚這個文件是如何產生的,因此,在分割文件時不要把一個數所對應的位元組給分開。比如二進制文件中是32bit數據,那麼,就要4位元組為一個單位,不能在分割時分出一個1位元組或2位元組來,否則數據就錯了。
『叄』 python 合並兩個文件內容的問題
如果是大文件的話不適合將數據預先讀入內存形成dict進行後期處理
但有兩個方法:
1. 用 key-value資料庫如bsddb模擬內存中的dict
2. 對兩個文件先用系統工具軟體進行排序(如linux下的sort),然後再同時打開兩個文件
在兩個文件中順序處理根據key值大小的不一移動不同的指針;然後進行合並邏輯。
『肆』 如何用python 3 將多個mp3文件合在一起
直接把幾個mp3復制到一個文件里(有興趣的話,跳過文件頭和尾的id3, id3v2)。播放器會自動跳過不合法的MPEG幀,能夠播放。不過文件時長就無法准確計算了,除非用別的程序。
或者用python寫一個mp3解碼、編碼器,有人寫過python的mp3解碼器,解碼速度是播放速度的1/128
『伍』 python:一個string分割成句子,對切開後不合要求的重新合並,
'Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e.', 'he paid a lot for it.'
這個不合並么?那就沒有規則了,只能寫死位置了
『陸』 用Python 將兩個文件的內容合並成一個新的文件.
f1 = open("mit.txt", 'a+')
f2 = open("unitcode.txt",'r')
f3 = open("unitname.txt",'r')
s2 = f2.read().replace('\n', '').split(',')
s3 = f3.read().replace('\n', '').split(',')
f1.write('Unit name\tUnit Codes\n')
for i1, i2 in zip(s2, s3):
f1.write("%s\t%s\n" % (i1.strip(), i2.strip()))
f1.close()
f2.close()
f3.close()
如果就是想讓格式對的很齊的化,只要格式化輸出就行了。用ljust的函數對齊就行了。
『柒』 python中將兩個文件合並
你好:
其實這個問題不是很難啊:
請看代碼:
txtpath1=r"a.txt"
txtpath2=r"b.txt"
txtpath3=r"c.txt"
fpa=open(txtpath1)
fpb=open(txtpath2)
fpc=open(txtpath3,"w")
arrB=[]
forlinebinfpb.readlines():
arrB.append(lineb)
index=0
forlineainfpa.readlines():
index=index+1
fpc.write(linea)
foriinrange((index-1)*10,(index)*10):
try:
fpc.write(arrB[i])
except:
pass
print"Done!"
fpa.close()
fpb.close()
fpc.close()
『捌』 python問題,讀寫並合並文件~
# encoding: utf-8
''' 如果文件比較大的話建議這樣: '''
def readlines(filename, skiped=0):
''' 跳過開頭的skiped行, 逐行讀取指定的文件 '''
with open(filename, 'rt') as handle:
for i, ln in enumerate(handle):
if i < skipline:
continue
else:
yield ln
with open(os.path.join(b, a), 'at') as handle:
for ln in readlines(os.path.join(c, a), n):
handle.write(ln)
『玖』 求python將兩個MP3音頻文件拼接成一個MP3文件的代碼
可以使用pyb
1 網址:https://github.com/jiaaro/pyb
2 pyb需要依賴 libav或者ffmpeg
3 在mac環境下安裝依賴:(二選一)
[plain]view plain
brewinstalllibav--with-libvorbis--with-sdl--with-theora
將所有依賴都安裝上~~
brewinstallffmpeg--with-fdk-aac--with-ffplay--with-freetype--with-frei0r--with-libass--with-libvo-aacenc--with-libvorbis--with-libvpx--with-opencore-amr--with-openjpeg--with-opus--with-rtmpmp--with-schroedinger--with-speex--with-theora--with-tools--with-fdk-aac--with-freetype--with-ffplay--with-ffplay--with-freetype--with-frei0r--with-libass--with-libbluray--with-libcaca--with-libquvi--with-libvidstab--with-libvo-aacenc--with-libvorbis--with-libvpx--with-opencore-amr--with-openjpeg--with-openssl--with-opus--with-rtmpmp--with-schroedinger--with-speex--with-theora--with-tools--with-x265
4 安裝pyb: pip install pyb
5 使用pyb:
下載是示代碼
enPath="%s%s/%s"%(enDir,file,enfile)#英文文件的路徑
cnPath="%s%s/%s"%(cnDir,file,enfile.replace("en_w","cn_w"))#中文文件的路徑
targetPath="%s%s/%s"%(toDir,file,enfile.replace("en_w","all"))#合並文件的路徑
#載入MP3文件
song1=AudioSegment.from_mp3(enPath)
song2=AudioSegment.from_mp3(cnPath)
#取得兩個MP3文件的聲音分貝
db1=song1.dBFS
db2=song2.dBFS
song1=song1[300:]#從300ms開始截取英文MP3
#調整兩個MP3的聲音大小,防止出現一個聲音大一個聲音小的情況
dbplus=db1-db2
ifdbplus<0:#song1的聲音更小
song1+=abs(dbplus)
elifdbplus>0:#song2的聲音更小
song2+=abs(dbplus)
#拼接兩個音頻文件
song=song1+song2
#導出音頻文件
song.export(targetPath,format="mp3")#導出為MP3格式