question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

yt-dlp in combination with ffmpeg returns wrong PID

See original GitHub issue

Checklist

Question

Hi there,

we have a problem with the returned PID’s from ffmpeg in combination with yt-dlp. We need a valid PID from ffmpeg to terminate the process via timer later.

Test the following little Python program and then you will see the problem:

#!/usr/bin/env python3

import subprocess
import time

link = "https://fashiontv-fashiontv-4-de.samsung.wurl.com/manifest/playlist.m3u8"

cmd_string = 'ffmpeg -i $(yt-dlp -g "' + link + '") -c:v copy -c:a copy "out.ts" 2> /dev/null'
#cmd_string = 'ffmpeg -i $(youtube-dl -g "' + link + '") -c:v copy -c:a copy "out.ts" 2> /dev/null'

for z in range(3):
    try:
        last_pid = int(subprocess.check_output(["pidof", "-s", "ffmpeg"]))
    except:
        last_pid = 0

    subprocess.Popen(cmd_string, shell=True)
    print("\n0.00 - 000 - {:d}".format(last_pid))

    start = time.time()
    new_pid = 0
    for x in range(700):
        try:
            new_pid = int(subprocess.check_output(["pidof", "-s", "ffmpeg"]))
        except:
            pass
        if not new_pid == last_pid:
            print("{:.2f} - {:3d} - {:d}".format(time.time()-start, x, new_pid))
            last_pid = new_pid

    print("{:.2f} - {:3d} - {:d}".format(time.time()-start, x, new_pid))
    print(subprocess.check_output(["pidof", "ffmpeg"]))

subprocess.Popen('killall ffmpeg', shell=True)

You can see that after about 0.5 seconds it returns an wrong PID, then it switched back to the old one and after some time the correct PID is returned. This is not how we can reliably determine a valid PID. If you try the same with youtube-dl everything is fine. What is it?

Thanks for your effort

Verbose log

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
fstirlitzcommented, Feb 18, 2022

The problem is that you have poor code. It’s just as ill-advised to do the same in production:

https://github.com/sc44/Stream-Recorder/blob/34dab4113c14860da33272d535a2985e4c9f4b87/srecorder.py#L927-L933

https://github.com/sc44/Stream-Recorder/blob/34dab4113c14860da33272d535a2985e4c9f4b87/srecorder.py#L1003

subprocess.Popen has a pid attribute. Use that one and never touch any other PID.

1reaction
fstirlitzcommented, Feb 18, 2022

This is some extremely poor code. pidof and killall are tools for sysadmins, not programmers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FFMPEG: Too many packets buffered for output stream 0:1
I disabled the audio stream on my camera and it works fine without this option. In manual I read about ffmpeg that will...
Read more >
Modules on CPAN alphabetically
The list contains modules distribution files on CPAN for modules that are not included in the perl standard distribution but are included in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found