Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't spawn with quotes in options. #2542

Closed
physiii opened this issue Mar 21, 2020 · 4 comments
Closed

Can't spawn with quotes in options. #2542

physiii opened this issue Mar 21, 2020 · 4 comments

Comments

@physiii
Copy link

physiii commented Mar 21, 2020

System: Ubuntu 19.10, node -v v13.11.0

I need to reliably spawn and kill this process:

ffmpeg -re -i video.avi -f tee -codec:v mpeg1video -map 0:a -map 0:v -r 20
'[f=mpegts:select=v]http://127.0.0.1:8083/video_token/|[f=mpegts:select=a]http://127.0.0.1:8083/audio_token/'

I switched from double to single quotes (as mentioned in previous issues) to no avail.

I tried using shell: true but then I can't reliable .kill() the process. Same with execFile.

Also tried more options:

fileStreamProcess = spawn('ffmpeg', options, { shell: true, stdio: ["ignore", "pipe", "inherit"], detached: true });

How do I use spawn without the shell with an option that contains double quotes?

@mscdex mscdex transferred this issue from nodejs/node Mar 21, 2020
@Hakerh400
Copy link

  • What is your operating system?
  • What is your Node.js version?

I switched from double to single quotes (as mentioned in previous issues) to no avail.

Which issues?

Can you share the exact full code that your are using to spawn the process?

What exactly are you trying to achieve? The ffmpeg configuration you provided cannot be run on windows, since the official ffmpeg installation for windows does not include the tree output format decoder.

@physiii
Copy link
Author

physiii commented Mar 22, 2020

  • What is your operating system?
  • What is your Node.js version?

I switched from double to single quotes (as mentioned in previous issues) to no avail.

Which issues?

Added system details and issue link.

Can you share the exact full code that your are using to spawn the process?

Sure, see below. Or my repo

What exactly are you trying to achieve? The ffmpeg configuration you provided cannot be run on windows, since the official ffmpeg installation for windows does not include the tree output format decoder.

No sure what you are asking? I am trying to reliably run and kill that ffmpeg process like I've done hundreds of times over the past few years. Problem is quotes in the options mess things up and this seems to be a common issue.

	getAudioVideoStreamUrl (streamId) {
		let audioUrl = config.relay_server + ':' + (config.video_stream_port || defaultStreamPort) + '/' + streamId + '/' + audioStreamToken + '/',
			videoUrl = config.relay_server + ':' + (config.video_stream_port || defaultStreamPort) + '/' + streamId + '/' + videoStreamToken + '/';

		if (config.use_ssl) {
			audioUrl = 'https://' + audioUrl;
			videoUrl = 'https://' + videoUrl;
		} else {
			audioUrl = 'http://' + audioUrl;
			videoUrl = 'http://' + videoUrl;
		}

		let url =  "\'" + "[f=mpegts:select=v]" + videoUrl + "|" + "[f=mpegts:select=a]" + audioUrl + "\'";
		return url;
	}


	streamAudioFile (streamId, streamToken, file) {

		audioStreamToken = streamToken;

		let options = [
			'-loglevel', 'panic',
			'-re',
			'-i', file,
			'-f', 'tee',
				'-codec:v', 'mpeg1video',
				'-q:v', '0',
				'-b:v', '900k',
				'-c:a', 'mp2',
				'-flags', '+global_header',
				'-b:a', '128k',
				'-map', '0:a',
				'-map', '0:v',
				'-r', '20',
			this.getAudioVideoStreamUrl(streamId)
		];

		let command = this.printFFmpegOptions(options);

		if (fileStreamProcess) fileStreamProcess.kill();

		console.log(TAG, 'Starting audio/video file stream. Stream ID:', command);

		fileStreamProcess = spawn('ffmpeg', options, { stdio: ["ignore", "pipe", "inherit"] });
		fileStreamProcess.on('close', (code) => {
			console.log(TAG, `Audio/video file stream exited with code ${code}. Stream ID:`, streamId);
		});
	}

	printFFmpegOptions (options) {
		let options_str = 'ffmpeg';
		for (let i = 0; i < options.length; i++) {
			options_str += ' ' + options[i];
		}
		return options_str;
	}

I can see the process running from:

ps aux | grep ffmpeg

andy 17479 40.7 0.7 1856272 122080 ? SLl 08:21 0:02 ffmpeg -loglevel panic -re -i /usr/local/lib/open-automation/camera/events/b619eb40-6764-4612-90b1-d9067ab4c65e/2020/03/22/2020-03-22_07:51:01AM.avi -f tee -codec:v mpeg1video -q:v 0 -b:v 900k -c:a mp2 -flags +global_header -b:a 128k -map 0:a -map 0:v -r 20 '[f=mpegts:select=v]http://127.0.0.1:8083/e68f4f79-b2e1-46c5-b00c-855f3a9ff0a9/76bbf72d8d5df8d2500081b29f19854c106b14ec60ed505e966c794193e0c87cc47295b60c007dd921ef33e79875405326930f3b261fa6d88cef4a2c7774832ff5e6b99aa0c20e2deb50c93a546b3565bd25a43f914779dd8ff0e69483d3535b1bbbb9a74d10dbdc417b119cd4aae444dc147e7acbe4f3d4a02b2e265d67b62c/|[f=mpegts:select=a]http://127.0.0.1:8083/e68f4f79-b2e1-46c5-b00c-855f3a9ff0a9/7a541314e6c30257f2f4945eb68a529a6e03bc2d749f98d108aec35cc9c0e0171558cbe13ec19758899691f7e9c14a0a8fa06e5f80c017ea33e77ef6135b13989d9b6193b584aade05726a0af97cf34db8b1e7dff853b9a1c603655747b34e36f6615767a2c705e2a45f9f1b2889391770865c3391ae4916a5098e9db8b9255a/'

It shows the process running but it's something with how node is executing the command because if I just copy and paste the command from ps aux it works. Also if I use the shell: true it works.

@Hakerh400
Copy link

Added system details and issue link.

How is that issue related? That issue is about verbatim arguments for command prompt on windows, and you are using linux...

The ffmpeg configuration you provided cannot be run on windows, since the official ffmpeg installation for windows does not include the tree output format decoder.

No sure what you are asking?

I am talking about tree output format being excluded from the official ffmpeg binaries, both for windows and linux. You are probably using a custom ffmpeg build if that command works for you.

Back to your issue, I think you don't need any quotes at all. Try to change

let url =  "\'" + "[f=mpegts:select=v]" + videoUrl + "|" + "[f=mpegts:select=a]" + audioUrl + "\'";

to

let url =  "[f=mpegts:select=v]" + videoUrl + "|" + "[f=mpegts:select=a]" + audioUrl;

because spawn internally escapes each argument, so you don't need extra escaping.

@physiii
Copy link
Author

physiii commented Mar 22, 2020

because spawn internally escapes each argument, so you don't need extra escaping.

That worked!

Much appreciated.

@physiii physiii closed this as completed Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants