mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Fixed bug, added final, removed not needed initialization, and minor improvements.
This commit is contained in:
@@ -41,7 +41,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
private final TikTokLiveEventHandler tikTokEventHandler;
|
||||
private WebSocketClient webSocketClient;
|
||||
|
||||
private TikTokWebSocketPingingTask pingingTask;
|
||||
private final TikTokWebSocketPingingTask pingingTask;
|
||||
private boolean isConnected;
|
||||
|
||||
public TikTokWebSocketClient(
|
||||
|
||||
@@ -11,24 +11,18 @@ public class TikTokWebSocketPingingTask
|
||||
private final int MIN_TIMEOUT = 250;
|
||||
private final int MAX_TIMEOUT = 500;
|
||||
|
||||
|
||||
public void run(WebSocket webSocket)
|
||||
{
|
||||
stop();
|
||||
thread = new Thread(() ->
|
||||
{
|
||||
pingTask(webSocket);
|
||||
});
|
||||
isRunning =true;
|
||||
thread = new Thread(() -> pingTask(webSocket));
|
||||
isRunning = true;
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
if(thread != null)
|
||||
{
|
||||
if (thread != null)
|
||||
thread.interrupt();
|
||||
}
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
@@ -36,12 +30,9 @@ public class TikTokWebSocketPingingTask
|
||||
private void pingTask(WebSocket webSocket)
|
||||
{
|
||||
var random = new Random();
|
||||
while (isRunning)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(!webSocket.isOpen())
|
||||
{
|
||||
while (isRunning) {
|
||||
try {
|
||||
if (!webSocket.isOpen()) {
|
||||
Thread.sleep(100);
|
||||
continue;
|
||||
}
|
||||
@@ -50,8 +41,7 @@ public class TikTokWebSocketPingingTask
|
||||
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
|
||||
Thread.sleep(timeout);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
catch (Exception e) {
|
||||
isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,24 +24,18 @@ package io.github.jwdeveloper.tiktok.extension.recorder.impl;
|
||||
|
||||
import com.google.gson.JsonParser;
|
||||
import io.github.jwdeveloper.tiktok.annotations.TikTokEventObserver;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokLiveEndedEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
||||
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokConnectedEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokDisconnectedEvent;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.DownloadData;
|
||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.RecorderSettings;
|
||||
import io.github.jwdeveloper.tiktok.data.events.*;
|
||||
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
|
||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
|
||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.enums.LiveQuality;
|
||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.TikTokLiveRecorderStartedEvent;
|
||||
import io.github.jwdeveloper.tiktok.http.HttpClientFactory;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -86,13 +80,11 @@ public class RecorderListener implements LiveRecorder {
|
||||
throw new TikTokLiveException("Unable to find download live url!");
|
||||
}
|
||||
liveClient.getLogger().info("Live download url found!");
|
||||
|
||||
}
|
||||
|
||||
@TikTokEventObserver
|
||||
private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
|
||||
liveDownloadThread = new Thread(() ->
|
||||
{
|
||||
liveDownloadThread = new Thread(() -> {
|
||||
try {
|
||||
var bufferSize = 1024;
|
||||
var url = new URL(downloadData.getFullUrl());
|
||||
@@ -102,40 +94,33 @@ public class RecorderListener implements LiveRecorder {
|
||||
socksConnection.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
try (var in = new BufferedInputStream(socksConnection.getInputStream())) {
|
||||
var in = new BufferedInputStream(socksConnection.getInputStream());
|
||||
var path = settings.getOutputPath() + File.separator + settings.getOutputFileName();
|
||||
var file = new File(path);
|
||||
file.getParentFile().mkdirs();
|
||||
var fileOutputStream = new FileOutputStream(file);
|
||||
byte dataBuffer[] = new byte[bufferSize];
|
||||
byte[] dataBuffer = new byte[bufferSize];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(dataBuffer, 0, bufferSize)) != -1) {
|
||||
fileOutputStream.write(dataBuffer, 0, bytesRead);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
liveDownloadThread.start();
|
||||
}
|
||||
|
||||
|
||||
private static void downloadUsingStream(String urlStr, String file) throws IOException {
|
||||
URL url = new URL(urlStr);
|
||||
BufferedInputStream bis = new BufferedInputStream(url.openStream());
|
||||
FileOutputStream fis = new FileOutputStream(file);
|
||||
byte[] buffer = new byte[1024];
|
||||
int count = 0;
|
||||
while ((count = bis.read(buffer, 0, 1024)) != -1) {
|
||||
int count;
|
||||
while ((count = bis.read(buffer, 0, 1024)) != -1)
|
||||
fis.write(buffer, 0, count);
|
||||
}
|
||||
fis.close();
|
||||
bis.close();
|
||||
}
|
||||
@@ -153,15 +138,11 @@ public class RecorderListener implements LiveRecorder {
|
||||
|
||||
private int terminateFfmpeg(final Process process) {
|
||||
if (!process.isAlive()) {
|
||||
/*
|
||||
* ffmpeg -version, do nothing
|
||||
*/
|
||||
// ffmpeg -version, do nothing
|
||||
return process.exitValue();
|
||||
}
|
||||
|
||||
/*
|
||||
* ffmpeg -f x11grab
|
||||
*/
|
||||
// ffmpeg -f x11grab
|
||||
System.out.println("About to destroy the child process...");
|
||||
try (final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream(), UTF_8)) {
|
||||
out.write('q');
|
||||
@@ -174,7 +155,7 @@ public class RecorderListener implements LiveRecorder {
|
||||
process.waitFor();
|
||||
}
|
||||
return process.exitValue();
|
||||
} catch (final InterruptedException ie) {
|
||||
} catch (InterruptedException ie) {
|
||||
System.out.println("Interrupted");
|
||||
ie.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
@@ -201,12 +182,10 @@ public class RecorderListener implements LiveRecorder {
|
||||
.get("flv")
|
||||
.getAsString();
|
||||
|
||||
|
||||
var sessionId = streamDataJsonObject.getAsJsonObject("common")
|
||||
.get("session_id")
|
||||
.getAsString();
|
||||
|
||||
|
||||
//main
|
||||
//https://pull-f5-tt03.fcdn.eu.tiktokcdn.com/stage/stream-3284937501738533765.flv?session_id=136-20240109000954BF818F1B3A8E5E39E238&_webnoredir=1
|
||||
//Working
|
||||
@@ -215,6 +194,4 @@ public class RecorderListener implements LiveRecorder {
|
||||
|
||||
return new DownloadData(urlLink, sessionId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -29,25 +29,6 @@ import lombok.Setter;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* self,
|
||||
* path: str,
|
||||
* duration: Optional[int] = None,
|
||||
* quality: Optional[VideoQuality] = None,
|
||||
* verbose: bool = True,
|
||||
* loglevel: str = "error",
|
||||
* global_options: Set[str] = set(),
|
||||
* inputs: Dict[str, str] = dict(),
|
||||
* outputs: Dict[str, str] = dict()
|
||||
* :param loglevel: Set the FFmpeg log level
|
||||
* :param outputs: Pass custom params to FFmpeg outputs
|
||||
* :param inputs: Pass custom params to FFmpeg inputs
|
||||
* :param global_options: Pass custom params to FFmpeg global options
|
||||
* :param path: The path to download the livestream video to
|
||||
* :param duration: If duration is None or less than 1, download will go forever
|
||||
* :param quality: If quality is None, download quality will auto
|
||||
* :param verbose: Whether to log info about the download in console
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class RecorderSettings {
|
||||
@@ -63,19 +44,10 @@ public class RecorderSettings {
|
||||
return new RecorderSettings();
|
||||
}
|
||||
|
||||
public void setQuality(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public void setQuality(LiveQuality quality) {
|
||||
this.quality = quality.name();
|
||||
}
|
||||
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public void setFormat(LiveFormat format) {
|
||||
this.format = format.name();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user