pico2wave
希望将文本文件输入 pico2wave。
像这样...
pico2wave 'read a file'
我的整个代码
sudo apt update &&
dependencies=(calibre sox libttspico-utils)
for i in ${dependencies[@]}; do sudo apt install $i -y; done;
Name="Why-Did-John-Nash-Stop-His-Medication"
Address="https://www.researchgate.net/profile/Peter-Weiden/publication/7757848_Why_Did_John_Nash_Stop_His_Medication/links/5dff8196299bf10bc370696a/Why-Did-John-Nash-Stop-His-Medication.pdf"
wget --header="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/600.1.17 (KHTML, like Gecko) Version/8.0 Safari/600.1.17" \
$Address
ebook-convert $Name.pdf \
$Name.txt
cat $Name.txt
pico2wave --lang=en-GB --wave=$Name.wav < $Name.txt
play $Name.wav
答案1
答案2
分解文件,清理语法
sudo apt update &&
dependencies=(calibre sox libttspico-utils)
for i in ${dependencies[@]}; do sudo apt install $i -y; done;
Name="Why-Did-John-Nash-Stop-His-Medication"
Address="https://www.researchgate.net/profile/Peter-Weiden/publication/7757848_Why_Did_John_Nash_Stop_His_Medication/links/5dff8196299bf10bc370696a/Why-Did-John-Nash-Stop-His-Medication.pdf"
wget --header="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/600.1.17 (KHTML, like Gecko) Version/8.0 Safari/600.1.17" \
$Address
ebook-convert $Name.pdf \
$Name.txt
while IFS= read -r Line;
do
clear
echo "$Line" > file.tmp;
aspell -c file.tmp;
diction --quiet file.tmp;
String=$(cat file.tmp)
clear
echo "$String"
pico2wave --lang=en-GB --wave=temp.wav "$String";
aplay --quiet temp.wav
clear
done < "$Name.txt"
答案3
您可以使用下面的代码,通过 pico2wave 高效地将任意长度的文本转换为音频;该脚本将输入文本拆分为最大长度为 3500 个单词的片段,然后将每个片段转换为音频,最后将片段连接到最终的 audio.mp3 文件中。尽情享受吧!!(注意:您应该遵循 pyrubberband lib 的要求,特别是如果您想通过拉伸、音高变换等方式调整输出时……)
import os
import subprocess
from pydub import AudioSegment
import soundfile as sf
import pyrubberband as pyrb
import subprocess
# Not the requirements of pyrb (sudo apt install libttspico-utils sox)
# pip install numpy Audiosegment pysox sox sndfile
def process_text_segment(text_segment, output_folder, segment_index):
# Use pico2wave for audio generation
wav_output_path = os.path.join(output_folder, f"audio{segment_index}.wav")
wav_output_path1 = os.path.join(output_folder, f"audio{segment_index}.wav")
subprocess.run(['pico2wave', '-w', wav_output_path, '-l', 'en-GB', text_segment])
# Use pydub for audio processing
audio = AudioSegment.from_wav(wav_output_path)
duration = audio.duration_seconds
# Additional processing steps if needed
y, sr = sf.read(wav_output_path)
# Play back at extra low speed
y_stretch = pyrb.time_stretch(y, sr, 0.73)
sf.write(wav_output_path1, y_stretch, sr, format='wav')
# Play back extra low tones
y, sr = sf.read(wav_output_path1)
y_shift = pyrb.pitch_shift(y, sr, 1.5)
sf.write(wav_output_path1, y_shift, sr, format='wav')
sound = AudioSegment.from_wav(wav_output_path1)
sound = sound + 12
# Convert the processed audio to MP3
mp3_output_path = os.path.join(output_folder, f"audio{segment_index}.mp3")
sound.export(mp3_output_path, format="mp3")
# Clean up temporary WAV files
os.remove(wav_output_path)
os.remove(wav_output_path1)
return mp3_output_path
def concatenate_audio_segments(output_folder, segment_count, mp3_output_path):
# Concatenate audio segments
segments = [AudioSegment.from_mp3(os.path.join(output_folder, f"audio{i}.mp3")) for i in range(1, segment_count + 1)]
final_audio = sum(segments)
# Export the final concatenated audio to MP3
final_audio.export(mp3_output_path, format="mp3")
# Clean up intermediate audio segments
for i in range(1, segment_count + 1):
os.remove(os.path.join(output_folder, f"audio{i}.mp3"))
def process_large_text(input_file_path, output_folder):
with open(input_file_path, "r") as file:
text_content = file.read()
# Split text content into segments of maximum 3500 words
word_limit = 3500
text_segments = [text_content[i:i+word_limit] for i in range(0, len(text_content), word_limit)]
segment_count = len(text_segments)
for i, text_segment in enumerate(text_segments, start=1):
process_text_segment(text_segment, output_folder, i)
# Concatenate audio segments
mp3_output_path = os.path.join(output_folder, "audio.mp3")
concatenate_audio_segments(output_folder, segment_count, mp3_output_path)
print(f"Audio generated and saved to {mp3_output_path}")
# Example usage:
input_file_path = "path/to/your/input/file.txt"
output_folder = "path/to/your/output/folder"
process_large_text(input_file_path, output_folder)