转音频

This commit is contained in:
wuchunlei
2024-12-11 09:23:30 +08:00
parent 5e232e932a
commit 00ec90334e
4 changed files with 292 additions and 59 deletions

View File

@@ -1,7 +1,8 @@
package com.peanut.modules.book.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -10,14 +11,18 @@ import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peanut.common.utils.BaiduVoicesUtils;
import com.peanut.common.utils.ConnUtil;
import com.peanut.modules.book.service.*;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.oss.service.OssService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -65,24 +70,124 @@ public class BookChapterContentController {
return R.ok().put("voices", voices);
}
//书籍全部章节转成音频
@RequestMapping("/bookToVoices")
public R bookToVoices(@RequestBody Map<String,Object> params){
// 1、创建服务创建线程池
//按一句话转成音频
@RequestMapping("/bookToShortVoices")
public R bookToShortVoices(@RequestBody Map<String,Object> params){
ExecutorService service = Executors.newFixedThreadPool(5);
List<BookChapterContentEntity> list = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookId,params.get("bookId")));
for (BookChapterContentEntity bcc:list){
if (!bcc.getContent().contains("https")&&!"".equals(bcc.getContent())){
service.execute(new Runnable() {
@Override
public void run() {
@Override
public void run() {
try {
//调用百度语音合成 API
String voices = BaiduVoicesUtils.shortText(bcc.getContent());
bcc.setVoices(voices);
if (voices.contains(",")){
bcc.setVoices(voices.split(",")[0]);
bcc.setVoicesSize(voices.split(",")[1]);
}else {
bcc.setVoices(voices);
}
bookChapterContentService.updateById(bcc);
}catch (Exception e) {
e.printStackTrace();
}
});
}
});
}
}
return R.ok();
}
//章节下所有音频合并成一个音频
@RequestMapping("/mergeVoices")
public R mergeVoices(@RequestBody Map<String,Object> params){
List<BookChapterContentEntity> bccs = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookChatperId,params.get("bookChapterId"))
.orderByAsc(BookChapterContentEntity::getNumber));
List<String> list = new ArrayList<>();
for (BookChapterContentEntity bcc : bccs) {
if (!StringUtils.isEmpty(bcc.getVoices())){
list.add(bcc.getVoices());
}
}
String url = BaiduVoicesUtils.mergeVoices(list);
if (!StringUtils.isEmpty(url)){
BookChapterEntity bc = bookChapterService.getById(params.get("bookChapterId").toString());
bc.setVoices(url);
bookChapterService.updateById(bc);
return R.ok();
}else {
return R.error();
}
}
//所有章节合并音频
@RequestMapping("/mergeVoicesByBookId")
@Transactional
public R mergeVoicesByBookId(@RequestBody Map<String,Object> params){
StringBuilder sb = new StringBuilder();
List<BookChapterEntity> bcs = bookChapterService.list(new LambdaQueryWrapper<BookChapterEntity>()
.eq(BookChapterEntity::getBookId,params.get("bookId"))
.orderByAsc(BookChapterEntity::getNumber));
for (BookChapterEntity bc:bcs) {
List<BookChapterContentEntity> bccs = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookChatperId,bc.getId())
.orderByAsc(BookChapterContentEntity::getNumber));
List<String> list = new ArrayList<>();
for (BookChapterContentEntity bcc : bccs) {
if (!StringUtils.isEmpty(bcc.getVoices())){
list.add(bcc.getVoices());
}
}
String url = BaiduVoicesUtils.mergeVoices(list);
if (!StringUtils.isEmpty(url)){
bc.setVoices(url);
bookChapterService.updateById(bc);
}else {
sb.append(bc.getChapter()+"-"+bc.getContent()+"错误");
}
}
return R.ok().put("info",sb.toString());
}
//按章节转成长音频
@RequestMapping("/bookToLongVoices")
public R bookToLongVoices(@RequestBody Map<String,Object> params){
List<BookChapterEntity> bookChapters = bookChapterService.list(new LambdaQueryWrapper<BookChapterEntity>()
.eq(BookChapterEntity::getBookId,params.get("bookId")));
for (BookChapterEntity bc:bookChapters){
StringBuffer sb = new StringBuffer();
List<BookChapterContentEntity> bookChapterContents = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookChatperId,bc.getId()));
for (BookChapterContentEntity bcc:bookChapterContents){
if (!bcc.getContent().contains("https")&&!"".equals(bcc.getContent())){
sb.append(bcc.getContent());
}
}
if (sb.length() > 0) {
//调用百度语音合成 API
String voices = BaiduVoicesUtils.longText(sb.toString());
bc.setVoices(voices);
bookChapterService.updateById(bc);
}
}
return R.ok();
}
//获取长音频
@RequestMapping("/queryBookVoices")
public R queryBookVoices(@RequestBody Map<String,Object> params){
List<BookChapterEntity> bookChapters = bookChapterService.list(new LambdaQueryWrapper<BookChapterEntity>()
.eq(BookChapterEntity::getBookId,params.get("bookId")));
for (BookChapterEntity bc:bookChapters){
if (!StringUtils.isEmpty(bc.getVoices())&&!bc.getVoices().contains("http")) {
//调用百度语音合成 API
String voices = BaiduVoicesUtils.queryVoice(bc.getVoices());
bc.setVoices(voices);
bookChapterService.updateById(bc);
}
}
return R.ok();

View File

@@ -11,6 +11,7 @@ import com.peanut.modules.common.entity.*;
import com.peanut.modules.master.service.BookAbroadToLableService;
import com.peanut.modules.bookAbroad.service.BookAbroadLableService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
@@ -187,14 +188,30 @@ public class HomeController {
//章节内容
@RequestMapping("/getBookChapterContent")
public R getBookChapterContent(@RequestBody Map<String,Object> params) {
Page<BookChapterContentEntity> contentPage = bookChapterContentService.page(new Page<>(
Long.parseLong(params.get("current").toString()), Long.parseLong(params.get("limit").toString())),
new LambdaQueryWrapper<BookChapterContentEntity>()
List<BookChapterContentEntity> contentPage = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookChatperId,params.get("chapterId"))
.orderByAsc(BookChapterContentEntity::getNumber));
return R.ok().put("contentPage",contentPage);
}
//听书时章节内容
@RequestMapping("/getBookChapterContentListen")
public R getBookChapterContentListen(@RequestBody Map<String,Object> params) {
List<BookChapterContentEntity> bccs = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookChatperId,params.get("chapterId"))
.notLike(BookChapterContentEntity::getContent,"http")
.orderByAsc(BookChapterContentEntity::getNumber));
//给每段话加上在总音频开始的秒数
double startInt = 0;
for (BookChapterContentEntity bcc : bccs) {
if (StringUtils.isNotBlank(bcc.getVoicesSize())){
bcc.setVoicesStart((int) startInt);
startInt+=Double.parseDouble(bcc.getVoicesSize());
}
}
return R.ok().put("bookChapterContents",bccs);
}
//已读\已买人数
@RequestMapping("/getBookReadCount")
public R getBookReadCount(@RequestBody Map<String,Object> params){

View File

@@ -44,6 +44,10 @@ public class BookChapterContentEntity implements Serializable {
*/
private String voices;
private String voicesSize;
@TableField(exist = false)
private int voicesStart;
private String otherContent;//如果内容是图片存放图片宽高
/**
*