转音频
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user