You are not logged in.

#1 2014-02-24 20:49:23

colbert
Member
Registered: 2007-12-16
Posts: 809

Batch converting .MP4 > flac?

I have an alias in my bash for converting single files like this (just using ffmpeg), but how would I do it batch? I need this for a project where we have a few hundred short .MP4 clips that we need to put to audio only, appreciate any help!

Offline

#2 2014-02-24 20:58:00

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Batch converting .MP4 > flac?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2014-02-24 22:03:59

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: Batch converting .MP4 > flac?

Possibly this:

#!/usr/bin/python
# -*- coding: utf8 -*-
# Dieses Script konvertiert alle im gegenwärtigen Verzeichnis gefundenen Mediadateien 
# in MP3 Dateien. Also beispielsweise Youtube Downloads. 

# Importiere OS Modul
import os

# Modul fuer reguläre Ausdruecke
import re

# Modul für Multiprocessing Funktionen importieren
import multiprocessing

# Queue für gefundene Mediadateien initialisieren
q = multiprocessing.Queue()

# Funktion zum Encoden von MP3 Dateien
def encodeAudio ():
  while not q.empty():
    # Nächsten Eintrag der Datei-Queue holen
    sourcefile = q.get()
    print sourcefile
    targetfile = sourcefile[:-3] + 'mp3'
    commandline = "ffmpeg -i \"" + sourcefile  +  "\" -acodec libmp3lame -ac 2 -ab 192k -vn -y \""+ targetfile + "\""
    os.popen (commandline) 

# Herausfinden wie viele CPUs / Kerne der Host hat
cores=multiprocessing.cpu_count()

# Definition der unterstützten Dateiendungen
extensions = ['avi','flv', 'mp4', 'wav', 'ogg', 'mpg', 'aac', 'flac', 'm4a' ]

# Vergleichspattern "zusammenbauen"
pattern = ''

for n in extensions:
  # Trennzeichen anhängen, falls bereits pattern definiert sind
  if len(pattern) > 0: pattern += '|'
  pattern = pattern + n  + '|' + n.upper() 

# RegEx für gesuchte Dateiendungen zusammenbauen
expr = re.compile (".*\.(" + pattern  + ")$")

# Alle Dateien der aktuell bearbeiteten Verzeichnis einlesen
files = os.listdir('.')  

# Nur Mediadateien bearbeiten
for n in files:
  # Gefundene Mediadateien der Verarbeitungsqueue hinzufügen
  if expr.match (n): 
    q.put(n)
    print (n)
# Spawnen entsprechend vieler Prozesse für das parallele Audioencoding
for i in range (cores):
  process = multiprocessing.Process(target=encodeAudio, args=())
  process.start()

..if you do not mind german comments in the code and adjust the ffmpeg commandline ;-)


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#4 2014-02-24 22:07:54

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Batch converting .MP4 > flac?

@Darksoul71
OP wants flac, not mp3s, but otherwise it just might work :-)

Offline

#5 2014-02-24 22:11:26

WorMzy
Administrator
From: Scotland
Registered: 2010-06-16
Posts: 12,600
Website

Re: Batch converting .MP4 > flac?

for file in *.mp4; do
  function "$file"
done

Profit?

Last edited by WorMzy (2014-02-24 22:25:48)


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#6 2014-02-24 22:21:36

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Batch converting .MP4 > flac?

WorMzy wrote:
for file in *.mp4; do
function $file
done

Profit?

Not if the filenames contain spaces... tongue


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2014-02-24 22:26:13

WorMzy
Administrator
From: Scotland
Registered: 2010-06-16
Posts: 12,600
Website

Re: Batch converting .MP4 > flac?

I knew I forgot something, I thought it was just indentation. sad


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#8 2014-02-24 23:19:24

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: Batch converting .MP4 > flac?

jasonwryan wrote:
WorMzy wrote:
for file in *.mp4; do
function $file
done

Profit?

Not if the filenames contain spaces... tongue

The glob won't be word expanded so the iteration will work. Quotes around the variable in the loop will make it safe, i.e.

for file in *.mp4; do
function "$file"
done

Otherwise,

#!/bin/bash
for file in "$@"; do
funcion "$file"
done

and then invoke the script with "script *.mp4".


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#9 2014-02-25 01:06:26

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Batch converting .MP4 > flac?

To expand on what others have said:

for i in *.mp4; do
    out=$(echo $i | sed -e 's/.mp4//g')
    ffmpeg -i "$i" "$out.flac"
done

This requires sed and needs to be run in the directory with the mp4 files. It will remove the ".mp4" suffix and it will replace it with ".flac". It's kind of ugly as it's not generic and uses FFmpeg's defaults for flac, but... whatever.

Offline

#10 2014-02-25 01:15:51

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Batch converting .MP4 > flac?

Or, without sed:

 ffmpeg -i "$i" "${i/mp4/flac}"

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#11 2014-02-25 01:15:56

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Batch converting .MP4 > flac?

Would

for i in *.mp4; do
    ffmpeg -i "$i" "${i%%.*}.flac"
done

work?

Offline

#12 2014-02-25 01:59:47

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Batch converting .MP4 > flac?

I think that we have a good script assuming that all of the files are in the same directory:

#!/bin/bash
for file in "$@"; do
    ffmpeg -i "$file" "${file%%.*}.flac"
done

As Xyne said, run the script with "script *.mp4". Or more generic:

<scriptname> *.<media_file_extension>

karol, I'll add this to the karol_rocks.pdf file. If you'd slow down, I may get us ready for release.

Last edited by skottish (2014-02-25 02:05:07)

Offline

#13 2014-02-25 02:18:26

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Batch converting .MP4 > flac?

With the scripts mentioned in previous replies, you might consider using ampersands to exploit multiple CPUs -- although you'll want to do it in a way that avoids starting too many processes simultaneously.

Another way of doing it:

$ find ./ -type f -regex ".*\.mp4" -exec ffmpeg -i {} {}.flac \;

(... or something.)


Edit: The answers to this serverfault question about parallelising unix find offer more interesting hints along these lines.

Last edited by /dev/zero (2014-02-25 02:27:34)

Offline

#14 2014-02-25 02:31:30

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Batch converting .MP4 > flac?

/dev/zero wrote:

With the scripts mentioned in previous replies, you might consider using ampersands to exploit multiple CPUs -- although you'll want to do it in a way that avoids starting too many processes simultaneously.

Another way of doing it:

$ find ./ -type f -regex ".*\.mp4" -exec ffmpeg -i {} {}.flac \;

(... or something.)


Edit: The answers to this serverfault question about parallelising unix find offer more interesting hints along these lines.

FFmpeg is multithreaded, so there may be a slight performance loss in trying to instantiate more instances.

Offline

#15 2014-02-25 02:50:41

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Batch converting .MP4 > flac?

skottish wrote:

FFmpeg is multithreaded, so there may be a slight performance loss in trying to instantiate more instances.

Interesting. I didn't realise that, thanks for the pointer. But now in this case, I see this: https://superuser.com/questions/538164/ … -parallel/

In particular, one commenter indicates that using ffmpeg's inbuilt threading doesn't yield as much efficiency as they expected.

I guess an optimal outcome could be found by trying the script multiple times, varying the number of permitted processes and threads ...

Offline

#16 2014-02-25 03:00:44

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Batch converting .MP4 > flac?

/dev/zero wrote:

In particular, one commenter indicates that using ffmpeg's inbuilt threading doesn't yield as much efficiency as they expected.

Multithreading is efficient when forking processes makes sense. FFmpeg, x264, and others will only use as much CPU power as they need. In this case, generating flac files requires very little overhead as it's not transcoding much of anything. I'm not suggesting that passing multiple instances of FFmpeg won't help, but I'm not sure in this case that it will.

Last edited by skottish (2014-02-25 03:02:58)

Offline

#17 2014-02-25 06:33:01

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: Batch converting .MP4 > flac?

karol wrote:

@Darksoul71
OP wants flac, not mp3s, but otherwise it just might work :-)

Shure...I know...that is why I wrote he needs to adjust the ffmpeg commandline smile

Also ffmpeg is not mandatory. You can easily adjust the commandline inside the Python script to use any tool of your choice. The big bonus is that the script maxes out your CPU as well as your processing speed.

@others: I never found the multithreading parts of ffmpeg too impressive. At least unless I run it for h264 encoding where it really maxes out my AMD quadcore. So far my approach to spawn a dedicated process for each file and each CPU core plus some dirty queue handling worked like a charm. As for FLAC encoding I can not tell.

Last edited by Darksoul71 (2014-02-25 06:36:46)


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#18 2014-02-25 14:44:55

ANOKNUSA
Member
Registered: 2010-10-22
Posts: 2,141

Re: Batch converting .MP4 > flac?

karol wrote:

@Darksoul71
OP wants flac, not mp3s, but otherwise it just might work :-)

Transcode from a lossy to lossless codec, and all you get is bigger files. The OP wants mp3 (or ogg wink ), but just doesn't realize it. Also, since mp4 is a container, the OP will probably want to know what the actual audio codec used is in order to get the best results.

EDIT: Come to think of it, I don't have time to look for any options right now, but it's probably possible to use something like k9copy (or some other video ripping/transcoding solution) to just strip the audio straight out of the mp4 file.

Last edited by ANOKNUSA (2014-02-25 14:47:11)

Offline

#19 2014-02-25 14:50:25

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Batch converting .MP4 > flac?

ANOKNUSA wrote:
karol wrote:

@Darksoul71
OP wants flac, not mp3s, but otherwise it just might work :-)

Transcode from a lossy to lossless codec, and all you get is bigger files.

Yup.

ANOKNUSA wrote:

The OP wants mp3 (or ogg wink ), but just doesn't realize it.

Not necessarily, maybe the input options fro whatever he wants to do with these audio files are limited and flac has been deemed the optimal solution.
I hope he does realize that flac doesn't magically equal unparalleled quality.

Offline

#20 2014-02-25 15:19:03

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: Batch converting .MP4 > flac?

Lol...in the end I tend not to question the purpose of things that someone asks smile
Especially since there are sometimes special use cases / scenarios which require things that does not seem to make sense for other peoples (e.g. transcoding from lossy to lossy).


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#21 2014-02-25 19:13:23

DrZaius
Member
Registered: 2008-01-02
Posts: 193

Re: Batch converting .MP4 > flac?

skottish wrote:

FFmpeg is multithreaded, so there may be a slight performance loss in trying to instantiate more instances.

Not all encoders have threading capabilities however:

$ ffmpeg -h encoder=flac 2>&1 | grep -i thread
    Threading capabilities: no

...but this "Threading capabilities" may only be for showing frame/slice threading support for video encoders. I didn't check the code...

Last edited by DrZaius (2014-02-25 19:18:41)

Offline

#22 2014-02-25 20:15:34

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: Batch converting .MP4 > flac?

ANOKNUSA wrote:

Transcode from a lossy to lossless codec, and all you get is bigger files. The OP wants mp3 (or ogg wink ), but just doesn't realize it.

That occurred to me as well but I ended up just shrugging and mumbling "bah, whatever".


Incidentally, threads such as this make me really appreciate this community. A dead-simple script to batch convert files goes through multiple iterations that lead to a clearly improved version and ultimately starts a conversation about the efficiency of multithreading. Nice.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#23 2014-02-25 21:09:03

defears
Member
Registered: 2010-07-26
Posts: 218

Re: Batch converting .MP4 > flac?

This is what I use for 6 processors. If you mean multi-threading. I don't see why ffmpeg won't work.

find . -type f -name '*.wav' -print0 |xargs -0 -P 6 -n 1 lame --preset extreme

Offline

#24 2014-02-25 21:15:33

ANOKNUSA
Member
Registered: 2010-10-22
Posts: 2,141

Re: Batch converting .MP4 > flac?

karol wrote:

Not necessarily, maybe the input options fro whatever he wants to do with these audio files are limited and flac has been deemed the optimal solution.

Indeed, point taken. I just always feel the need to speak up whenever someone wants to "convert" their audio files to FLAC, since the only real solution is to rip straight from the source rather than actually converting files. tongue

On-topic, I've had caudec sitting on my system for a while, and haven't gotten around to doing anything with it. Depending on what the audio codec in the MP4 is, it can do mult-threaded batch conversion. I don't think it can take MP3 or AAC as input, though, so there's a good chance it's not an option.

Offline

Board footer

Powered by FluxBB