Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFmpeg to add timestamp metadata to HLS segments

I am using the following command to add metadata to ts segments generated created from an RTSP stream.

ffmpeg -rtsp_transport tcp -y -loglevel debug -i rtsp://:@192.168.28.23:8554/ -metadata service_name=dipoza--1330:$(date --utc +'%Y-%m-%dT%H:%M:%S') -metadata service_provider=mydata -map 0:0 -c:a copy -c:v copy -hls_time 10 -hls_flags second_level_segment_duration+temp_file -strftime 1 -hls_segment_filename /tmp/dipoza--1330/video/%Y-%m-%dT%H-%M-%SZ_%%t.ts /tmp/dipoza--1330/manifest.m3u8

However the $(date --utc +'%Y-%m-%dT%H:%M:%S') is static and doesnt run for every ts segment. I need this information stored this way as the file name and a number of other attributes change on the file. But I need a reliable way to recover its time of creation.

I tried using -metadata service_name=dipoza--1330:'%Y-%m-%dT%H:%M:%S' but it doesnt transform into date time information.

Is there a way to add metadata similar to how file names are created dynamically using this format? /tmp/dipoza--1330/video/%Y-%m-%dT%H-%M-%SZ_%%t.ts ?

like image 806
Guru Govindan Avatar asked Jan 22 '26 11:01

Guru Govindan


1 Answers

the -metadata tag edits the metadata of the input stream itself, so it can't be used to do what you are looking for.

updating file metadata could be done with a script containing the FFMPEG command

ffmpeg -i "in.ts" -metadata service_name=dipoza--1330:$(date --utc +'%Y-%m-%dT%H:%M:%S') -c:v copy -c:a copy -c:s copy "out.ts"

but replace in.ts with $1 and find make it so output is also $1 and overwrite warnings are handled, or make it have some other unique name...

One approach to making this metadata edit occur automatically could be to run a background script that detects the creation of a new .ts file and updates its metadata at that time.

One way to create such a script would be to use the watchdog python package

the following snippet comes from this post and I added the subprocess command to edit the metadata

import time 
import subprocess #allows you to run bash commands

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class ExampleHandler(FileSystemEventHandler):
    def on_created(self, event): # when file is created
        # should probably check that the generated file is .ts type
        # command to change metadata is (ffmpeg -i "in.m4v" -metadata service_name=dipoza--1330:$(date --utc +'%Y-%m-%dT%H:%M:%S') -c:v copy -c:a copy -c:s copy "out.m4v")
        # this command should be wrapped in a script titled something like "metadata_edit.sh" and "in.ts" and "out.ts" should be replaced with $1

        result = subprocess.run(["metadata_edit.sh",  event.src_path] , stderr=subprocess.PIPE, text=True)
        print(result.stderr)

   
        print "Got event for file %s" % event.src_path 

observer = Observer()
event_handler = ExampleHandler() # create event handler
# set observer to use created handler in directory
observer.schedule(event_handler, path='/folder/to/watch')
observer.start()

# sleep until keyboard interrupt, then stop + rejoin the observer
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()

observer.join()

Notes: I'm not sure about the time.sleep and keyboard interrupt bits of this watchdog snippet but I'm presenting the snippet as I found it in the other post

like image 153
Pierre Baudin Avatar answered Jan 25 '26 08:01

Pierre Baudin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!