Python Forum
Convert .MTS files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Convert .MTS files (/thread-2843.html)

Pages: 1 2


Convert .MTS files - pberrett - Apr-14-2017

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


RE: Convert .MTS files - sparkz_alot - Apr-14-2017

Have you looked at this? https://gist.github.com/kracekumar/3929886 .


RE: Convert .MTS files - snippsat - Apr-14-2017

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'])



RE: Convert .MTS files - pberrett - Apr-15-2017

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


RE: Convert .MTS files - pberrett - Apr-15-2017

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


RE: Convert .MTS files - pberrett - Apr-15-2017

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


RE: Convert .MTS files - zivoni - Apr-15-2017

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.


RE: Convert .MTS files - pberrett - Apr-15-2017

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


RE: Convert .MTS files - snippsat - Apr-15-2017

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.


RE: Convert .MTS files - pberrett - Apr-15-2017

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 ?