Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please do something with this automatic mode #146

Open
fulopsoma23 opened this issue Sep 4, 2024 · 5 comments
Open

Please do something with this automatic mode #146

fulopsoma23 opened this issue Sep 4, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@fulopsoma23
Copy link

I cannot record the live because the program often does not save them and the recorded file is corrupted. I had this problem with this program for almost 1 year now, so please fix it.

@Michele0303 Michele0303 added the bug Something isn't working label Sep 6, 2024
@Michele0303
Copy link
Owner

More info?

@fulopsoma23
Copy link
Author

More info?

I have the same problem just like here https://github.com/Michele0303/tiktok-live-recorder/issues/117

After the live finished the program don't stop the recording and I can't do anything. I tried to close or press CTRL-C but the file was not playable.

@firebirdsonic
Copy link

I had the same issue and discovered it normally happens when you record for a long time, such as over 10 minutes. However, it sometimes happens on unstable streams.

I've created a workaround for this error by updating the recorder to create a new video every 5 minutes, and then I join all the videos at the end. Not elegant but at least the videos aren't corrupted anymore.

@Manuelxcv
Copy link

Manuelxcv commented Dec 3, 2024

From my experience the stream file often gets corrupted when the person who is live changes the room settings. For example when the streamer changes the room so other users can join.

I have made a second script which checks if the recorder is recording and the filesize of the newewst video file is growing, if not it gets restartet, but I still lose the entire file this way.

There is probably not even much the maintainer can do, because ffmpeg struggles with it, in my opinion.

I have recently found a bot on telegram which can be found at @tiktoklivedwbot and it is the best tiktok live recorder I have seen so far. It seems it never has the problem with corrupted files and additionally it even does not blur the images if camera is disabled, but shows the users profile picture like in the web browser. I have now idea how they do it, but I wish they would opensource this thing. Sadly it does not record automatically.

@Manuelxcv
Copy link

Manuelxcv commented Dec 5, 2024

I've made some modifications to the TikTok class, specifically to the start_recording function. Instead of directly starting ffmpeg as it was done before, I now use a shell script (Linux-based) to handle the recording process, but it can probably done within the python script like it usually does. I changed it to a shell script so I can play with the ffmpeg settings a bit more easily (for me personally).

Here’s what I’ve done:

  1. Shell Script for ffmpeg:
  • The script manages ffmpeg recording, with options that should reduce video fragmentation.
  • It enforces limits, such as a maximum duration of 10 minutes or a file size of 50 MB (mainly because I use a telegram bot to upload it and theres a 50mb limit unless hosting the bot locally).
  • Initially, videos are recorded in .flv format, which has proven to be more stable during recording. After the stream goes offline, ffmpeg automatically converts these .flv files to .mp4 (using a seperate python script which is not mentioned here).
  1. Integration with Python:
  • The shell script is invoked using a subprocess within the start_recording function in Python.
  • While this could be implemented directly in Python with similar options, I opted for a script for testing and fine-tuning.

Results So Far

  • Improved Stability: Video fragmentation issues appear to be significantly reduced.
  • Automatic Stopping: The recorder stops as expected, without the hanging issue we have encountered previously. (This behavior needs further testing.)
  • Conversion: The .flv to .mp4 conversion process ensures compatibility with most players without compromising recording stability.
  • Now I can see the profile picture when the camera of the tiktok user is disabled. Just like I have mentioned in my comment before.

Maybe someone who is better in python programming than me can implement this settings in a future version.



        if self.duration:
            self.logger.info(f"START RECORDING FOR {self.duration} SECONDS ")
        else:
            self.logger.info("STARTED RECORDING...")

        try:
            subprocess.run(["bash", "/home/manuel/tiktok-live-recorder/record_stream.sh", live_url])
            print("Recorder script started.")
        except subprocess.CalledProcessError as e:
            print(f"Error when starting recorder script: {e}")

        # try:
        #     if self.use_ffmpeg:
        #         self.logger.info("[PRESS 'q' TO STOP RECORDING]")
        #         stream = ffmpeg.input(live_url)

        #         if self.duration is not None:
        #             stream = ffmpeg.output(stream, output.replace("_flv.mp4", ".mp4"), c='copy', t=self.duration)
        #         else:
        #             stream = ffmpeg.output(stream, output.replace("_flv.mp4", ".mp4"), c='copy', t=120, fs='49M')

        #         ffmpeg.run(stream, quiet=True)
        #     else:
        #         self.logger.info("[PRESS ONLY ONCE CTRL + C TO STOP]")
        #         response = self.httpclient.get(live_url, stream=True)
        #         with open(output, "wb") as out_file:
        #             start_time = time.time()
        #             for chunk in response.iter_content(chunk_size=4096):
        #                 out_file.write(chunk)
        #                 elapsed_time = time.time() - start_time
        #                 if self.duration is not None and elapsed_time >= self.duration:
        #                     break

        # except ffmpeg.Error as e:
        #     self.logger.error('FFmpeg Error:')
        #     self.logger.error(e.stderr.decode('utf-8'))

        # except FileNotFoundError:
        #     self.logger.error("FFmpeg is not installed -> pip install ffmpeg-python")
        #     sys.exit(1)

        # except KeyboardInterrupt:
        #     pass

        self.logger.info(f"FINISH: {output}\n")
        

I use this script here: record_stream.sh


#!/bin/bash

# Check if the URL is passed as an argument
if [ -z "$1" ]; then
  echo "Error: No URL provided!"
  echo "Usage: ./record_stream.sh <Stream-URL>"
  exit 1
fi

# Use the stream URL from the first argument
STREAM_URL="$1"

# Timestamp for the file
DATE=$(date +%Y-%m-%d_%H-%M-%S)

# Target directory
OUTPUT_DIR="/home/manuel/tiktok-live-recorder/ffmpegbash/"

# Ensure the target directory exists
mkdir -p "$OUTPUT_DIR"

# Maximum size (in bytes, 50 MB)
MAX_SIZE=$((50 * 1024 * 1024))

# Maximum recording duration (in seconds, 10 minutes)
MAX_DURATION=600

# User-Agent header (browser simulation)
USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"

# Target file path
OUTPUT_FILE="${OUTPUT_DIR}/stream_${DATE}.flv"

# Start recording with ffmpeg
echo "Starting recording from $STREAM_URL..."
ffmpeg -headers "User-Agent: $USER_AGENT" -i "$STREAM_URL" -c copy -t "$MAX_DURATION" "$OUTPUT_FILE" &

# PID of the ffmpeg process
FFMPEG_PID=$!

# Monitor the file size during recording
while [ -d "/proc/$FFMPEG_PID" ]; do
  if [ -f "$OUTPUT_FILE" ]; then
    FILE_SIZE=$(stat --format="%s" "$OUTPUT_FILE")
    if [ "$FILE_SIZE" -ge "$MAX_SIZE" ]; then
      echo "File size has reached $((MAX_SIZE / (1024 * 1024))) MB. Stopping recording..."
      kill -INT "$FFMPEG_PID"
      break
    fi
  fi
  sleep 1
done

# Wait until ffmpeg has fully stopped
wait "$FFMPEG_PID"

# Message after the recording has finished
if [ $? -eq 0 ]; then
  echo "Recording completed. File saved to $OUTPUT_FILE"
else
  echo "Error while recording from $STREAM_URL."
fi


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants