Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert .MTS files
#1
Hi everyone

I am trying to write a python program to do some video manipulation and uploading i.e. automate some boring tasks which I normally do with handbrake and FileZilla.

One task I am struggling to write python code for is to convert a .mts file (1980 x 1080 resolution) into an .MP4 H.264 video file at 1280 x 720 resolution.

Does anyone know of a video library that can handle the .MTS format  (this is video shot by a Canon video camera) as I have been unable to find any documented python libraries that can do so.

Any suggested code to achieve the above outcome would also be welcome. 

Thanks Peter
Reply
#2
Have you looked at this? https://gist.github.com/kracekumar/3929886 .
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
FFmpeg dos this fine,now is code in @sparkz_alot post a little old.
os.system() is deprecated,now we use subprocess.
Quote:a .mts file (1980 x 1080 resolution) into an .MP4 H.264 video file at 1280 x 720 resolution. 
If you use Windows use Zerano build(has libx264).
Something like this:
ffmpeg -i input.mts -c:v libx264 -vf scale=1280:720 -crf 23 output.mp4
To write a test,this use feature for newest version(like f-string) so Python 36.
Loop over files if .mkv convert all files to .mp4,this copy video format(faster that libx264).
import subprocess
import os

path = 'C:/ffmpeg_new/bin'
for file in os.scandir('.'):
    if file.name.endswith('mkv'):
        #print(file.name)
        file_path = os.path.join(path, file.name)
        subprocess.run(['ffmpeg', '-i', f'{file_path}', '-c:v', 'copy', f'{os.path.splitext(file.name)[0]}.mp4'])
Reply
#4
Thanks all. I decided to use FFMPEG and altered the code above to the following. First though I installed the Zerano build of FFMPEG.

Here's my code

import subprocess
import os

path = 'C:/test'
for file in os.scandir('.'):
if file.name.endswith('MTS'):
#print(file.name)
file_path = os.path.join(path, file.name)
subprocess.run(['ffmpeg', '-i', f'{file_path}', '-c:v', 'copy', f'{os.path.splitext(file.name)[0]}.mp4'])

In the subdirectory c:/test I have a small file called 00020.MTS which is a brief video. When I run the above script I get the following response before being returned to Idle.

>>>
== RESTART: C:/Users/Peter/AppData/Local/Programs/Python/Python36-32/t1.py ==
>>>

I then uncommented the command

#print(file.name)

and ran the script again but got the same result. The If.. command did not seem to be being triggered, In other words the python program was not seeing the . MTS file.

Why is this so? How can this be fixed.

That aside I must say I like the efficient way that files can be converted using FFMPEG.

cheers Peter
Reply
#5
I'll answer my own question. After some muddling around I found the following command gave me the output I wanted including mono output.

ffmpeg -i c:\test\00003.mts -r 30 -s 1280x720 -c:v libx264 -crf 23 -ac 1 c:\test\filename3.mp4

cheers Peter
Reply
#6
Another question if I may please.

The above ffmpeg command was successful in converting 00003.mts into filename3.mp4. I am now trying to work thsi into my python script.

Here's my script so far
import subprocess
import os, sys
path = "C:\\upload"
path2= "C:\\upload\completed"
dirs = os.listdir( path )
Count=0
d = []

for file in dirs:
   file_path = os.path.join(path, file)
   file_path2 = os.path.join(path2, file)
   print (file_path)
   os.system('ffmpeg -i file_path -r 30 -s 1280x720 -c:v libx264 -crf 23 -ac 1 file_path2')
   Count = Count+1 
and here's the output
Output:
>>> = RESTART: C:/Users/Peter/AppData/Local/Programs/Python/Python36-32/test2.py = C:\upload\00030.MTS C:\upload\00031.MTS C:\upload\00032.MTS C:\upload\00033.MTS C:\upload\completed >>>
The object of my script is to cycle through all the files in C:\upload, process each one and put the output in c:\upload\completed. As you can see I am running into a couple of problems. First my os.system() commands don't seem to be resulting in any processing. Nothing is appearing in the completed folder. Next my loop is capturing the text  'completed as as file which is not what I want. As you can see I have 4 files in the C:\upload directory. I just want to process these and finish. I would be grateful if you could point out what I am doing wrong. I did try to use the subprocess.run command but this did not work either.

Hoping someone can help.

cheers Peter
Reply
#7
os.system('ffmpeg -i file_path -r 30 -s 1280x720 -c:v libx264 -crf 23 -ac 1 file_path2')
You need to replace file_path and file_path2 with actual values of file_path and file_path2 variables. Use some sort of string formating or variable substition, as snippsat posted. And you should follow his other advice too - os.system() is deprecated, use subprocess. Checking if your file ends with ".MTS" and replacing it with ".mp4" for output file is good idea too.
Reply
#8
Apologies for not using the tags.

Ok I've recycled Snippsat's code as follows

 

import subprocess
import os
path = 'C:/upload'
for file in os.scandir('.'):
   if file.name.endswith('mts'):
       #print(file.name)
       file_path = os.path.join(path, file.name)
       subprocess.run(['ffmpeg', '-i', f'{file_path}', '-c:v', 'copy', f'{os.path.splitext(file.name)[0]}.mp4'])
This is the output I get

Output:
>>> = RESTART: C:/Users/Peter/AppData/Local/Programs/Python/Python36-32/test2.py = >>>
However nothing gets processed? I have 4 .MTS files in c:\upload but no .MP4 file is generated

Thanks for your patience

cheers Peter
Reply
#9
os.scandir('.') this mean that i run the script in folder where video file are.
To set in and out path can do it like this:
import subprocess
import os

path_in = 'C:/ffmpeg_new/bin'
path_out = 'C:/ffmpeg_new/'
for file in os.scandir(path_in):
    if file.name.endswith('mkv'):
        #print(file.name)
        file_path = os.path.join(path_in, file.name)
        subprocess.run(['ffmpeg', '-i', f'{file_path}', '-c:v', 'copy', f'{path_out+os.path.splitext(file.name)[0]}.mp4'])
To make it more general i would make a function,
where path_in,path_out and file extension is taken in as argument.
Shall see if i write something and post it here.
Reply
#10
Thank you

I've amended my code per your instructions. It is now

import subprocess
import os
path_in = 'C:/upload'
path_out = 'C:/upload/completed'

for file in os.scandir(path_in):
   if file.name.endswith('mts'):
       #print(file.name)
       file_path = os.path.join(path_in, file.name)
       subprocess.run(['ffmpeg', '-i', f'{file_path}', '-c:v', 'copy', f'{os.path.splitext(file.name)[0]}.mp4'])
but it still gives the same output

Output:
>>> = RESTART: C:/Users/Peter/AppData/Local/Programs/Python/Python36-32/test2.py = >>>
There are no rendered files in the c:/output/completed folder. 

cheers Peter

Oops - I've just seen an error

There doesn't appear to be path_out coded anywhere in your example in order to get the files into the output directory ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,568 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Separate text files and convert into csv marfer 6 2,886 Dec-10-2021, 12:09 PM
Last Post: marfer
  Convert a PDF files to HTML files Underground 4 12,155 Oct-25-2020, 09:12 PM
Last Post: Larz60+
  convert old excel files(xls) to xlsm zarize 1 3,391 Jul-14-2020, 02:12 PM
Last Post: DeaD_EyE
  How to convert python files from 32 bits tto 64 bits sylas 2 5,097 Oct-29-2017, 03:51 AM
Last Post: Larz60+
  Convert grib files to text files tuxman 6 9,325 Sep-15-2017, 03:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020