I'm trying to convert one of my CCTV RTSP streams to RTMP so I can add it onto a website. I've read many articles and understand that ffmpeg for Ubuntu 18.04 is the way to go.
I my script I've added the following code into Python 3.7:
ffmpeg -i "rtsp://xx.xxx.xx.x:554/user=admin&password=123#&channel=1&stream=0.sdp" -f flv -r 25 -s 640x480 -an "rtmp://localhost:1935/uid/1/camera1"
but I get a syntax error in the quote mark at the end of the first link (rtsp link) I've searched for hours and can't see what anyone has mentioned this before. I've also reviewed the ffmpeg documentation and can't see any issues.
Please can anyone point me in the right direction?
Thanks
Chris
According to the comment, you can't do it like this, since you can't directly execute commands from Python.
The simplest way to do it is by using os.system:
import os
os.system('ffmpeg -i "rtsp://xx.xxx.xx.x:554/user=admin&password=123#&channel=1&stream=0.sdp" -f flv -r 25 -s 640x480 -an "rtmp://localhost:1935/uid/1/camera1"')
Be careful, you either have to use single quotation marks as I did, or to escape double quotation marks.
A cleaner way to do it may use subprocess, to redirect stderr and stdout into Python variables for instance.
Related
I'm trying to convert a video between two file types, when run, nothing happens and no file is made. I've tried doing it with subprocess and os - both have the same result - nothing.
I can do the command fine through shell. I really want to be able to use this through python.
import subprocess
command = "ffmpeg -i X:/Desktop/twd.mp4 X:/Desktop/twd.mp3"
subprocess.run(command.split(),shell=True)
nothing happens and no file is made
This is very strange. Absolutely no text printed on console? I agree #Rotem's suggestion, but at the minimum the version info should print on your console (assuming you are using one of prebuilt binaries). BTW, shell=True is not needed and not recommended.
If you wish, you can give my ffmpegio package a try, it might make your life a bit easier.
import ffmpegio
ffmpegio.transcode('X:/Desktop/twd.mp4','X:/Desktop/twd.mp3')
It should auto detect your ffmpeg binaries and run your example above.
Couple days ago I ran script using watch:
watch -c -n 300 python3 some_script.py
Did not thought that I would like to have possibility to look at the log of this and I did not redirected the output to a separate log.txt file.
Is there any possibility that I can look the output up?
The watch command is still running and the session is active.
I wasn't able to find any solution for this, but maybe I did not look hard enough. If so - sorry.
Best regards,
matmakonen
use an external command witch is tee.
so your final command should be :
watch -c -n 300 python3 some_script.py | tee log.txt
as #RamanSailopal mentioned, There are no historic logs saved. But if you want to change files in the future, you do not necessarily have to use the external packages. You can use &> or >> or >.
See here and here for more details.
I have a python tool that concatentates several mp4s together into a single movie file using FFMPEG run from a subprocess call. The issue is that an arbitrarily large number of inputs in the command string will hit the windows CLI character max.
ffmpeg -y -i a.mp4 -i b.mp4 -i c.mp4 -f lavfi -t 0.1 -i anullsrc -filter_complex '[0]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2: (oh- ih)/2,setsar=1[0:v];[1]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(owiw)/2:(oh-ih)/2,setsar=1[1:v];[2]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow- iw)/2:(oh-ih)/2,setsar=1[2:v]; [0:v][3:a][1:v][3:a][2:v][3:a]concat=n=3:v=1:a=1[v][a]' -c:v libx264 -map [v] -map [a] output.mp4
Assuming 3 input movies that may or may have audio (hence the null audio source).
This works fine assuming there are no more than 30-50 input files. Often the input files have rather long filepaths and can get long very quickly.
After doing a bit of reading I tried a simple:
ffmpeg -f concat -i files.txt -c copy output.mp4
Which works if I can guarantee that all the files are the same codec/resolution/etc, but this cannot be guaranteed.
I'm not clear on how to translate my filter_complex to work with a text file of inputs. Finding a specific enough solution has proven elusive.
I'd be perfectly fine assigning the input paths to a temporary environment variable but in my use case this doesn't seem to work either. The subprocess call doesn't seem to translate wildcard %MYVAR% calls to environment variables.
I feel like there has to be an answer to this problem. If anyone can suggest a solution I would greatly appreciate it.
Thanks!
I'm working on a python script that does a custom conversion of videos via ffmpeg.
My problem is that the execution of ffmpeg stops suddenly after a bit of conversion (usually 3-4 mb out of 100mb) with a None exit code.
I'm using the pexpect library. Currently I do not check the progress but I will in a close future. It seems that I am right using pexpect regarding these questions FFMPEG and Pythons subprocess and Getting realtime output from ffmpeg to be used in progress bar (PyQt4, stdout). This is the command that I'm running (I have checked that it's exactly this one)
nice ffmpeg -i '/full/path' -s 640x360 -strict experimental -vcodec libx264
-f mp4 - coder 0 -bf 0 -refs 1 -flags2 -wpred-dct8x8 -level 30 -crf 26
-bufsize 4000k -maxrate 350k -preset medium -acodec libvo_aacenc
-ar 48000.0 -ab 128K -threads 2 -y '/full/path/out'
I am using nice but I have tried also without it and the result ends up being the same.
I'm running pexpect this way:
output, exit = pexpect.run(self.command(), withexitstatus=True,\
logfile=logfile)
print output
print exit
Of course I have tried the same command on the command line and it works fine.
Any clue on what might be happening?
The problem ended up being a bug in pexpect run function regarding the timeout. I have found the bug that was reported before me (yes, I forget to check ;))
http://sourceforge.net/tracker/?func=detail&aid=3316509&group_id=59762&atid=492077
Sadly the bug is really old and it explains how to solve the little bug. As a workaround I could just rewrite my code using spawn instead.
I don't really like the idea of using a code with trivial bugs that is still unmaintained so I have writen the code using Popen as Albert suggested.
I will wait some time to make the choice of going taking the chances with pexpect (if the code seems reliable).
For the record, here is my working code:
output = file(LOG_FILE, 'a')
args = shlex.split(self.command_video())
return subprocess.call(args, stdout=output, stderr=output)
Thanks for the help.
I am attempting to call ffmpeg to create an image from a frame in a video, I am using python to do this with subprocess.Popen on a mac, eventually this will move to a unix server.
I can successfully create a video from the command line with this line
ffmpeg -i /Users/bimemployee/Movies/ski\ commute.m4v -r .5 -vframes 1 -ss 00:01:14 /Users/bimemployee/Movies/untitled\ folder/image-%d.jpeg
I then turn this into a python iterable and passed it Popen
s=["ffmpeg","-i","Users/bimemployee/Movies/ski\ commute.m4v","-r","1","-vframes","1","-ss","00:01:14","/Users/bimemployee/Movies/untitled\ folder/image-%d.jpeg"]
subprocess.Popen(s)
When I do so I get the standard info screen from ffmpeg and an error that says Users/bimemployee/Movies/ski\ commute.m4v: No such file or directory
Why would this path work ok from the command line but not from python?
Secondly is their a better library for handling this, the ones I could find don't seem to be active projects or don't work with straight python but require things like cython.
Thanks,
CG
You're missing the opening forward slash:
/Users/bimemployee/Movies/ski_commute.m4v
is not the same as
Users/bimemployee/Movies/ski_commute.m4v