Chris_J
Posts: 42
Joined: Wed Sep 18, 2013 3:14 pm

Running a bash command from python with arguments

Sat Sep 24, 2016 10:26 pm

I'm running a Python program that creates a video file and saves it to a location.

After the file has been saved, I'd then like to run a bash command that converts the video from a h264 file to an mp4. At the same time, I'd like to give the mp4 file a name that contains a timestamp but I can't figure out how to send the timestamp to the bash command. Here's what I have so far:

Code: Select all

...

convert_video = "MP4Box -add /home/pi/video.h264 /home/pi/videos/"+timestamp+".mp4"

while True:
    pir.wait_for_motion()
    while pir.motion_detected:
        print('Taking photo')
        ts = '{:%Y%m%d-%H%M%S}'.format(datetime.now())
        logging.info('Taking photo: '+ str(ts)+'.jpg')
        with picamera.PiCamera() as cam:
            cam.resolution=(1024,768)
            cam.annotate_background = picamera.Color('black')
            cam.start_recording('/home/pi/video.h264')
            start = datetime.now()
            while (datetime.now() - start).seconds < 5:
                cam.annotate_text = datetime.now().strftime('%d-%m-%y %H:%M:%S')
                cam.wait_recording(0.2)
            cam.stop_recording()
        time.sleep(0.5)
        timestamp = datetime.now().strftime('%d-%m-%y %H:%M:%S')
        call ([convert_video, timestamp], shell=True)
    print('Motion Ended')

...
The goal is to make sure all the saved mp4 files have a filename containing the time they were created.

Thanks,

Chris

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Running a bash command from python with arguments

Sun Sep 25, 2016 6:44 am

Try posting more code. You left a few holes.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Running a bash command from python with arguments

Sun Sep 25, 2016 6:55 am

Assuming format is

Code: Select all

MP4Box -add inputfile outputfile
I would do something like this:

Code: Select all

timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')  # I've removed colons and spaces
input_video = "/home/pi/video.h264"
output_video = "/home/pi/videos/{}.mp4".format(timestamp)
call(["MP4Box", "-add", input_video, output_video])
You should be able to adapt your code to fit this in.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

IanH
Posts: 68
Joined: Mon Dec 19, 2011 5:53 pm

Re: Running a bash command from python with arguments

Sun Sep 25, 2016 7:08 am

elParaguayo wrote:

Code: Select all

...
call(["MP4Box", "-add", input_video, output_video])
You'll need this

Code: Select all

from subprocess import call
at the top of the file. If you want to check whether the call has worked, do something like this:

Code: Select all

retcode = call(["MP4Box", "-add", input_video, output_video])
if retcode != 0:
    # It went wrong...
    print "Couldn't convert", input_video
For more information, the subprocess module docs are at: https://docs.python.org/2/library/subprocess.html
https://github.com/IanHarvey

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Running a bash command from python with arguments

Sun Sep 25, 2016 7:21 am

IanH wrote:
elParaguayo wrote:

Code: Select all

...
call(["MP4Box", "-add", input_video, output_video])
You'll need this

Code: Select all

from subprocess import call
at the top of the file. If you want to check whether the call has worked, do something like this:
Absolutely. I assumed it was already in the code as the OP's code used the "call" command.

Code: Select all

retcode = call(["MP4Box", "-add", input_video, output_video])
if retcode != 0:
    # It went wrong...
    print "Couldn't convert", input_video
Yes - helpful to include this too. Does rely on MP4Box having differing return codes but hopefully it does!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Chris_J
Posts: 42
Joined: Wed Sep 18, 2013 3:14 pm

Re: Running a bash command from python with arguments

Sun Sep 25, 2016 8:58 am

Thank you all to your quick replies.

Code: Select all

timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')  # I've removed colons and spaces
input_video = "/home/pi/video.h264"
output_video = "/home/pi/videos/{}.mp4".format(timestamp)
call(["MP4Box", "-add", input_video, output_video])
This helped a lot. I didn't quite grasp how to send arguments to bash but this clears it up nicely!

Many thanks,

Chris

Return to “Python”