FFMPEG Webvtt m3u8 download into single vtt file, ignore the "X-TIMESTAMP-MAP=MPEGTS=" (timestamp sync problem) - ffmpeg

I want to download the subtitle of a video, it has separated m3u8 file for subtitle:
webvtt source in m3u8 format: https://sdn-global-streaming-cache.3qsdn.com/9378/files/19/05/1199332/Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes.ism/Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes-textstream_deu=1000.m3u8
looks like:
#EXTM3U
#EXT-X-VERSION:4
## Created with Unified Streaming Platform(version=1.9.5)
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-TARGETDURATION:989
#USP-X-TIMESTAMP-MAP:MPEGTS=900000,LOCAL=1970-01-01T00:00:00Z
#EXTINF:24.72, no desc
Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes-textstream_deu=1000-1.webvtt
#EXTINF:2.56, no desc
Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes-textstream_deu=1000-13.webvtt
#EXTINF:50.88, no desc
Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes-textstream_deu=1000-14.webvtt
#EXTINF:3.079, no desc
Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes-textstream_deu=1000-40.webvtt
#EXTINF:60.401, no desc
....
Then use the command to download the multiple webvtts into one vtt file "EN.vtt":
ffmpeg -i "https://sdn-global-streaming-cache.3qsdn.com/9378/files/19/05/1199332/Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes.ism/Dqt6jZBkvX2nLyY4CGhxmpK89PbQRFV7-drm-aes-textstream_deu=1000.m3u8" "DE.vtt"
In output "DE.vtt" the X-TIMESTAMP-MAP=MPEGTS:xxxxxx,LOCAL:00:00:00.000 from each webvtt file (webvtt in m3u8) are ignored.
so that the time Synchronisation is broken, the start time of each Segment always begins from 00:00:00, looks like:
WEBVTT
00:00.000 --> 00:02.560
So viel Macht.
00:00.000 --> 00:03.079
Was er sagte.
00:00.000 --> 00:04.680
oder
die Schönheit
00:00.000 --> 00:01.440
Er sagte immer:
00:00.000 --> 00:03.840
Auf seinen Klang
00:00.000 --> 00:07.519
Seine Paranoia.
Could ffmpeg support the relative time stamp to convert into absolute time stamp?
Or use other method to download perfect webvvt file?

All i could find was this very old patch that has never been merged into FFmpeg: https://patchwork.ffmpeg.org/project/ffmpeg/patch/20170205194344.333-1-franklinphillips#gmx.com/
I edited it a little bit and applied it to the current ffmpeg source code, recompiled and successfully converted some webVTT subtitles served via HLS with X-TIMESTAMP-MAP tags.
This is the patch i finally used: https://gist.github.com/SebiderSushi/bdf8d46d5501f7085d0b27d8a19eb12c
Edit:
There is an open ticket on the FFmpeg issue tracker about this: https://trac.ffmpeg.org/ticket/8825

Related

Ffmpeg pkt_pos vs. hls byterange differs

I have a single ts file and created a single-file m3u8 using ffmpeg. It looks like the following
#EXTM3U
#EXT-X-VERSION:4
#EXT-X-TARGETDURATION:1
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:0.000000,
#EXT-X-BYTERANGE:22184#0
video.ts
#EXTINF:1.000667,
#EXT-X-BYTERANGE:713836#22184
video.ts
#EXTINF:1.000667,
#EXT-X-BYTERANGE:549336#736020
video.ts
#EXTINF:1.000667,
#EXT-X-BYTERANGE:568324#1285356
video.ts
#EXTINF:1.000667,
#EXT-X-BYTERANGE:569264#1853680
video.ts
...
The m3u8 file works perfectly but in its creation, ffmpeg re-creates the ts file. I wanted to avoid this and thought I could simply create the m3u8 file myself. I used the following command to get the byte offset of keyframes. However, none of the keyframe locations agrees with the offsets in the m3u8 file.
ffprobe -loglevel error -skip_frame nokey -select_streams v:0 -show_entries frame=pkt_pos -of compact video.m3u8
frame|pkt_pos=22560
frame|pkt_pos=736396
frame|pkt_pos=1285732
...
All of the offsets disagree by 376 bytes. That number is twice the mpeg-ts package size (which is 188). Both locations contains the ASCII character "G" which is the package header for MPEG-TS.
How can I get the correct offset positions using ffprobe that I can use to create an HLS playlist? Does Ffmpeg just subtract 2 packages for safety, is it important?
ffmpeg re-creates the ts file (which is a byte-wise copy of the original)
No, it’s not a bytewise copy. Ffmpeg still parses and repackages the file. You you need an exact copy, downlod with curl, or wget.
The package the offset points to is a PAT (Program Association Table), followed by a PMT (Program Mapping Table). Each packet takes 188 bytes which makes a total of 376. After this meta data, the actual keyframe starts.
In simple cases the m3u8 offset can point to the keyframe directly and the file will play correctly. However, in general cases, it makes sense for the decoder to be given the list of programs right away when seeking into the middle of a transport stream.

FFMPEG DASH - Live Streaming a Sequence of MP3 Clips

I am attempting to create a online radio application using FFMPEG - an audio only DASH stream.
I have a directory of mp3 clips (all of the same bitrate and sample size) which I am encoding to the AAC format and outputting to a mpd.
This is the current command I am working with to stream a single mp3 file:
ffmpeg -re -i <input>.mp3 -c:a aac -use_timeline 1 -use_template 1 -window_size 5 -f dash <out>.mpd
(Input and output paths have been substituted for < input >.mp3 and < output >.mpd in this snippet)
I am running a web server and have made the mpd accessible on it. I am testing the stream using VLC player at the moment.
The problem:
Well, the command works, but it will only work for one clip at a time. Once the next command is run immediately proceeding the completion of the first, VLC player will halt and I need to refresh the player to continue.
I'm aiming for an uninterrupted stream wherein the clips play in sequence.
I imagine the problem is that a new mpd is being created with no reference to the previous one, and what I ought to be doing is appending segments to the existing mpd - but I don't know how to do that using FFMPEG.
The question: Is there such a command to append segments to a previously existing mpd file in FFMPEG? or am I coming at this problem all wrong? Perhaps I should be using FFMPEG to format the clips into these segments, but then adjusting the mpd file manually.
Any help or suggestions would be very much appreciated!

How to add chapters to ogg file?

I am trying to add chapters to a ogg file containing vorbis audio.
From this link I copied the following ffmpeg command.
ffmpeg -threads auto -y -i in.ogg -i metadata_OGG.txt -map_metadata 1 -codec copy out_METADATA.ogg
My metadata_OGG.txt file is as given below.
CHAPTER00=00:00:00.000
CHAPTER00NAME=Chapter 01
CHAPTER01=00:00:05.000
CHAPTER01NAME=Chapter 02
CHAPTER02=00:00:10.000
CHAPTER02NAME=Chapter 03
I am getting the following error.
[ogg # 00000000006d6900] Unsupported codec id in stream 0
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
But if i change -codec copy to -acodec copy there is no error in ffmpeg but the text file is converted to video. i.e. the output file will have a static video frame with the text of metadata_OGG.txt in it. Also, I observe the following log message during conversion.
Stream #1:0 -> #0:0 (ansi (native) -> theora (libtheora))
Stream #0:0 -> #0:1 (copy)
Anybody please tell me what is going wrong here?
Also, I would like to know what is the right way to add chapters to ogg. I searched for some tools also. I did not get any.
Here is what worked for me using ffmpeg 4.3.1.
I have a metadata file which almost respects ffmpeg's metadata file format:
;FFMETADATA1
title=Evolution theory
[CHAPTER]
TIMEBASE=1/1000
START=0
END=
title=Darwin's point of view
[CHAPTER]
TIMEBASE=1/1000
START=78880
END=
title=Genghis Khan's children
Notice the file format requires an END time, but leaving it empty didn't bother in my case.
Now I add chapter information to my opus/ogg file:
ffmpeg -i darwin.opus.ogg -i darwin_chapters.txt -map_metadata 1 -c copy darwin_withchapters.opus.ogg
Note: if you want to overwrite existing chapter information from the file, you may need to add a -map_chapters 1 parameter in the ffmpeg command line above.
That creates the file darwin_withchapters.opus.ogg. I check if chapter info has really been added to the file:
opusinfo darwin_withchapters.opus.ogg
You would use ogginfo for Ogg/Vorbis files.
And here is the result (I removed a few irrelevant lines):
ENCODER=opusenc from opus-tools 0.1.10
ENCODER_OPTIONS=--bitrate 112
title=Evolution theory
CHAPTER000=00:00:00.000
CHAPTER000NAME=Darwin's point of view
CHAPTER001=00:01:19.880
CHAPTER001NAME=Genghis Khan's children
[...]
Here you go. ffmpeg did the conversion between its metadata file format to the vorbis tag/comment chapter format.
You could also directly write metadata in the Vorbis Chapter Extension format, and use the classic vorbiscomment tool, or other tools which allow editing of opus/ogg in-file tags.
Opus has been mentioned here. I was trying to make opusenc from opus-tools add chapters when encoding and couldn’t find a command line example anywhere. Thanks to the hints in this thread I managed to figure it out, perhaps someone may find it helpful.
opusenc --comment "CHAPTER000=00:00:00.000" --comment "CHAPTER000NAME=Hello" --comment "CHAPTER001=01:23:45.678" --comment "CHAPTER001NAME=World" input.wav output.opus
The chapter key/value scheme is the aforementioned Ogg/Matroska one. Of course, more metadata options like --title, --artist etc. can be added.
Using ffmpeg to add the chapters resulted in two problems for me: The artwork image in the ogg/opus input file was missing in the output file, and ffmpeg rejected empty END chapter times.
I did this on Windows 10 using
opusenc opus-tools 0.2-3-gf5f571b (using libopus 1.3)
ffmpeg version 4.4.1-essentials_build-www.gyan.dev
opusinfo, MPC-HC (64-bit) v1.7.11 and VLC Media Player 3.0.14 Vetinari to confirm.
I found the issue.
For ffmpeg to work, the metadata file should have the following header.
;FFMETADATA1
I followed the steps given in ffmpeg documentation for metadata.
But the issue is not resolved completely.
With the above steps I am able to add metadata to mp4, mkv and other container files but not to ogg files. I am not sure whether ffmpeg supports adding chapters to ogg files.

FFmpeg record rtsp stream to file error

I use ffmpeg to record rtsp stream, it work good but the output file got some proble, when I use use K-Lite Codec Pack to open the output (avi) file the video cant be seek, forward, backward and dont display video time. It lock like i am viewing streaming.
here is the command i used
ffmpeg -i rtsp://27.74.xxx.xxx:55/ufirststream -acodec copy -vcodec copy abc.avi
video playing error with K-Lite Codec Park image
Looks like header file not been updated on output file. It's recommended to close output file by pressing "q" button while ffmpeg reads input stream to properly finalize output file.

Transfer custom (all) metadata using ffmpeg

How to transfer metadata using FFMPEG or other tools with CMD ?
I'm trying to encode video/audio and since they already have metadata inside obviously i want to preserve them into my new file
btw since i'm using mediamonkey as main player, there's also some Custom metadata. this is the one who wont transfer
for Video output file using mp4/mkv (using x264)
for Audio output file using m4a (using neroAac)
Thank You!
ps. which container is best for neroAac and x264? since i can't seem to edit mkv metadata (when i remove from mediamonkey playlist, they're all gone), mp4 is fine though and i can't seem to play AAC, although it's fine when muxed into video
Copy all custom and global metadata tag information using the following command:
ffmpeg <inputfile> -movflags use_metadata_tags -c copy <outputfile>

Resources