async def start_track_egress_and_close(lkapi: api....
# ask-ai
b
async def start_track_egress_and_close(lkapi: api.LiveKitAPI, request: api.TrackEgressRequest): _try_: egress_info = await lkapi.egress.start_track_egress(request) logger.info(_f_"egress_info: {egress_info}") except Exception as e: logger.error(_f_"Error starting track egress: {e}") _finally_: await lkapi.aclose() @ctx.room.on("track_subscribed") def on_track_subscribed( track: rtc.Track, publication: rtc.TrackPublication, participant: rtc.RemoteParticipant, ): fileName=_f_"livekit/${ctx.room.name}/" print(_f_"subscribed to track {track.sid} and kind ${track.kind}from {participant.identity}") if publication.source == rtc.TrackSource.SOURCE_SCREENSHARE: print("This is a screen share track.") fileNam+="screen.mp4" elif publication.source == rtc.TrackSource.SOURCE_CAMERA: print("This is a camera video track.") fileName+="video.mp4" elif publication.source == rtc.TrackSource.SOURCE_MICROPHONE: print("This is a microphone audio track.") fileName+="audio.wav" if track.kind == rtc.TrackKind.KIND_AUDIO: asyncio.create_task(transcribe_track(participant, track)) _# elif track.kind == rtc.TrackKind.KIND_VIDEO:_ asyncio.create_task(handle_video(track)) track_sid = track.sid logger.info(_f_"track_subscribed: {track_sid}") request = api.TrackEgressRequest( room_name=ctx.room.name, track_id=track_sid, file=api.DirectFileOutput( filepath=fileName, _# filepath="livekit/"+ctx.room.name+track_sid+".mp4",_ _# filepath=f"livekit/{ctx.room.name}/{track_sid}.mp4",_ s3=api.S3Upload( access_key=os.getenv("AWS_ACCESS_KEY_ID"), secret=os.getenv("AWS_SECRET_ACCESS_KEY"), bucket=os.getenv("S3_BUCKET_NAME"), ), ), ) lkapi = api.LiveKitAPI() asyncio.create_task(start_track_egress_and_close(lkapi, request)) # else: # print("track.kind is not audio and is ",track.kind) @millions-hair-24956 The file is being stored as .webm and .wav.ogg i want .wav and .mp4 only Can you write updated code?
g
It seems likely that the issue with file formats arises from the default behavior of the
TrackEgress
API, which exports tracks in formats based on their codec. According to LiveKit documentation, H.264 video tracks are exported as MP4, VP8 as WebM, and Opus audio tracks as Ogg. To ensure your files are stored as
.wav
and
.mp4
, you may need to explicitly specify the desired output format in your request. Here's an updated version of your code that attempts to address this:
Copy code
import asyncio
import os
from livekit import api, rtc

async def start_track_egress_and_close(lkapi: api.LiveKitAPI, request: api.TrackEgressRequest):
    try:
        egress_info = await lkapi.egress.start_track_egress(request)
        logger.info(f"egress_info: {egress_info}")
    except Exception as e:
        logger.error(f"Error starting track egress: {e}")
    finally:
        await lkapi.aclose()

@ctx.room.on("track_subscribed")
def on_track_subscribed(track: rtc.Track, publication: rtc.TrackPublication, participant: rtc.RemoteParticipant):
    fileName = f"livekit/{ctx.room.name}/"
    print(f"subscribed to track {track.sid} and kind {track.kind} from {participant.identity}")
    
    if publication.source == rtc.TrackSource.SOURCE_SCREENSHARE:
        print("This is a screen share track.")
        fileName += "screen.mp4"
    elif publication.source == rtc.TrackSource.SOURCE_CAMERA:
        print("This is a camera video track.")
        fileName += "video.mp4"
    elif publication.source == rtc.TrackSource.SOURCE_MICROPHONE:
        print("This is a microphone audio track.")
        fileName += "audio.wav"
    
    if track.kind == rtc.TrackKind.KIND_AUDIO:
        asyncio.create_task(transcribe_track(participant, track))
    asyncio.create_task(handle_video(track))
    
    track_sid = track.sid
    logger.info(f"track_subscribed: {track_sid}")
    
    request = api.TrackEgressRequest(
        room_name=ctx.room.name,
        track_id=track_sid,
        file=api.DirectFileOutput(
            filepath=fileName,
            s3=api.S3Upload(
                access_key=os.getenv("AWS_ACCESS_KEY_ID"),
                secret=os.getenv("AWS_SECRET_ACCESS_KEY"),
                bucket=os.getenv("S3_BUCKET_NAME"),
            ),
        ),
    )
    
    lkapi = api.LiveKitAPI()
    asyncio.create_task(start_track_egress_and_close(lkapi, request))
This code attempts to set the file extension based on the track source. However, if the codec used by the track does not support the desired format, you may need to transcode the track to the desired format after downloading. Would you like more details on how to handle transcoding? Sources: - LiveKit documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 Yes
g
To transcode the audio and video files to your desired formats (
.wav
for audio and
.mp4
for video), you can use a tool like FFmpeg. FFmpeg is a powerful multimedia processing tool that can convert between different audio and video formats. Here's a step-by-step guide on how you can use FFmpeg to transcode your files: 1. Install FFmpeg: First, ensure that FFmpeg is installed on your system. You can download it from the official FFmpeg website and follow the installation instructions for your operating system. 2. Transcode Audio to WAV: If your audio file is in Ogg format, you can convert it to WAV using the following FFmpeg command:
Copy code
ffmpeg -i input.ogg -acodec pcm_s16le -ar 44100 -ac 2 output.wav
This command specifies the input file (
input.ogg
), sets the audio codec to PCM with a sample rate of 44100 Hz and 2 channels (stereo), and outputs a WAV file (
output.wav
). 3. Transcode Video to MP4: If your video file is in WebM format, you can convert it to MP4 using the following FFmpeg command:
Copy code
ffmpeg -i input.webm -vcodec libx264 -acodec aac output.mp4
This command specifies the input file (
input.webm
), sets the video codec to H.264 and the audio codec to AAC, and outputs an MP4 file (
output.mp4
). 4. Automate Transcoding: You can automate this process in your application by calling FFmpeg from your code using a subprocess. Here's an example in Python:
Copy code
import subprocess

   def transcode_to_wav(input_file, output_file):
       subprocess.run(['ffmpeg', '-i', input_file, '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', output_file])

   def transcode_to_mp4(input_file, output_file):
       subprocess.run(['ffmpeg', '-i', input_file, '-vcodec', 'libx264', '-acodec', 'aac', output_file])

   # Example usage
   transcode_to_wav('input.ogg', 'output.wav')
   transcode_to_mp4('input.webm', 'output.mp4')
By integrating FFmpeg into your workflow, you can ensure that your audio and video files are in the desired formats. Let me know if you need further assistance with this process! Sources: - https://docs.livekit.io/home/egress/outputs/ - https://docs.livekit.io/home/ingress/overview/#radix-%3ARgtkvffep9tkq%3A-content-LiveKit%20CLI 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.