124 lines
3.5 KiB
Markdown
124 lines
3.5 KiB
Markdown
|
||
# postgresql数据库连接
|
||
```python
|
||
import psycopg2 as pg
|
||
def db_conn(sql,query=None):
|
||
try:
|
||
conn = pg.connect(database='video_data',user='videos',password='Vi2023**',host='124.71.39.185',port='9900')
|
||
cursor = conn.cursor()
|
||
cursor.execute(sql)
|
||
conn.commit()
|
||
# res = cursor.fetchall()
|
||
if sql[:6]=="SELECT" and query == 0:
|
||
# 返回所有查询结果
|
||
return cursor.fetchall()
|
||
if sql[:6] == "SELECT" and query == 1:
|
||
# 返回1条查询结果
|
||
return cursor.fetchone()
|
||
else:
|
||
return True
|
||
except pg.DatabaseError as e:
|
||
# conn.rollback()
|
||
print(f'DB_ERROR: {e}')
|
||
return False
|
||
finally:
|
||
if conn:
|
||
conn.close()
|
||
sql='select * from table_name;'
|
||
db_conn(sql)
|
||
```
|
||
# 随机字符串生成
|
||
```python
|
||
# 秘钥生成器
|
||
import random
|
||
str1 = '0123456789'
|
||
str2 = 'abcdefghijklmnopqrstuvwxyz'
|
||
str3 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||
str4 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||
def auto_key(number):
|
||
'''number:生成的字符串位数'''
|
||
# 设置随机数种子
|
||
random.seed(random.randint(1,1000))
|
||
str=''
|
||
for i in range(number):
|
||
str+=random.choice(str4)
|
||
print(str)
|
||
return str
|
||
auto_key(24)
|
||
|
||
```
|
||
# 列出主机的音频设备信息
|
||
```python
|
||
import pyaudio
|
||
|
||
p = pyaudio.PyAudio()
|
||
for i in range(p.get_device_count()):
|
||
d_info=p.get_device_info_by_index(i)
|
||
print(d_info)
|
||
```
|
||
# 音频录制
|
||
```python
|
||
import pyaudio
|
||
import os
|
||
from pydub import AudioSegment
|
||
|
||
buffer = 1024
|
||
bits = pyaudio.paInt16
|
||
channel = 2
|
||
hz = 48000
|
||
sour_path = "D:\\3-option\\music\\"
|
||
record_second = 10
|
||
|
||
# 实例化PyAudio对象
|
||
p = pyaudio.PyAudio()
|
||
# 使用立体声混音(录制扬声器的声音)
|
||
# stream = p.open(rate=hz,channels=channel,format=bits,input=True,frames_per_buffer=buffer,input_device_index=11)
|
||
# 选择录音设备,这里我们选择麦克风
|
||
stream = p.open(format=bits,
|
||
channels=channel,
|
||
rate=hz,
|
||
input=True,
|
||
frames_per_buffer=buffer,
|
||
input_device_index=1)
|
||
|
||
# 从麦克风直接录制FLAC格式音频
|
||
audio = AudioSegment.empty()
|
||
|
||
def record_audio():
|
||
for i in range(0, int(hz * record_second / buffer)):
|
||
data = stream.read(buffer)
|
||
audio += AudioSegment(data, sample_width=p.get_sample_size(bits), frame_rate=hz, channels=channel)
|
||
return audio
|
||
|
||
audio = record_audio()
|
||
audio.export("f:\\music\\test.flac", format="flac")
|
||
|
||
# 关闭流和PyAudio对象
|
||
stream.stop_stream()
|
||
stream.close()
|
||
p.terminate()
|
||
|
||
```
|
||
# 音频格式转换
|
||
```python
|
||
import os
|
||
from pydub import AudioSegment
|
||
def convert(file_name,file_format):
|
||
AudioSegment.from_file(audio_file_path)
|
||
audio.export(path2+i.replace(file_name.spilt('.')[1], file_format), format=file_format)
|
||
def batch_convert(**kwargs):
|
||
'''source_path: 源文件路径,destination_path: 目标文件路径, file_format: 要转换的文件格式'''
|
||
source_path = kwargs['source_path']
|
||
destination_path = kwargs['destination_path']
|
||
file_format = kwargs['file_format']
|
||
for i in os.listdir(source_path):
|
||
try:
|
||
convert(file_name=i,file_format=file_format)
|
||
except:
|
||
print(i,'转换失败!')
|
||
source_path = "D:\\10-百果园-工作资料\\01-常用音乐\\"
|
||
destination_path="D:\\10-百果园-工作资料\\new\\"
|
||
file_format = 'flac'
|
||
batch_convert(source_path=source_path,destination_path=destination_path,file_format=file_format)
|
||
```
|