You are not logged in.
Pages: 1
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
Offline
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
@Darksoul71
OP wants flac, not mp3s, but otherwise it just might work :-)
Offline
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
for file in *.mp4; do function $file done
Profit?
Not if the filenames contain spaces...
Offline
I knew I forgot something, I thought it was just indentation.
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
WorMzy wrote:for file in *.mp4; do function $file done
Profit?
Not if the filenames contain spaces...
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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
Or, without sed:
ffmpeg -i "$i" "${i/mp4/flac}"
Offline
Would
for i in *.mp4; do
ffmpeg -i "$i" "${i%%.*}.flac"
done
work?
Offline
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
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
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
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
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
@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
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
@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 ), 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
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.
The OP wants mp3 (or ogg
), 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
Lol...in the end I tend not to question the purpose of things that someone asks
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
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
Transcode from a lossy to lossless codec, and all you get is bigger files. The OP wants mp3 (or ogg
), 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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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
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.
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
Pages: 1