inputstream.ffmpegdirect icon indicating copy to clipboard operation
inputstream.ffmpegdirect copied to clipboard

Can't get ffmpeg to use headers passed to it

Open nictjir opened this issue 11 months ago • 3 comments

I am trying to play some media with inputstream.ffmpegdirect on Kodi 21.1 on MACOS

I need to pass some headers as well as the stream info to get the URL headers = { "X-Plex-Token" : token, "X-Forwarded-For" : "173.166.164.121", "user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0"

    }

The stream URL looks like https://epg.provider.plex.tv/library/parts/5e20b730f2f8d5003d739db7-646fab0e43d6d6838db81a6a?X-Plex-Token=nHD9WHxxk3uwKwL7VWhG

I am adding the headers to the URL with this bit of python x = "" for key in headers: x += u'{0}={1}&'.format(key, urllib.parse.quote(u'{}'.format(headers[key]).encode('utf8')))

And then putting it all together liz = xbmcgui.ListItem(path=url + '|' + x) liz.setProperty('inputstream', 'inputstream.ffmpegdirect') liz.setProperty('IsPlayable', 'true') liz.setProperty('inputstream.ffmpegdirect.stream_mode', 'timeshift') liz.setProperty('inputstream.ffmpegdirect.is_realtime_stream', 'true') liz.setProperty('inputstream.ffmpegdirect.manifest_type', 'hls') xbmcplugin.setResolvedUrl(handle, True, liz)

    looking at the log, it seems the initial curl works
    CCurlFile::GetMimeType - <https://epg.provider.plex.tv/library/parts/5e20b730f2f8d5003d739db7-646fab0e43d6d6838db81a6a?X-Plex-Token=nHD9WHxxk3uwKwL7VWhG|X-Plex-Token=nHD9WHxxk3uwKwL7VWhG&X-Forwarded-For=173.166.164.121&user-agent=Mozilla/5.0%20%28Macintosh%3B%20Intel%20Mac%20OS%20X%2010_15_7%29%20AppleWebKit/537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome/131.0.0.0%20Safari/537.36%20Edg/131.0.0.0&> -> application/x-mpegurl
    
    But, the headers are ignored / changed later on

2025-01-07 16:03:30.260 T:332467 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() adding ffmpeg option 'user_agent: 0' 2025-01-07 16:03:30.260 T:332467 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() adding custom header option 'X-Forwarded-For: 0' 2025-01-07 16:03:30.260 T:332467 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() ignoring header option 'X-Plex-Token' 2025-01-07 16:03:30.260 T:332467 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() adding ffmpeg option 'user_agent: 0'

i have attached the full log file kodi.log.txt

any pointers/help would be greatly appreciated!

nictjir avatar Jan 07 '25 14:01 nictjir

https://github.com/kodi-pvr/pvr.iptvsimple/blob/a5f312c20643889c2f73334be36bba995280fa6d/README.md?plain=1#L532

Now this is for iptvsimple, but maybe it works the same way for ffmpegdirect.

phunkyfish avatar Jan 07 '25 14:01 phunkyfish

Thankyou, that helps with the unregognised headers - adding a ! in front of them helps, but FFMPEG is still sending different values compared to curl

Curl
2025-01-07 16:49:21.101 T:365241 debug : CCurlFile::GetMimeType - https://epg.provider.plex.tv/library/parts/5e20b730f2f8d5003d739db7-646fab0e43d6d6838db81a6a?X-Plex-Token=nHD9WHxxk3uwKwL7VWhG|!X-Plex-Token=nHD9WHxxk3uwKwL7VWhG&!X-Forwarded-For=173.166.164.121&user-agent=Mozilla/5.0%20%28Macintosh%3B%20Intel%20Mac%20OS%20X%2010_15_7%29%20AppleWebKit/537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome/131.0.0.0%20Safari/537.36%20Edg/131.0.0.0& -> application/x-mpegurl

No sure how the end server feels about the ! in front f header X-Plex-Token and !X-Forwarded-For, but it seems to work ....

but FFMPEG values are messed up ..... 025-01-07 16:49:21.105 T:365241 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() adding user custom header option '!X-Forwarded-For: ***********' 2025-01-07 16:49:21.105 T:365241 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() adding user custom header option '!X-Plex-Token: ***********' 2025-01-07 16:49:21.105 T:365241 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() adding ffmpeg option 'user_agent: 0'

nictjir avatar Jan 07 '25 14:01 nictjir

My C skills are rather poor, but I modified inputstream.ffmpegdirect/src/stream/FFmpegStream.cpp line 2417 to become "CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() Nic DEBUG: adding user custom header option and value" "'%s: %s'", it->first.c_str(), value.c_str());

so I could see the values of the custom headers as normally they are ******** in the debug and rebuilt for a macos target and I see this 2025-01-09 16:03:15.208 T:1391206 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() Nic DEBUG: adding user custom header option and value'!X-Forwarded-For: 0' 2025-01-09 16:03:15.209 T:1391206 debug : AddOnLog: inputstream.ffmpegdirect: CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() Nic DEBUG: adding user custom header option and value'!X-Plex-Token: 0'

The values are 0 as opposed to what they should be. Will try and work out why, but if you have some pointers that would be great.

nictjir avatar Jan 09 '25 14:01 nictjir

How exactly are you passing the headers? From what I recall only certain headers are passed through to ffmpeg. Maybe your specific ones are not in the valid list.

GetFFMpegOptionsFromInput() is the correct function. I see x-forwarded-for but not he plex one.

phunkyfish avatar Jul 01 '25 08:07 phunkyfish

I tried passing attributes in a bunch of different ways. These are what I was trying to send - the ones that are not the standard ones that ffmpeg understands are prefixed with a ! |!X-Plex-Token',token,'!X-Forwarded-For','173.166.164.121','user-agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'

Most basic way is x= 'X-Plex-Token',token,'!X-Forwarded-For','173.166.164.121','user-agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0' url = https://epg.provider.plex.tv/library/parts/XXXXX/ allurl = url + '|' + x

    liz = xbmcgui.ListItem(path=allurl)
    liz.setPath(allurl)
    liz.setProperty('inputstream', 'inputstream.ffmpegdirect')
    liz.setProperty('IsPlayable', 'true')
    liz.setProperty('mimetype', 'application/x-mpegurl')
    liz.setProperty('inputstream.ffmpegdirect.stream_mode', 'timeshift')
    liz.setProperty('inputstream.ffmpegdirect.is_realtime_stream', 'true')
    liz.setProperty('inputstream.ffmpegdirect.manifest_type', 'hls')
    liz.setProperty('inputstream.ffmpegdirect.open_mode', 'ffmpeg')
    xbmcplugin.setResolvedUrl(__handle__, True, liz)

nictjir avatar Jul 01 '25 09:07 nictjir

I noticed that headers don't seem to work or at least the respective log debug message is not printed for stream urls with .ts file extension

#EXTINF:-1, Video item
#KODIPROP:inputstream=inputstream.ffmpegdirect
#KODIPROP:mimetype=application/vnd.apple.mpegurl
#KODIPROP:inputstream.ffmpegdirect.is_realtime_stream=true
http://example.com/stream/stream.m3u8|User-Agent=otg/1.5.1 (AppleTv Apple TV 4; tvOS16.0; appletv.client) libcurl/7.58.0 OpenSSL/1.0.2o zlib/1.2.11 clib/1.8.56

Shows:

CDVDDemuxFFmpeg::GetFFMpegOptionsFromInput() adding ffmpeg option 'user_agent: otg/1.5.1 (AppleTv Apple ....

But for .ts CDVDDemuxFFmpeg doesn't seem to be used and cUrl is used for IO instead. I assume headers are passed to cUrl instead but not sure.

jairoxyz avatar Oct 22 '25 09:10 jairoxyz