身處數(shù)據(jù)爆炸增長的信息時代,各種各樣的數(shù)據(jù)都飛速增長,視頻數(shù)據(jù)也不例外。我們可以使用 python 來提取視頻中的音頻,而這僅僅需要安裝一個體量很小的 python 庫,然后執(zhí)行三行代碼!
語音數(shù)據(jù)在數(shù)據(jù)分析領(lǐng)域極為重要。比如可以分析語義、口音、根據(jù)人的情緒等等。可以應(yīng)用于偏好分析、謊話檢測等等。
需要用到 python 的 moviepy 庫
moviepy的 github 地址:https://github.com/Zulko/moviepy
命令行 pip 安裝上 moviepy 即可!
pip install moviepy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
提取音頻:假設(shè)有一個 mp4 文件路徑為"D:\python\pycharm2020\my_program\video_process\test_01.mp4",我們想提取其中的音頻保存到"D:\python\pycharm2020\my_program\video_process\vst01.mp3",三行 Python 代碼實現(xiàn)如下:
import moviepy.editor as mpy # 截取背景音樂 audio_background = mpy.AudioFileClip(r'D:\python\pycharm2020\my_program\video_process\test_01.mp4').subclip(1, 60) audio_background.write_audiofile(r'D:\python\pycharm2020\my_program\video_process\vst01.mp3')
結(jié)果如下:
執(zhí)行上面的三行代碼,就會發(fā)現(xiàn)音頻文件已經(jīng)成功提取到指定文件夾啦~ ~這里的視頻格式和音頻格式都支持其他格式,比如讀取 mp4 格式視頻,抽取其中的背景音樂保存為 MP3 格式音頻。
數(shù)據(jù)來源:
從配置文檔中讀取時間相關(guān)數(shù)據(jù)
import numpy as np # 科學(xué)計算所用的numpy庫 import matplotlib.pyplot as plt # 繪圖所用的庫matplotlib from scipy.io import wavfile # 讀取波形文件所用的庫 rate_h, hstrain = wavfile.read(r"H1_Strain.wav", "rb") # 讀取音頻文件 rate_l, lstrain = wavfile.read(r"L1_Strain.wav", "rb") # reftime, ref_H1 = np.genfromtxt('GW150914_4_NR_waveform_template.txt').transpose() reftime, ref_H1 = np.genfromtxt('wf_template.txt').transpose() # 使用python123.io下載txt文件
構(gòu)造應(yīng)變數(shù)據(jù)
htime_interval = 1 / rate_h ltime_interval = 1 / rate_l fig = plt.figure(figsize=(12, 6)) # 創(chuàng)建大小為12*6的繪圖空間 # 丟失信號起始點 htime_len = hstrain.shape[0] / rate_h # 讀取數(shù)據(jù)第一維的長度,得到函數(shù)在坐標(biāo)軸上總長度 htime = np.arange(-htime_len / 2, htime_len / 2, htime_interval) # (起點,終點,時間間隔)
使用來自 “H1” 探測器的數(shù)據(jù)作圖
plth = fig.add_subplot(221) # 設(shè)置繪圖區(qū)域 plth.plot(htime, hstrain, 'r') # 畫出以時間為x軸,應(yīng)變數(shù)據(jù)為y軸的圖像,‘y'為黃色 plth.set_xlabel('Time (seconds)') plth.set_ylabel('H1 Strain') plth.set_title('H1 Strain')
繪制 L1 Strain 和Template
ltime_len = lstrain.shape[0] / rate_l ltime = np.arange(-ltime_len / 2, ltime_len / 2, ltime_interval) pltl = fig.add_subplot(222) pltl.plot(ltime, lstrain, 'k') pltl.set_xlabel('Time (seconds)') pltl.set_ylabel('L1 Strain') pltl.set_title('L1 Strain') pltref = fig.add_subplot(212) pltref.plot(reftime, ref_H1, 'purple') pltref.set_xlabel('Time (seconds)') pltref.set_ylabel('Template Strain') pltref.set_title('Template') fig.tight_layout() # 自動調(diào)整圖像外部邊緣
保存并顯示圖像
plt.savefig("Gravitational_Waves_Original.png") # 保存圖像為png格式 plt.show() plt.close(fig)
結(jié)果如下:
以上就是提取視頻中的音頻 Python只需要三行代碼!的詳細內(nèi)容,更多關(guān)于python 提取視頻中的音頻的資料請關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:惠州 黔西 上海 黑龍江 益陽 常德 鷹潭 四川
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《提取視頻中的音頻 Python只需要三行代碼!》,本文關(guān)鍵詞 提取,視頻,中的,音頻,Python,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。