I have a gif file with accompanying mp3 music.
My goal is to run the gif file with the mp3 music file and out-put it as an MP4 file. This works, HOWEVER
Once the file is outputted, I have some issues
Firstly, the gif runs only once and freezes after that, the music runs until the file ends.
Here is the fmpegg command I am using
subprocess.run(
["ffmpeg", "-i", gif_file, "-i", chosen_mp3, "-pix_fmt", "yuv420p", "-t", "10", "-loop", "0", output_file],
check=True,shell=True
)
How can I make it so that the gif does not freeze up after running once, i.e loops until end of file or loops for an N amount of times?
In the code above, I tried to make it loop for 10 seconds but still the same issue is happening.
Related
Is there a way to write for example every 2 seconds a sound in a (e.g. .mp3 or .wav) file in python?
For example if I have a loop like this
x=0
while x<10:
#write sound.wav into an audio file (file.mp3)
time.sleep(2)
x=x+1
So as a result I want to have an audio file where you hear a sound every 2 seconds (and that for example 10 times)
Thanks in advance
It doesn't work like that. If you sleep(2), the Python interpreter will just idle for 2 seconds and not do anything, i.e. it will pause any file writes and resume after the sleep is over. What you need to do is to write your sound data, then write two seconds of silence (zero-valued samples), then write the sound data again etc.
I have many robotframework test cases and in the first case, a ffmpeg command like the following is invoked to record the whole running process:
ffmpeg -framerate 30 -f gdigrab -i desktop -c:v libx264rgb -crf 0 -preset ultrafast output.mkv
Whenever I firstly run all cases and then manuually run the above command from an addition command console, the recorded video always looks fine, it looks like all contents on the screen can be correctly captured.
However, once I execute the command the same as the above one in the first case by call the following code:
p=subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
and then in the final test case the record process is stopped by calling the following code to tell ffmpeg that we want to stop the recording:
p.stdin.write(bytes("q",'UTF-8'))
the final result video only contain correct contents of the "start" and the "end" of the whole process, but all other contents no longer changed and seemd just a static image, which means all the dynamic effects on the screen cannot be captured.
Could anyone be so kind as to let me know what the matter is and how to solve it?
I was wondering how I could go about doing telling FFMPEG where to save files it converts, it keeps making the files save way too deep in my computer.
check_output("ffmpeg -i " + location + " -vn -ar 44100 -ac 1 -b:a 32k -f wav audio.wav", shell=True)
I use that in my program to convert the files to wav.
The question is totally unrelated to python.
I suggest stopping using shell=True, because that invokes a shell to call your subprocess, adding useless overhead (invoke a process to invoke another process). Another advantage is that it allows you to provide the command line as a list of parameters, which frees you from quote/space hell and is simpler.
That said, just add the folder to the destination filename and it will be generated there:
filename = 'audio.wav'
destination = r'C:\Some\Folder'
subprocess.check_output(["ffmpeg",
"-i", location,
"-vn",
"-ar", "44100",
"-ac", "1",
"-b:a", "32k",
"-f", "wav",
os.path.join(destination, filename)
])
I wrote code to play an audio file using python, like below.
def playSound(self):
os.system('start C:\\Users\\unavaras\\Music\\223.wav')
time.sleep(10)
It's playing the audio file recursively and it's not being closed untill i manually close it.
Tried below code as well to stop audio, but didn't work
def playSound(self):
p1 = Popen('start C:\\Users\\unavaras\\Music\\223.wav', shell=True)
time.sleep(5)
p1.kill()
In both cases audio is being played recursively.
Can some help me how I can stop the audio after some time or once it finishes playing.
I am using linux but you can try this
def playSound(self):
os.system('timeout 10 start C:\\Users\\unavaras\\Music\\223.wav')
For finding the time duration of audio this link
Get .wav file length or duration
I'm trying to convert a mp3 file on the fly in Python to wav file using ffmpeg.
I call it using subprocess, how can I get it's output to play it as wav on the fly wthout saving it as the file (or playing it while its converting) and then playing it?
This is what I have so far:
I'm using aplay just for a example.
FileLocation = "/home/file.mp3"
subprocess.call(["ffmpeg", "-i", FileLocation etc etc "newfail.wav"])
os.system("aplay ... ") #play it on the fly here
As far as I understand, if I put "-" as file name, it will output it instead to stdout, but I don't know how to read stdout...
To emulate source arg1 arg2 | sink shell command without the shell:
from subprocess import Popen, PIPE
source = Popen(['source', 'arg1', 'arg2'], stdout=PIPE)
sink = Popen(['sink'], stdin=source.stdout)
source.stdout.close()
source.wait()
sink.wait()