nilaoda / N_m3u8DL-RE

Cross-Platform, modern and powerful stream downloader for MPD/M3U8/ISM. English/简体中文/繁體中文.
MIT License
4.75k stars 499 forks source link

PIPE to FFMPEG? #162

Open dromicron opened 1 year ago

dromicron commented 1 year ago

hi, i try to use pipe function but i only get .TS file it is possible to pipe to ffmpeg to make live hls output?

atuctuc commented 1 year ago

yes, i would also appreciate it if it could redirect the output to a unix pipe!!

edgar017 commented 1 year ago

@nilaoda how to use live pipe options?

nilaoda commented 1 year ago

@nilaoda how to use live pipe options?

For example:

set RE_LIVE_PIPE_OPTIONS="-c copy -f flv rtmp://your-streaming-server-url/your-stream-key"

N_m3u8DL-RE.exe --live-real-time-merge --live-pipe-mux "http://path/to/a.mpd"

This will change the command line used for mixing streams and output it to the server you specify. You can also output it as HLS segments by set specific value of RE_LIVE_PIPE_OPTIONS.

On Linux, If you only want it to take effect on a specific command line:

RE_LIVE_PIPE_OPTIONS="..." ./N_m3u8DL-RE ...
edgar017 commented 1 year ago

@nilaoda Very thanks!

Shporterator commented 1 year ago

I noticed that in this mode, temporary files are not deleted after completion. The folder remains The command looks like this. Maybe I did something wrong?

RE_LIVE_PIPE_OPTIONS="-c copy -f mpegts pipe:1" /home/N_m3u8DL-RE --log-level off --tmp-dir /tmp --no-log --ffmpeg-binary-path /home/ffmpeg --decryption-binary-path /home/mp4decrypt --key 0:0 "$URL" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" --del-after-done true --mp4-real-time-decryption true --live-keep-segments false --live-real-time-merge true --live-pipe-mux true --thread-count 3 -sv best -sa all

And I also noticed that errors in the image periodically freezes if you use this method. I can't figure out what this is related to yet. I tried different versions of ffmpeg, the result is the same

liimee commented 1 year ago

I noticed that in this mode, temporary files are not deleted after completion.

@Shporterator have you figured this out? noticed this too

Shporterator commented 1 year ago

have you figured this out?

No. Do you have any problems with the video when using this method?

liimee commented 1 year ago

And I also noticed that errors in the image periodically freezes if you use this method.

what do you mean by periodically freezes? the stream/pipe stops?

Shporterator commented 1 year ago

what do you mean by periodically freezes? the stream/pipe stops?

During the loading of new fragments, the image stops for a few seconds, the sound continues to work normally.

liimee commented 1 year ago

I think that happens to me too... the video fragment loads way slower (pauses for a moment) than the audio

nilaoda commented 1 year ago

You can try set a smaller value for --live-wait-time

Shporterator commented 1 year ago

You can try set a smaller value for --live-wait-time

Problem remains

nilaoda commented 1 year ago

@Shporterator Then there's nothing I can do. 🫠

Shporterator commented 1 year ago

Then there's nothing I can do.

Very sad

Shporterator commented 1 year ago

Then there's nothing I can do.

This problem is absolutely with any streams, which makes this new feature useless:(

andremxmx commented 1 year ago

Then there's nothing I can do.

This problem is absolutely with any streams, which makes this new feature useless:(

Yep i have the same problem, fo now just do this, maybe you do the same as me for now

N_m3u8DL-RE.exe --save-dir stream --del-after-done true --save-name tNoticias --tmp-dir temp "url" --live-real-time-merge true --live-wait-time 5 --mp4-real-time-decryption true --live-pipe-mux -sv best -sa all --key xxxxxxxxxx:xxxxxxxxx --use-system-proxy true --custom-proxy http://xxx.xxxx.xxx.xxx:3128

FFMPEG

ffmpeg.exe -re -i stream/tNoticias.ts -c copy -f hls -hls_time 5 -hls_list_size 4 -hls_flags delete_segments C:/wamp64/www/bin/m3u8/tNoticias.m3u8

Shporterator commented 1 year ago

Yep i have the same problem, fo now just do this, maybe you do the same as me for now

How can I contact you?

andremxmx commented 1 year ago

Telegram: @cursoryapp_uy

Yep i have the same problem, fo now just do this, maybe you do the same as me for now

How can I contact you?

andremxmx commented 1 year ago

Now working grate no jittler or audio loss or stuck, perfect!! thanks!

alphalneo commented 1 year ago

To me the problem seems to be the output Vid 1920x1080 | 5499 Kbps | 50 00m38s/00m55s 16/23 Recording 69% 0.00Bps(1) ⣟ Aud 129 Kbps | ita | 2CH 00m38s/00m55s 16/23 Recording 69% 0.00Bps(1) ⣯ Can we suppress easily?

Edited: Yes I confirmed, I changed pipe:1 to pipe:2 I sent the stdout to dev null and finally I resent stderr to stdout and evrything is working!!! Therefore is the contamination of the std out that create the problem

michaelarnauts commented 1 year ago

To me the problem seems to be the output Vid 1920x1080 | 5499 Kbps | 50 00m38s/00m55s 16/23 Recording 69% 0.00Bps(1) ⣟ Aud 129 Kbps | ita | 2CH 00m38s/00m55s 16/23 Recording 69% 0.00Bps(1) ⣯ Can we suppress easily?

Edited: Yes I confirmed, I changed pipe:1 to pipe:2 I sent the stdout to dev null and finally I resent stderr to stdout and evrything is working!!! Therefore is the contamination of the std out that create the problem

Could you share the full command that you used to make this work?

alphalneo commented 1 year ago

Sure, I am using linux

RE_LIVE_PIPE_OPTIONS="-c copy -f mpegts pipe:2" /MYPATH/N_m3u8DL-RE --log-level off --no-date-info --no-log True THEURLOFYOURSTREAM --del-after-done true --mp4-real-time-decryption true --live-keep-segments false --live-real-time-merge true --live-pipe-mux true --thread-count 3 -sv best -sa all --concurrent-download --save-dir pippo_FOLDER 2>&1 >/dev/null

The output is N_m3u8DL-RE is done on STDERR, I am sending the SDTOUT to a devnull and I am sending the STDERR to STDOUT

alphalneo commented 1 year ago

Waiting for a solution, for now I solved in this way modify this line var progress = AnsiConsole.Progress().AutoClear(true); which you can find in 3 files with this var progress = AnsiConsole.Progress().AutoClear(true).AutoRefresh( DownloaderConfig.MyOptions.LogLevel != LogLevel.OFF );

alphalneo commented 1 year ago

Thank you!

JohnTrabusca commented 1 year ago

@nilaoda is it possible to change the pipe file location (RE_pipe_dc3a37d4-9ef7-482f-975a-e6a6fa6579bf) to another folder instead of /tmp ?

nilaoda commented 1 year ago

@nilaoda is it possible to change the pipe file location (RE_pipe_dc3a37d4-9ef7-482f-975a-e6a6fa6579bf) to another folder instead of /tmp ?

Latest Actions version supported RE_LIVE_PIPE_TMP_DIR environment var.

JohnTrabusca commented 1 year ago

@nilaoda It still creates to the default /tmp after setting the new path per example: /tmp/ramdisk

Not sure if I'm doing something wrong.

nilaoda commented 1 year ago

@JohnTrabusca

RE_LIVE_PIPE_TMP_DIR="/data/my_temp" ./N_m3u8DL-RE xxx
JohnTrabusca commented 1 year ago

After setting and doing a stat to that file (RE_pipe) in the given path in logs, it cannot be found.

neorom23 commented 8 months ago

Hello. There is such a link mpd...

https://vs12.vcdn.biz/ea7c2a50874e06892616c81fed8764f1_mgg/vod/mpd/b/450_900_1350_1500_2000_5000/u_sid/0/o/176166551/rsid/8a209735-8af0-46f9-afc9-5aae98954316/u_uid/1546846946/u_vod/4/u_device/cms_html5/u_devicekey/_site/u_srvc/2741/te/1760354712/u_did/web_ufUvWAJW4hh3VWBsJtUHNwHAi8JtabBU/wtrmrk/0/manifest.mpd

The program N_m3u8DL-RE 0.2.0 Beta normally downloads temporary video mp4 files 142 pieces (each size 10.0 MB), adds the disputed one 143 piece and the last one has a file size of 1.5 GB. Collect together the mp4 files have a size of - 2.11 GB with a duration of 01:15:38, although it should be 1.39 GB with a duration of 00:49:59 (only the video without sound and subtitles)

Here are the options available on the command line - N_m3u8DL-RE "%mpd%" --check-segments-count:false --save-name "02" --key 7d91116c8f0b95b8db4953f1f26e6aa2:5a4491ef67bc263c0 125c5914322dfba

yt-dlp - this program load normally video size 1.39 GB

dark125 commented 7 months ago

There is a problem with this option RE_LIVE_PIPE_TMP_DIR="/tmp/channel", the pipe file keeps being created in the /tmp/ folder and N_m3u8DL-RE looks for that file in /tmp/channel/, that's why it gives an error /tmp/channel/RE_pipe_f2eee6bf-6068-43aa-b36a-ac8bca0e5677: No such file or directory

Technerd-SG commented 5 months ago

I have the same problem I sovled it like so:

streamlink "http://stream.example/manifest.mpd" best --ffmpeg-dkey "123456789abcdefghijklmnopqrstuvwx" --stdout | ffmpeg -re -i pipe:0 -c:v libx264 -preset veryfast -tune zerolatency -c:a aac -f flv rtmp://server.ip/stream1/stream

the normal streamlink software doesnt support this you would need to use: "https://github.com/sunghome/streamlink_optionalkey". This is not a good solution... Please implement a pipe function!...

mamontovmv commented 4 months ago

Now working grate no jittler or audio loss or stuck, perfect!! thanks!

How did you fix this problem?

matthias1232 commented 3 months ago

i compiled the changes what @alphalneo said. https://github.com/matthias1232/N_m3u8DL-RE/releases/tag/1.0

This Script works for me with TVHeadend Server:

#!/bin/sh
rm /var/www/html/tv/yourchannel/temp -rf
export RE_LIVE_PIPE_OPTIONS="-c copy -f mpegts pipe:2"
./N_m3u8DL-RE --log-level off --no-date-info --no-log True --key yourkey1 --key yourkey2 https://example.com/manifest.mpd --del-after-done true --mp4-real-time-decryption true --live-keep-segments false --live-real-time-merge true --live-pipe-mux true --thread-count 3 -sv best -sa all --concurrent-download --tmp-dir /var/www/html/tv/yourchannel/temp --save-dir /var/www/html/tv/yourchannel/temp --save-name temp 2>&1 >/dev/null
pid=$!

# If this script is killed, kill the `N_m3u8DL-RE'.
#trap "kill $pid 2> /dev/null" EXIT
# While N_m3u8DL-RE is running...
while kill -0 $pid 2> /dev/null; do
    find /var/www/html/tv/yourchannel/temp -mmin +1 -type f -exec rm -fv {} \;
    sleep 1
done

# Disable the trap on a normal exit.

In TVheadend add the MUX: pipe:///var/www/html/tv/yourchannel/stream.sh

matthias1232 commented 2 months ago

I got it working now without any problems with Jitter, Stream Stucking lost Packets etc.... but the initial stream loading Time is incredibly slow now. The Stream Start Takes around 10-15 Seconds now. Maybe we can reduce the time to a Minimum, any ideas?

#!/bin/sh
rm /var/www/html/temp/1 -rf
mkdir /var/www/html/temp/1 -p
export RE_LIVE_PIPE_OPTIONS="-movflags faststart -loglevel error -map 0 -c:a copy -c:v copy -c:s copy -f mpegts -y pipe:"
/opt/N_m3u8DL-RE_Beta_linux-arm64/N_m3u8DL-RE --header "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" https://orf3-247.mdn.ors.at/orf/orf3/qxa-247/manifest.mpd -no-log true --use-ffmpeg-concat-demuxer false --no-date-info false --live-real-time-merge true --live-pipe-mux true --save-dir /var/www/html/temp/1 --tmp-dir /var/www/html/temp/1 --check-segments-count false --live-fix-vtt-by-audio true --mp4-real-time-decryption true --skip-merge false --del-after-done true --save-name temp -mt TRUE --thread-count 16 --auto-select --live-take-count 4 --live-wait-time 9 -M format=mp4:muxer=ffmpeg | ffmpeg -loglevel fatal -i pipe:0 -map 0 -c:a copy -c:v copy -c:s copy -mpegts_service_type advanced_codec_digital_hdtv -f mpegts -y pipe:1

The MPD Dash is not DRM Protected in my example. You should be able to try it.

matthias1232 commented 2 months ago

Hi again, i made a new Version of N_m3u8DL-RE. This should fix the issues with livestreams. I disabled the ffmpeg -re feature, so pipe will send out the first segments directly to the clients, and then stream as usual when new segments are downloaded. i also added ffmpeg options: -rtbufsize 1G -y -max_interleave_delta 100M -fflags +fastseek+ignidx+discardcorrupt+shortest+genpts

https://github.com/matthias1232/N_m3u8DL-RE/releases/tag/1.8

Im not sure if my version breaks other features, but my purpose is livestreams with TVHeadend, and this works well now.

A working script for tvheadend could look like this:

!/bin/sh

rm /var/www/html/temp/1 -rf rm /var/www/html/save/1 -rf mkdir /var/www/html/temp/1 -p mkdir /var/www/html/save/1 -p export RE_LIVE_PIPE_OPTIONS="-dn -drop_pkts_on_overflow 1 -attempt_recovery 1 -recover_any_error 1 -map_metadata:g 1:g -loglevel quiet -mpegts_service_type advanced_codec_digital_hdtv -protocol_whitelist pipe,file,http,https,tcp,tls,crypto -f mpegts -protocol_whitelist pipe,file,http,https,tcp,tls,crypto -movflags +delay_moov+default_base_moof+cmaf+frag_every_frame+separate_moof+rtphint+omit_tfhd_offset -rtpflags +h264_mode0 pipe:1" /opt/N_m3u8DL-RE_Beta_linux-arm64/N_m3u8DL-RE /opt/N_m3u8DL-RE_Beta_linux-arm64/N_m3u8DL-RE --header "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" --key xxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxx --key xxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxx https://orf1-247.mdn.ors.at/orf/orf1/drmqxa-247/manifest.mpd --save-dir /var/www/html/save/1 --save-name temp --tmp-dir /var/www/html/temp/1 --auto-subtitle-fix true --live-fix-vtt-by-audio true --mp4-real-time-decryption true --use-ffmpeg-concat-demuxer true --no-date-info --download-retry-count 0 --check-segments-count true --live-real-time-merge false --live-keep-segments false --auto-select --live-take-count 3 --force-ansi-console false --del-after-done false --no-log --log-level OFF --live-pipe-mux true

chmod +x /var/www/html/stream-10.sh

Tvheadend mux:

pipe:///var/www/html/stream-10.sh

ivovas commented 2 months ago

Hello, I am trying to decrypt an MPD source and push it to RTMP using the following command:

export RE_LIVE_PIPE_OPTIONS=' -c copy -f flv rtmp://ip:port/live/test_turk ';
N_m3u8DL-RE 'url' --key 'key:key' --use-shaka-packager true --ffmpeg-binary-path /bin/ffmpeg --decryption-binary-path bin/packager-linux-x64 --del-after-done true --mp4-real-time-decryption true --live-keep-segments false --binary-merge --live-pipe-mux true --thread-count 5 --use-ffmpeg-concat-demuxer --auto-select --save-name 10dc0e29-521b-4861-9ee8-3903015a1400

The issue I'm encountering is that the RTMP process stops unexpectedly after around 10 minutes, and I have no idea why. There is nothing in the logs that indicates what might be causing the problem.

Does anyone have any suggestions on how to troubleshoot or resolve this?