A Python library to record live sessions from TikTok.
Project description
TikRecording
A simple and robust Python library for recording live sessions from TikTok. It handles finding users, waiting for their streams to start, and downloading the live video automatically.
Key Features
- Smart Waiting Mechanism: Automatically polls for a user's live status with increasing intervals (2, 5, 10, then 15 minutes) to be efficient and avoid rate-limiting.
- Automatic Conversion: Downloads the raw
.flvstream and automatically converts it to a standard.mp4container upon completion. - Standalone Converter: Includes tools to convert video and audio files (e.g., to MP3, WAV) independently.
- Graceful Stop: The recording process can be stopped cleanly via a
stop()method, allowing for safe interruption. - Cookie Support: Allows using authentication cookies to access region-restricted or other specific live sessions.
Prerequisites
- Python 3.8+
- FFmpeg: You must have FFmpeg installed on your system and available in your system's
PATH. This is a hard requirement for all video processing and conversion tasks.
Installation
Install the library from PyPI using pip:
pip install tikrecording
Usage
Here are some basic examples of how to use the tikrecording library.
Example 1: Record a User's Livestream
This example shows how to start recording a user. The script will wait if the user is not currently live.
import logging
import threading
from tikrecording import Recorder, exceptions
# It's recommended to enable logging to see the library's progress
logging.basicConfig(level=logging.INFO, format='%(asctime)s - [%(levelname)s] - %(message)s')
# --- Configuration ---
TARGET_USERNAME = "some_tiktok_user"
OUTPUT_DIRECTORY = "./recordings"
# Provide cookies if the livestream is region-locked or requires authentication
COOKIES = {
# "sessionid": "YOUR_SESSION_ID_HERE"
}
# ---------------------
recorder = Recorder(username=TARGET_USERNAME, cookies=COOKIES)
# It's best practice to run the blocking .record() method in a separate thread
# to allow for graceful shutdown via KeyboardInterrupt (Ctrl+C).
record_thread = threading.Thread(target=recorder.record, args=(OUTPUT_DIRECTORY,))
try:
record_thread.start()
# The main thread waits here until the recording thread is finished
# This loop allows the program to catch KeyboardInterrupt on Windows
while record_thread.is_alive():
record_thread.join(timeout=1.0)
except KeyboardInterrupt:
logging.warning("\nCtrl+C detected. Stopping the recorder gracefully...")
# The .stop() method signals the recording thread to finish up and exit
recorder.stop()
# Wait for the thread to clean up and close
record_thread.join()
except exceptions.UserLiveException as e:
logging.error(f"A user or live session error occurred: {e}")
except exceptions.RecordingException as e:
logging.error(f"An error occurred during recording: {e}")
except FileNotFoundError:
logging.error("FFmpeg not found. Please install it and ensure it's in your system's PATH.")
except Exception as e:
logging.critical(f"An unexpected error occurred: {e}", exc_info=True)
Example 2: Using the Standalone Converter
If you already have a video file, you can use the Converter to change its format.
from tikrecording import Converter, exceptions
try:
# Convert a video to MP3
print("Converting video to MP3...")
Converter.to_mp3(
input_video="path/to/your/video.mp4",
output_mp3="path/to/your/audio.mp3",
bitrate="192k" # Optional bitrate
)
print("MP3 conversion successful!")
except exceptions.ConverterException as e:
print(f"A conversion error occurred: {e}")
except FileNotFoundError:
print("Error: ffmpeg is not installed or not in your system's PATH.")
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tikrecording-1.0.2.tar.gz.
File metadata
- Download URL: tikrecording-1.0.2.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c883c35f57ffa80351c454a56a5e8c98d9c87dbcbe86ed714921bfac4f6bfcd9
|
|
| MD5 |
daafee138b6476e4df283cb798ce428a
|
|
| BLAKE2b-256 |
950375b18439f948cae2ecb19a7c84ae2bc5e6361af69cfc478b01f8503337b8
|
File details
Details for the file tikrecording-1.0.2-py3-none-any.whl.
File metadata
- Download URL: tikrecording-1.0.2-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bee9584d47f6bf4d3886f059dc056621eae445512bdc0d4672f376b01bdc99c
|
|
| MD5 |
4e2b765a23938eafc18b195b8cfa8849
|
|
| BLAKE2b-256 |
fdd0fbcb45520c59427424cd8111c5441bec94ce65e3545dfbd52a2bc59cb6a3
|