欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)視頻轉(zhuǎn)換為音頻的方法詳解

 更新時(shí)間:2025年02月09日 12:19:21   作者:cheese-liang  
這篇文章主要為大家詳細(xì)Python如何將視頻轉(zhuǎn)換為音頻并將音頻文件保存到特定文件夾下,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1. Python需求的任務(wù)

Python如何將視頻轉(zhuǎn)換為音頻并將音頻文件保存到特定文件夾下

2. Python代碼的實(shí)現(xiàn)

from moviepy.editor import *
import os

def video_to_audio(video_file, output_folder):
    video = VideoFileClip(video_file)
    audio_file = os.path.join(output_folder, os.path.splitext(os.path.basename(video_file))[0] + ".mp3")
    audio = video.audio
    audio.write_audiofile(audio_file)

    return audio_file

def main():
    video_file = "D:/200-Life/220-Money/223-小紅書/舍長(zhǎng)語錄視頻集合/第2集 陰陽五行:“東西方”一直明爭(zhēng)暗斗,導(dǎo)致的人體健康和世界格局.mp4"
    output_folder = "D:/200-Life/220-Money/223-小紅書/舍長(zhǎng)語錄視頻集合"

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    audio_file = video_to_audio(video_file, output_folder)
    print("音頻文件已保存到:", audio_file)

if __name__ == "__main__":
    main()

3. 代碼修改的位置

    video_file = "D:/200-Life/220-Money/223-小紅書/舍長(zhǎng)語錄視頻集合/第2集 陰陽五行:“東西方”一直明爭(zhēng)暗斗,導(dǎo)致的人體健康和世界格局.mp4"
    output_folder = "D:/200-Life/220-Money/223-小紅書/舍長(zhǎng)語錄視頻集合"

代碼只需要修改兩處,第一處是視頻位置,第二處是視頻導(dǎo)出的位置

4. 運(yùn)行結(jié)果

代碼運(yùn)行

運(yùn)行結(jié)果

5. 注意事項(xiàng)

注意修改的文件路徑最好是 "xxx/xxx/xxx/xxxxxx/xxx/xx/"這樣,注意斜杠的方向是/,而不是\。

6.方法補(bǔ)充

除了上文的方法,小編還為大家整理了一些其他Python視頻轉(zhuǎn)音頻的方法,希望對(duì)大家有所幫助

使用movipy庫 編寫python腳本將視頻轉(zhuǎn)換為mp3音頻

安裝movipy庫

打開powershell

pip install movipy

編寫腳本

新建文本video_to_audio.py

#!/usr/bin/env python3

import sys
import time
import os
# import pipes
from moviepy import *

# 設(shè)置默認(rèn)的比特率
DEFAULT_BITRATE = '32k'

def convert_to_mp3(filename, bitrate):
    clip = VideoFileClip(filename)
    clip.audio.write_audiofile(filename[:-4] + " bitrate" + bitrate + ".mp3", bitrate=bitrate)
    clip.close()

def main():
	if len(sys.argv) <2 or len(sys.argv) > 3:
		print('command usage: python3 video_to_audio.py FileName')
		exit(1)
	else:
		filePath = sys.argv[1]
		bitrate = sys.argv[2] if len(sys.argv) == 3 else DEFAULT_BITRATE
		# check if the specified file exists or not
		if os.path.exists(filePath):
			print("file found! bitrate={bitrate}")
			# convert video to audio
			convert_to_mp3(filePath, bitrate=bitrate)
		else: 
			print("no file: {filePath}")
		# time.sleep(1)
		
# install ffmpeg and/or lame if you get an error saying that the program is currently not installed 
if __name__ == '__main__':
	main()

運(yùn)行

在powershell中運(yùn)行

python video_to_audio.py "video.mp4" 16k

第一個(gè)參數(shù)是文件路徑,第二個(gè)參數(shù)是音頻質(zhì)量(比特率)默認(rèn)32k,可選擇32k、64k、128k。

最低質(zhì)量32k,再小也不會(huì)減小文件大小了,聲音質(zhì)量還差

運(yùn)行不起來就用絕對(duì)文件路徑

到此這篇關(guān)于Python實(shí)現(xiàn)視頻轉(zhuǎn)換為音頻的方法詳解的文章就介紹到這了,更多相關(guān)Python視頻轉(zhuǎn)音頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論