2019년 4월 9일 화요일

[Linux/MacOS] 여러개의 동영상 합치기 (FFMPEG+Shell스크립트)


소니 카메라를 사용하다 보면
AVCHD 파일안에 여러개의 MTS파일이 있는 경우를 볼 수 있다.

이경우 AVCHD를 바로 인식하는 인코더(HandBrake) 를 사용하면 편하지만
각각의 파일만 있을 경우 낭패다.

필자는 방법을 찾는것의 귀찬음으로 인하여 FFMPEG 를 활용하여
파일을 합치는 방법을 기재한다.


1.방법

#1 [다운로드] 를 클릭하여 videomerge.zip을 다운받는다.
#2 이름이 분류된 파일을 추가한다.(예1-1 , 1-2 , 2-1 , 2-2)
#3 터미널에서 해당 디렉토리에 들어가 videomerge_2.sh를 실행한다.


1
2
3
cd ~/Download/videomerge

sh videomerge_2.sh

#4 결과물을 확인한다.

#1~4 까지 진행 하였을때 각 숫자로 구분된 파일로 합쳐진것을 확인 할 수 있다.

2. FFMPEG란?

[공식사이트]

대부분의 인코더 + 동영상플레이어 들의 기반이된
OPEN소스 프로젝트다.
쉽게 이야기 하자면 독자들이 알고 있는
대부분의 동영상 플레이어( 다음 팟플레이어/곰플레이어/VLC 등등)
인코더(곰인코더/팟인코더/Handbrake 등등)나
스트리밍사이트(아프리카TV/니코니코동화 등등)

같은 동영상을 사용하는 다양한 서비스의 기반기술이다.

3.스크립트 분석


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/sh
for fname in $(ls *.MTS);
do fname_f=${fname:0:1};
for fname_l in $(ls $fname_f-*.MTS);
do echo "file '$fname_l'";
done > $fname_f.txt ;
done

for x in $(ls *.txt);
do y=${x:0:1};
./ffmpeg -f concat -i $y.txt -c copy -r 29.97 -threads 2 -acodec aac -vcodec libx264 $y.mp4
done

rm -rf *.txt

Shell스크립트 작성이 미숙한 필자 이기에
다소 난잡함이 있음을 먼저 사죄한다.

2 ~ 7 라인까지는 FFMPEG사용을 위한 리스트를 만드는 구문이다.

file '1-1.MTS'
file '1-2.MTS'
위와 같은 형식의 리스트를 1.txt 라는 파일로 생성한다.

9~12 라인은 디렉토리의 txt파일을 읽어 FFMPEG를 실행시키는 구문이다.

9
10
11
12
for x in $(ls *.txt);
do y=${x:0:1};
./ffmpeg -f concat -i $y.txt -c copy -r 29.97 -threads 2 -acodec aac -vcodec libx264 $y.mp4
done

11 라인이 실제로 FFMPEG가 동작하는 구문이다.

11
./ffmpeg -f concat -i $y.txt -c copy -r 29.97 -threads 2 -acodec aac -vcodec libx264 $y.mp4

필자는 -r 29.97 -threads 2 -acodec aac -vcodec libx264 함수를 선언하여
[사용CPU쓰레드:2/프레임:29.97/오디오:AAC/비디오:H264/확장자:mp4]
로 지정된 파일로 인코딩 하였다.

별도의 인코딩 없이 합치기만 하려면
11라인을 다음과 같이 바꾸면 된다.

11
./ffmpeg -f concat -i $y.txt -c copy $y.MTS


14라인은 디렉토리에 있는 모든 txt파일을 삭제한다.
삭제되면 안되는 txt파일이 있다면 주의하자.

4.마치며

FFMPEG는 대부분의 인코더들의 기본이되는만큼
다양한 방법으로 활용이 가능한 프로그램이다.

사용하기에 따라 무궁무진한 방법으로 동영상에 대한
자동화가 가능할 부분으로 생각된다.

댓글 5개:

  1. Gioco Digale (VIP) Deposit Bonus | Updated 2021
    In a country where gambling is illegal, online casinos are in gioco digitale which you can only 카지노 가입 쿠폰 withdraw money on games with an 188bet

    답글삭제
  2. Casino | Dr. MD
    › entertainment › casino-casino › entertainment › casino-casino Casino at Dr. MD and MGM National Harbor. 여수 출장안마 Enjoy fun 김포 출장안마 at our Casino, Slots, Table Games and 세종특별자치 출장마사지 Casino 보령 출장샵 games anywhere in New Jersey. Dr. 영천 출장안마 MD

    답글삭제
  3. Casinos in the UK - How to find good games - GrizzGo
    So, what do air jordan 18 retro yellow suede order we mean by “casinos in air jordan 18 retro men red online store the UK”? to find a casino and live how to find air jordan 18 retro varsity red casino games on where to get air jordan 18 stockx a mobile 스코어 사이트 phone device in 2021.

    답글삭제
  4. Double-Deck Blackjack considered one of the|is amongst the|is likely considered one of the} most popular games within the on line casino. Each Player's wager is a bet against the Dealer's hand. The 카지노 object of this card recreation is to have the total level value of the playing cards dealt to you exceed the point value of the Dealer's hand without going over 21.

    답글삭제
  5. But, if the game begins to get out of hand, we want you to know that assist is available. Watch a enjoyable short video so have the ability to|you probably can} strategy a desk with extra confidence; bear in mind, our pleasant dealers are there {to assist you|that will assist you|that can help you} too. Experience a gaming surroundings unlike to|not like} any other Las Vegas on line casino at The Cosmopolitan. Experience a multitude number} of video games with an inviting 점보카지노 atmosphere. Shine brilliant within the math world by studying how to to|tips on how to} discover the products of 7. Have your individual math-themed party by studying how to to|tips on how to} discover the products of 4.

    답글삭제