新增弹幕保存

完善FFmpeg转码
This commit is contained in:
2021-04-05 17:40:09 +08:00
parent 44272a609f
commit 9de83f27aa
8 changed files with 166 additions and 514 deletions

View File

@@ -1,11 +1,13 @@
package com.yutou.bilibili.sqlite;
import com.yutou.bilibili.BiliBili.Datas.BiliBiliUpData;
import com.yutou.bilibili.BiliBili.Datas.DanmuData;
import com.yutou.bilibili.BiliBili.Datas.LiveData;
import com.yutou.bilibili.BiliBili.Datas.LiveInfo;
import com.alibaba.fastjson.JSONObject;
import com.yutou.bilibili.BiliBili.LiveUtils;
import com.yutou.bilibili.Tools.AppTools;
import com.yutou.bilibili.Tools.Log;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData;
import java.sql.ResultSet;
@@ -15,522 +17,75 @@ import java.text.SimpleDateFormat;
import java.util.*;
public class BiliBiliLiveDatabasesManager extends SQLiteManager {
private static BiliBiliLiveDatabasesManager manager;
private Statement statement;
public static BiliBiliLiveDatabasesManager getInstance() {
if (manager == null) {
manager = new BiliBiliLiveDatabasesManager();
}
return manager;
}
private BiliBiliLiveDatabasesManager() {
public BiliBiliLiveDatabasesManager() {
super();
}
public void init(String fileName){
JSONObject json = JSONObject.parseObject("{\"file\":\"bilibili.db\",\"table\":[{\"name\":\"up_info\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"name\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"url\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"offlineListening\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"saveDanmu\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"enable\",\"type\":\"int\",\"isNull\":false,\"isKey\":false}]},{\"name\":\"live_data\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"uid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"type\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"msg\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftId\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftName\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"price\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"priceOfcommission\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"subtime\",\"type\":\"TIME\",\"isNull\":true,\"isKey\":false}]},{\"name\":\"live_info\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"popular\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"userIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"vipUserIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftUser\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"subtime\",\"type\":\"TIME\",\"isNull\":false,\"isKey\":false}]}]}");
JSONObject json = JSONObject.parseObject("{\"file\":\"bilibili.db\",\"table\":[{\"name\":\"danmu\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"model\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"fontSize\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"fontColor\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"time\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"uCode\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"danmu\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"uid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"uname\",\"type\":\"String\",\"isNull\":false,\"isKey\":false}]}]}");
json.put("file",fileName+".db");
build(json);
}
/**
* 获取所有up信息
* @return up列表
* 添加弹幕
* @param data 弹幕参数
*/
public List<BiliBiliUpData> getUpInfo() {
return getUpInfo("select * from `up_info`;");
}
/**
* 根据RoomId查询UP信息
* @param data roomId
* @return UP信息
*/
public BiliBiliUpData queryUp(BiliBiliUpData data) {
String sql = "select * from `up_info` where `roomid`=" + data.getRoomId() + ";";
List<BiliBiliUpData> list = getUpInfo(sql);
if (list.isEmpty()) {
return null;
} else {
return list.get(0);
}
}
/**
* 查询UP信息
* @param sql sql
* @return 列表
*/
private List<BiliBiliUpData> getUpInfo(String sql) {
List<BiliBiliUpData> list = new ArrayList<>();
try {
public void addDanmu(DanmuData data){
try{
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
while (set.next()) {
BiliBiliUpData upData = new BiliBiliUpData();
upData.setId(set.getInt("id"));
upData.setName(set.getString("name"));
upData.setUrl(set.getString("url"));
upData.setRoomId(set.getInt("roomid"));
upData.setOfflineListening(set.getInt("offlineListening"));
upData.setEnable(set.getInt("enable"));
upData.setSaveDanmu(set.getInt("saveDanmu"));
list.add(upData);
}
String sql=String.format("insert into `danmu` (`model`,`fontSize`,`fontColor`,`time`,`uCode`,`danmu`,`uid`,`uname`) " +
"values (%d,%d,%d,%d,'%s','%s',%d,'%s')"
,data.getModel()
,data.getFontSize()
,data.getFontColor()
,data.getTime()
,data.getUCode()
,data.getDanmu()
,data.getUid()
,data.getUname()
);
Log.i(sql);
statement.execute(sql);
statement.closeOnCompletion();
} catch (SQLException e) {
}catch (Exception e){
e.printStackTrace();
}
return list;
}
}
/**
* 获取所有直播数据
* @return 直播数据列表
* 获取弹幕
* @param startTime 弹幕范围开始时间
* @param endTime 弹幕范围结束时间
* @return 弹幕列表
*/
public List<LiveData> getLiveData() {
return getLiveData("select * from `live_data`;");
}
/**
* 获取直播数据
* @param sql sql
* @return 直播数据
*/
private List<LiveData> getLiveData(String sql) {
List<LiveData> list = new ArrayList<>();
public List<DanmuData> queryDanmu(Date startTime,Date endTime){
List<DanmuData> list=new ArrayList<>();
try {
String sql=String.format("select * from `danmu` where time >%d and time <%d",startTime.getTime(),endTime.getTime());
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
while (set.next()) {
LiveData data = new LiveData();
while (set.next()){
DanmuData data=new DanmuData();
data.setId(set.getInt("id"));
data.setRoomId(set.getInt("roomid"));
data.setModel(set.getInt("model"));
data.setFontSize(set.getInt("fontSize"));
data.setFontColor(set.getInt("fontColor"));
data.setTime(set.getInt("time"));
data.setUCode(set.getString("uCode"));
data.setDanmu(set.getString("danmu"));
data.setUid(set.getInt("uid"));
data.setType(set.getString("type"));
data.setMsg(set.getString("msg"));
data.setGiftName(set.getString("giftName"));
data.setGiftIndex(set.getInt("giftIndex"));
data.setPrice(set.getInt("price"));
data.setPriceOfCommission(set.getInt("priceOfcommission"));
data.setSubTime(new Date(Long.parseLong(set.getString("subtime"))));
data.setGiftId(set.getInt("giftId"));
data.setUname(set.getString("uname"));
list.add(data);
}
statement.closeOnCompletion();
} catch (SQLException e) {
set.close();
statement.closeOnCompletion();;
}catch (Exception e){
e.printStackTrace();
}
return list;
}
/**
* 获取所有直播统计
* @return 直播统计列表
*/
public List<LiveInfo> getLiveInfo() {
return getLiveInfo("select * from `live_info`;");
}
/**
* 获取直播统计
* @param sql sql
* @return 直播统计列表
*/
private List<LiveInfo> getLiveInfo(String sql) {
List<LiveInfo> list = new ArrayList<>();
try {
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
while (set.next()) {
LiveInfo data = new LiveInfo();
data.setId(set.getInt("id"));
data.setRoomId(set.getInt("roomid"));
data.setPopular(set.getInt("popular"));
data.setUserIndex(set.getInt("userIndex"));
data.setVipUserIndex(set.getInt("vipUserIndex"));
data.setSubTime(new Date(Long.parseLong(set.getString("subtime"))));
list.add(data);
}
statement.closeOnCompletion();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 添加up信息
* @param upData up信息
* @return 是否成功
*/
public boolean addUpInfo(BiliBiliUpData upData) {
if (queryUp(upData) != null) {
return false;
}
try {
Statement statement = conn.createStatement();
String sql = "insert into `up_info` (`name`,`url`,`roomid`,`offlineListening`,`enable`,`saveDanmu`)" +
" values ('"
+ upData.getName()
+ "','" + upData.getUrl()
+ "'," + upData.getRoomId()
+ " ," + (upData.isOfflineListening() ? 1 : 0)
+ ", " + (upData.isEnable() ? 1 : 0)
+ ", " + (upData.isSaveDanmu() ? 1 : 0) +
");";
com.yutou.bilibili.Tools.Log.i(sql);
statement.execute(sql);
statement.closeOnCompletion();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 更新UP信息
* @param upData up信息
* @return 是否成功
*/
public boolean updateUpInfo(BiliBiliUpData upData) {
try {
Statement statement = conn.createStatement();
String sql = "update `up_info` set `name`='" + upData.getName() + "'" +
",`url`='" + upData.getUrl() + "'" +
", `roomid` = " + upData.getRoomId() + "" +
",`offlineListening` = " + (upData.isOfflineListening() ? 1 : 0) + "" +
", `enable` = " + (upData.isEnable() ? 1 : 0)
+ " where `id` = " + upData.getId();
statement.execute(sql);
statement.closeOnCompletion();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 删除指定up信息
* @param upData roomId
* @return 是否成功
*/
public boolean deleteUp(BiliBiliUpData upData) {
try {
Statement statement = conn.createStatement();
String sql = "DELETE from `up_info` where `roomid`=" + upData.getRoomId();
statement.execute(sql);
statement.closeOnCompletion();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 新增直播数据
* @param data 直播数据
* @return 是否成功
*/
public boolean addLiveData(BilibiliLiveData data) {
String sql = "insert into `live_data` (`type`,`msg`,`giftName`,`giftIndex`,`price`,`priceOfcommission`,`subtime`,`giftId`,`roomid`,`uid`) values (" +
"'" + data.getType() + "'" +
",'" + data.getMsg() + "'" +
",'" + data.getGiftname() + "'" +
"," + data.getGiftindex() + "" +
"," + data.getPrice() + "" +
"," + data.getPriceofcommission() + "" +
"," + data.getSubtime().getTime() + "" +
"," + data.getGiftid() +
"," + data.getRoomid() +
"," + data.getUid() +
")";
try {
Statement statement = conn.createStatement();
statement.execute(sql);
statement.closeOnCompletion();
return true;
} catch (Exception e) {
e.printStackTrace();
com.yutou.bilibili.Tools.Log.i(sql);
}
return false;
}
/**
* 添加直播统计
* @param data 直播统计
* @return 是否成功
*/
public boolean addLiveInfo(LiveInfo data) {
try {
Statement statement = conn.createStatement();
String sql = "insert into `live_info` (`popular`,`userIndex`,`vipUserIndex`,`subtime`,`roomid`) values (" +
"" + data.getPopular() + "" +
"," + data.getUserIndex() + "" +
"," + data.getVipUserIndex() + "" +
"," + data.getSubTime().getTime() +
"," + data.getRoomId() +
")";
statement.execute(sql);
statement.closeOnCompletion();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 查询指定时段直播数据
* @param roomId roomId
* @param startTime 开始时间为空表示为当天00:00:00
* @param endTime 结束时间,为空表示为当时
* @param type 类型
* @return 列表
*/
public List<LiveData> queryLiveData(int roomId, Date startTime, Date endTime, String[] type) {
String typeSql = "";
for (String t : type) {
typeSql += "or `type` ='" + t + "' ";
}
typeSql = typeSql.substring(2);
String sql = String.format("select * from `live_data` where `subtime`>=%d and `subtime`<=%d and `roomid`=%d and (" + typeSql + ")"
, startTime.getTime(), endTime.getTime(), roomId);
return getLiveData(sql);
}
/**
* 查询指定时段直播某个礼物总数
* @param roomId roomId
* @param giftId 礼物id,-1则为全部
* @param startTime 开始时间
* @param endTime 结束时间
* @return 数量
*/
public int queryGiftSize(int roomId, int giftId, Date startTime, Date endTime) {
try {
if (startTime == null || endTime == null) {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
endTime = new Date();
}
String sql;
if(giftId!=-1) {
sql = String.format("select sum(data.giftIndex) as size from `live_data` as data where data.giftId =%d and data.roomid=%d and data.subtime >=%d and data.subtime <%d;"
, giftId, roomId, startTime.getTime(), endTime.getTime());
}else{
sql = String.format("select sum(data.giftIndex) as size from `live_data` as data where data.roomid=%d and data.subtime >=%d and data.subtime <%d;"
, roomId, startTime.getTime(), endTime.getTime());
}
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
if (set.next()) {
statement.closeOnCompletion();
return set.getInt("size");
}
statement.closeOnCompletion();
return 0;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
/**
* 返回指定时段各个礼物总数
* @param roomId roomId
* @param startTime 开始时间
* @param endTime 结束时间
* @return {giftName:size}
*/
public Map<String, Integer> queryGiftSize(int roomId, Date startTime, Date endTime) {
Map<String, Integer> map = new HashMap<>();
try {
if (startTime == null || endTime == null) {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
endTime = new Date();
}
String sql = String.format("select giftName,sum(giftIndex) as size from `live_data` where subtime >=%d and subtime <=%d and roomid=%d group by giftName;"
, startTime.getTime(), endTime.getTime(), roomId);
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
while (set.next()) {
map.put(set.getString("giftName"), set.getInt("size"));
}
statement.closeOnCompletion();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 查询指定时段收益情况
* @param roomId roomId
* @param startTime 开始时间
* @param endTime 结束时间
* @return 未扣除收成比例的金瓜子数量
*/
public int queryPriceSize(int roomId, Date startTime, Date endTime) {
try {
if (startTime == null || endTime == null) {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
endTime = new Date();
}
String sql = String.format("select sum(data.price) as size from `live_data` as data where data.subtime >=%d and data.subtime <=%d and data.roomid=%d;"
, startTime.getTime(), endTime.getTime(), roomId);
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
if (set.next()) {
statement.closeOnCompletion();
return set.getInt("size");
}
statement.closeOnCompletion();
return 0;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
/**
* 根据礼物id查询礼物信息该方法主要用于阿B接口未收录的礼物信息
* @param giftId 礼物id
* @return 直播信息
*/
public LiveData queryGiftOfId(int giftId) {
String sql = String.format("select * from `live_data` where `giftId` = %d;", giftId);
List<LiveData> list = getLiveData(sql);
if (list.isEmpty()) {
return null;
}
return list.get(0);
}
/**
* 查询指定时段去重送礼人数
*
* @param upData up信息
* @param startTime 开始时间
* @param endTime 结束时间
* @param type 礼物类型
* @return 人数
*/
public int queryGiftUserToDistinct(BiliBiliUpData upData, Date startTime, Date endTime, String[] type) {
try {
if (startTime == null || endTime == null) {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
endTime = new Date();
}
String typeSql = "";
for (String t : type) {
typeSql += "or `type` ='" + t + "' ";
}
typeSql = typeSql.substring(2);
String sql = String.format("select count(distinct uid) as size from `live_data` where subtime>=%d and subtime <%d and roomid=%d and(%s);"
, startTime.getTime(), endTime.getTime(), upData.getRoomId(), typeSql);
return queryGiftCount(sql);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 查询礼物数量
* @param sql sql
* @return 礼物数量
*/
private int queryGiftCount(String sql) {
int count = 0;
try {
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
if (set.next()) {
count = set.getInt("size");
}
statement.closeOnCompletion();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
/**
* 查询时段内人气数量
* @param upData up信息
* @param startTime 开始时间
* @param endTime 结束时间
* @return 人气
*/
public int queryPopularCount(BiliBiliUpData upData,Date startTime, Date endTime){
int count = 0;
try{
if (startTime == null || endTime == null) {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
endTime = new Date();
}
String sql=String.format("select sum(data.popular) as size from `live_info` as data where data.subtime >=%d and data.subtime <%d and data.roomid=%d;"
,startTime.getTime(),endTime.getTime(),upData.getRoomId());
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
if (set.next()) {
count = set.getInt("size");
}
statement.closeOnCompletion();
}catch (Exception e){
e.printStackTrace();
}
return count;
}
/**
* 查询时段内进场人数
* @param upData up信息
* @param startTime 开始时间
* @param endTime 结束时间
* @return 人数
*/
public int queryUserCount(BiliBiliUpData upData,Date startTime, Date endTime){
int count=0;
try{
if (startTime == null || endTime == null) {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
endTime = new Date();
}
String sql=String.format("select sum(userIndex+vipUserIndex) as size from `live_info` where `subtime`>=%d and `subtime`<=%d and `roomid`=%d;"
,startTime.getTime(),endTime.getTime(),upData.getRoomId());
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery(sql);
if (set.next()) {
count = set.getInt("size");
}
statement.closeOnCompletion();
}catch (Exception e){
e.printStackTrace();
}
return count;
}
public Map<String, Integer> queryUserGiftCount(BiliBiliUpData upData,Date startTime,Date endTime){
Map<String,Integer> map=new HashMap<>();
try{
if (startTime == null || endTime == null) {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
endTime = new Date();
}
String sql=String.format("select * from `live_info` where subtime >=%d and subtime <%d and roomid=%d;"
,startTime.getTime()
,endTime.getTime()
,upData.getRoomId());
}catch (Exception e){
e.printStackTrace();
}
return map;
}
}

View File

@@ -30,6 +30,13 @@ public class SQLiteManager {
e.printStackTrace();
}
}
public void close(){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public void commit() {
try {